好文档 - 专业文书写作范文服务资料分享网站

黑马程序员javaweb教程:HttpSession练习1:登录(sessionlogin)

天下 分享 时间: 加入收藏 我要投稿 点赞

HttpSession练习1:登录(sessionlogin)

1 分析

? 给出一个index.jsp页面,其中包含登录表单,提交到LoginServlet来验证用户是否

已经登录;

? 给出LoginServlet,验证index.jsp中的用户名和密码:

? 验证通过,把用户信息保存到session,并跳转到AServlet; ? 验证失败,把输出用户名或密码错误!

? AServlet,受保护内容。查检session中是否包含用户信息:

? 如果session中包含用户信息,那么输出用户信息,并显示BServlet的超链接; ? 如果session中不包含用户信息,那么输出您还没登录,并显示返回index.jsp

页面的链接;

? BServlet,与AServlet相同。

2 index.jsp

itcast系统登录页面


用户名:
密 码:
3 LoginServlet

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\); request.setCharacterEncoding(\); String username = request.getParameter(\); String password = request.getParameter(\); String cxt = request.getContextPath(); if(validate(username, password)) { HttpSession session = request.getSession(); session.setAttribute(\, username); response.sendRedirect(cxt + \); } else {

} } String s = \点击返回到登录页面!\; out.print(\您还没有登录\); out.print(s); } else { HttpSession练习2:验证码(validatelogin)

1 回顾VerifyCode类(看懂即可)

public class VerifyCode { private int w = 70;

private int h = 35; private Random r = new Random(); private String[] fontNames = {\宋体\, \华文楷体\, \黑体\, \华文新魏\, \华文隶书\, \微private String codes = private Color bgColor = new Color(240, 240, 240); private String text; private Color randomColor() { } private Font randomFont() { } private void drawLine(BufferedImage image) { } private char randomChar() { } private BufferedImage createImage() { int index = r.nextInt(codes.length()); return codes.charAt(index); int num = 5; Graphics2D g2 = (Graphics2D)image.getGraphics(); for(int i = 0; i < num; i++) { } int x1 = r.nextInt(w); int y1 = r.nextInt(h); int x2 = r.nextInt(w); int y2 = r.nextInt(h); g2.setStroke(new BasicStroke(1.5F)); g2.setColor(Color.BLUE); g2.drawLine(x1, y1, x2, y2); int index = r.nextInt(fontNames.length); String fontName = fontNames[index]; int style = r.nextInt(4); int size = r.nextInt(5) + 24; return new Font(fontName, style, size); int red = r.nextInt(256); int green = r.nextInt(256); int blue = r.nextInt(256); return new Color(red, green, blue); 软雅黑\, \楷体_GB2312\}; \;

} } BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.setColor(this.bgColor); g2.fillRect(0, 0, w, h); return image; public BufferedImage getImage() { } public String getText() { } public static void output(BufferedImage image, OutputStream out) } throws IOException { ImageIO.write(image, \, out); return text; BufferedImage image = createImage(); Graphics2D g2 = (Graphics2D)image.getGraphics(); StringBuilder sb = new StringBuilder(); // 向图片中画4个字符 for(int i = 0; i < 4; i++) { } this.text = sb.toString(); drawLine(image); return image; String s = randomChar() + \; sb.append(s); float x = i * 1.0F * w / 4; g2.setFont(randomFont()); g2.setColor(randomColor()); g2.drawString(s, x, h); 2 页面中显示验证码

validatelogin项目可以在sessionlogin的基础上进行修改!在index.jsp添加一个文本框,用来输入验证码,还需要一个元素用来显示验证码。添加一个VerifyCodeServlet,给页面中的元素使用。

3 VerfiyCodeServlet

? 使用VerifyCode生成Image;

? 获取VerifyCode的text,保存到session中; ? 设置response的内容类型为图片; ? 把图片写入到响应流中。 VerifyCode vc = new VerifyCode(); BufferedImage image = vc.getImage(); String code = vc.getText(); request.getSession().setAttribute(\, code); response.setContentType(\); VerifyCode.output(image, response.getOutputStream());

4 修改index.jsp

itcast系统登录页面


用户名:
密 码:
验证码:
看不清,换一张

5 修改LoginServlet

response.setContentType(\); request.setCharacterEncoding(\); // 获取用户输入的验证码 String vCode = request.getParameter(\); // 获取session中正确的验证码

HttpSession session = request.getSession(); String code = (String)session.getAttribute(\); String cxt = request.getContextPath(); // 区别大小写比较验证码,如果不相同 if(!code.equalsIgnoreCase(vCode)) { } // 当验证码正确时,才会去验证用户名和密码 String username = request.getParameter(\); String password = request.getParameter(\); if(validate(username, password)) { session.setAttribute(\, username); response.sendRedirect(cxt + \); String s = \点击返回到登录页面!\; PrintWriter out = response.getWriter(); out.print(\用户名密码错误!\); out.print(s);

黑马程序员javaweb教程:HttpSession练习1:登录(sessionlogin)

HttpSession练习1:登录(sessionlogin)1分析?给出一个index.jsp页面,其中包含登录表单,提交到LoginServlet来验证用户是否已经登录;?给出LoginServlet,验证index.jsp中的用户名和密码:?验证通过,把用户信息保存到s
推荐度:
点击下载文档文档为doc格式
55aj91b1ld55mbv23rb17u3cm9b9uy004n3
领取福利

微信扫码领取福利

微信扫码分享