session测试.docx
- 文档编号:11389224
- 上传时间:2023-02-28
- 格式:DOCX
- 页数:20
- 大小:69.04KB
session测试.docx
《session测试.docx》由会员分享,可在线阅读,更多相关《session测试.docx(20页珍藏版)》请在冰豆网上搜索。
session测试
Session1.java
packageservlet;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
publicclasssession1extendsHttpServlet{
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
this.doPost(req,resp);
}
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
HttpSessionsession=req.getSession();
session.setAttribute("name","longestore");
System.out.println("已经向HttpSession对象存储了共享内容name=longestory.....");
}
}
GetSession.java
packageservlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
publicclassgetSessionextendsHttpServlet{
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
this.doPost(req,resp);
}
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
resp.setContentType("text/html;charset=utf-8");
PrintWriterout=resp.getWriter();
HttpSessionsession=req.getSession();
Stringname=(String)session.getAttribute("name");
out.print("
你存储在共享区域的内容是name="+name+"
");}
}
Web.xml
xmlversion="1.0"encoding="UTF-8"?
>
xsi="http: //www.w3.org/2001/XMLSchema-instance"xmlns="http: //xmlns.jcp.org/xml/ns/javaee"xsi: schemaLocation="http: //xmlns.jcp.org/xml/ns/javaeehttp: //xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID"version="3.1"> TextHttpServlet.java packageservlet; importjava.io.IOException; importjava.text.DateFormat; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; publicclasstextHttpServletextendsHttpServlet{ @Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub this.doPost(req,resp); } @Override protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub DateFormatformatter=newSimpleDateFormat("yyyy-MM-ddhh: mm: ss"); Calendarcalendar=Calendar.getInstance(); HttpSessionsession=req.getSession(); System.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.println("当前Session是否为新? "+session.isNew()); System.out.println("Session默认的活动时间为"+session.getMaxInactiveInterval()/60+"分钟"); } } 一次性验证码 CheckedImgServlet.java packageservlet; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.Graphics2D; importjava.awt.image.BufferedImage; importjava.io.IOException; importjava.util.Random; importjavax.imageio.ImageIO; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; @WebServlet("/checkimg")/*@只有一个不能是/xxx/的路径名只对应一个*/ publicclassCheckImgServletextendsHttpServlet{ privateGraphicsgraphics2; @Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub this.doPost(req,resp); } @Override protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub intwidth=120; intheight=30; BufferedImagebufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphicsgraphics=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); Graphics2Dgraphics2d=(Graphics2D)graphics; graphics2d.setFont(newFont("宋体",Font.BOLD,18)); Stringwords="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; Randomrandom=newRandom(); StringBufferbuffer=newStringBuffer(); intx=10; for(inti=0;i<4;i++){ graphics2d.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); intjiaodu=random.nextInt(60)-30; doubletheta=jiaodu*Math.PI/180; intindex=random.nextInt(words.length()); charc=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().setAttribute("checkcode_session",buffer.toString()); graphics.setColor(getRandColor(160,200)); intx1; intx2; inty1; inty2; for(inti=0;i<30;i++){ x1=random.nextInt(width); x2=random.nextInt(12); y1=random.nextInt(height); y2=random.nextInt(12); graphics.drawLine(x1,y1,x1+x2,x2+y2); } graphics.dispose(); ImageIO.write(bufferedImage,"jpg",resp.getOutputStream()); } /** *取其某一范围的color *@paramfc * *@parambc *@returnColor */ privateColorgetRandColor(intfc,intbc){ //TODOAuto-generatedmethodstub Randomrandom=newRandom(); if(fc>255){ fc=255; } if(bc>255){ bc=255; } intr=fc+random.nextInt(bc-fc); intg=fc+random.nextInt(bc-fc); intb=fc+random.nextInt(bc-fc); returnnewColor(r,g,b); } } LoginServlet.java packageservlet; importjava.io.IOException; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; @WebServlet("/login") publicclassLoginServletextendsHttpServlet{ @Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub this.doPost(req,resp); } @Override protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{ //TODOAuto-generatedmethodstub req.setCharacterEncoding("utf-8"); Stringusername=req.getParameter("username"); Stringpassword=req.getParameter("password"); Stringcheckcode=req.getParameter("checkcode"); Stringcheckcode_session=(String)req.getSession().getAttribute("checkcode_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().setAttribute("username",username); resp.sendRedirect("/yzm/welcome.jsp"); }else{ req.setAttribute("msg","用户名或密码错误"); req.getRequestDispatcher("login.jsp").forward(req,resp); } } } Login.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http: //www.w3.org/TR/html4/loose.dtd">
functionchange(){
document.getElementById("myimg").src="/yzm/checkimg?
timeStamp="+newDate().getTime();
}
登录页面
red">${requestScope.msg} pointer;"/>用户名 密码
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- session 测试
![提示](https://static.bdocx.com/images/bang_tan.gif)