session_login
来源:互联网 发布:中文网络搜索引擎名称 编辑:程序博客网 时间:2024/06/04 19:11
login.jsp
<title>Insert title here</title><script type="text/javascript"> function changeImage(){ document.getElementById("myimage").src = "/d_session/checkimage?"+new Date().getTime(); }</script></head><body> <h3>XX网站的登录页面</h3> <font color="red" size="5">${message }</font> <form action="/d_session/login" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> 验证码:<input type="text" name="checkcode"> <img src="/d_session/checkimage" style="cursor: pointer" onclick="changeImage();" id="myimage"><br/> <input type="submit" value="登录"> </form></body>
CheckImageServlet doGet
public class CheckImageServlet extends HttpServlet { private final int WIDTH = 120; private final int HEIGHT = 30; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage bf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) bf.getGraphics();//--Graphics2D is Graphics's child Color color = new Color(203, 222, 225); graphics.setColor(color); graphics.fillRect(0, 0, WIDTH, HEIGHT); String base ="ABCDEFGHIJKLMN"; Random random = new Random(); graphics.setColor(Color.RED); graphics.setFont(new Font("楷体",Font.BOLD,18)); int m = 13; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { int index = random.nextInt(base.length()); char charAt = base.charAt(index); int jiaodu = random.nextInt(60)-30; double theta = jiaodu*Math.PI/180; graphics.rotate(theta, m, 15); graphics.drawString(charAt+"", m, 19); sb.append(charAt); graphics.rotate(-theta, m, 15); m+=20; } request.getSession().setAttribute("checkcode_session", sb.toString()); graphics.setColor(Color.BLUE); for (int i = 0; i < 4; i++) { int x1 = random.nextInt(WIDTH); int x2 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int y2 = random.nextInt(HEIGHT); graphics.drawLine(x1, y1, x2, y2); } graphics.dispose(); ImageIO.write(bf, "png", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
LoginServlet doGet
request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String checkcode = request.getParameter("checkcode"); String checkcode_session = (String) request.getSession().getAttribute("checkcode_session"); if(checkcode==null||!checkcode.equalsIgnoreCase(checkcode_session)){ request.setAttribute("message", "对不起, 您输入验证码信息不正确..."); request.getRequestDispatcher("/login.jsp").forward(request, response); return ; } // 做用户名和密码的校验 // admin if("admin".equals(username)&&"admin".equals(password)){ request.getSession().setAttribute("loginUser", "admin"); response.sendRedirect(request.getContextPath()+"/index.jsp"); }else{ request.setAttribute("message", "对不起, 您 是用户名或密码不正确...");request.getRequestDispatcher("/login.jsp").forward(request, response); }
0 0
- session_login
- ListView,GridView的万能Adapter-------Android中的Adapter能玩出什么花样?
- 搭建ORACLE高可用 高性能 高扩展的 MMM_APE 架构
- Cocos2d-JS 数据持久化
- js如何获取当前月的天数
- Android okHttp上传单张或多张照片
- session_login
- Cocos2d-JS 基于HTTP网络通信
- hello
- String理论知识(学习)
- 欢迎使用CSDN-markdown编辑器
- Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot
- Linux设备模型(1)_基本概念
- 使用PowerDesigner画ER图详细教程
- hdu5396(区间dp)