1、session测试Session1.javapackage servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class session1 extends
2、HttpServletOverrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub this.doPost(req, resp);Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOExceptio
3、n / TODO Auto-generated method stub HttpSession session = req.getSession(); session.setAttribute(name, longestore); System.out.println(已经向HttpSession对象存储了共享内容name=longestory.); GetSession.javapackage servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;
4、import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class getSession extends HttpServletOverrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Serv
5、letException, IOException / TODO Auto-generated method stub this.doPost(req, resp);Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub resp.setContentType(text/html;charset=utf-8); PrintWriter out = r
6、esp.getWriter(); HttpSession session = req.getSession(); String name = (String) session.getAttribute(name); out.print(你存储在共享区域的内容是name=+name+); Web.xml libraryproject index.html index.htm index.jsp default.html default.htm default.jsp getSessionservlet.getSessionsession1servlet.session1getSession/se
7、condsession1/firstTextHttpServlet.javapackage servlet;import java.io.IOException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import jav
8、ax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class textHttpServlet extends HttpServletOverrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub this.doPost(req, resp);Overr
9、ide protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stubDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd hh:mm:ss);Calendar calendar = Calendar.getInstance();HttpSession session = req.getSession();System.
10、out.println(Session的ID为+session.getId();calendar.setTimeInMillis(session.getCreationTime();System.out.println(Session创建的时间为+formatter.format(calendar.getTime();calendar.setTimeInMillis(session.getLastAccessedTime();System.out.println(Session最后活动的时间为+formatter.format(calendar.getTime();System.out.pri
11、ntln(当前Session是否为新?+session.isNew();System.out.println(Session默认的活动时间为+session.getMaxInactiveInterval()/60+分钟);textHttpServletservlet.textHttpServlettextHttpServlet/three一次性验证码CheckedImgServlet.javapackage servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Gr
12、aphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import java
13、x.servlet.http.HttpServletResponse;WebServlet(/checkimg) /* 只有一个不能是/ xxx/的路径名只对应一个*/public class CheckImgServlet extends HttpServletprivate Graphics graphics2;Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated met
14、hod stub this.doPost(req, resp);Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub int width=120; int height=30; BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RG
15、B); Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(getRandColor(200,250); graphics.fillRect(0, 0, width, height); graphics.setColor(Color.WHITE); graphics.drawRect(0, 0, width-1, height-1); Graphics2D graphics2d = (Graphics2D) graphics; graphics2d.setFont(new Font(宋体,Font.BOLD,18
16、); String words = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890; Random random = new Random(); StringBuffer buffer = new StringBuffer(); int x =10; for (int i = 0; i 4; i+) graphics2d.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110); int jiaodu
17、 = random.nextInt(60)-30; double theta = jiaodu*Math.PI/180; int index = random.nextInt(words.length(); char c = words.charAt(index); buffer.append(c); graphics2d.rotate(theta,x,20); graphics2d.drawString(String.valueOf(c), x, 20); graphics2d.rotate(-theta,x,20); x +=30; req.getSession().setAttribut
18、e(checkcode_session, buffer.toString(); graphics.setColor(getRandColor(160,200); int x1; int x2; int y1; int y2; for (int i = 0; i 255) fc = 255; if(bc 255) bc=255; int r =fc+random.nextInt(bc-fc); int g = fc+random.nextInt(bc-fc); int b = fc + random.nextInt(bc -fc); return new Color(r,g,b);LoginSe
19、rvlet.javapackage servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;WebServlet(/login)public class LoginS
20、ervlet extends HttpServletOverrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub this.doPost(req, resp);Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletExcept
21、ion, IOException / TODO Auto-generated method stub req.setCharacterEncoding(utf-8); String username = req.getParameter(username); String password = req.getParameter(password); String checkcode = req.getParameter(checkcode); String checkcode_session = (String) req.getSession().getAttribute(checkcode_
22、session); req.getSession().removeAttribute(checkcode_session); if(checkcode_session = null | !checkcode_session.equals(checkcode) req.setAttribute(msg, 验证码错误); req.getRequestDispatcher(login.jsp).forward(req, resp); return; if(admin.equals(username)&admin.equals(password) req.getSession().setAttribu
23、te(username, username); resp.sendRedirect(/yzm/welcome.jsp); else req.setAttribute(msg, 用户名或密码错误); req.getRequestDispatcher(login.jsp).forward(req, resp); Login.jspInsert title herefunction change() document.getElementById(myimg).src=/yzm/checkimg?timeStamp=+new Date().getTime();登录页面$requestScope.msg 用户名密码/bod
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1