第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/5/25 22:31:40 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c55aj91b1ld55mbv23rb17u3cm9b9uy004n3_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top