index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script type="text/javascript"> function changeCode(){ var img=document.getElementsByTagName("img")[0]; img.src="/session/ShowCode?random="+Math.random(); } </script> </head> <body> <% String msg=(String)request.getAttribute("msg"); if(msg!=null){ out.print(msg); } %> <form action="/session/LoginCheck"> 用户名:<input type="text" name="userName"></br> 密码:<input type="password" name="pwd"></br> 验证码:<input type="text" name="code"><img src="/session/ShowCode" onclick="changeCode()"><a href="javascript:changeCode()">看不清换一张</a></br> <input type="submit" value="登录"> </form> </body> </html>
验证登录
package qingxia.tang; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginCheck */ @WebServlet("/LoginCheck") public class LoginCheck extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginCheck() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter pw=response.getWriter(); String userName = request.getParameter("userName"); String pwd = request.getParameter("pwd"); String code = request.getParameter("code"); //从session中获取code值 String ycode =(String) request.getSession().getAttribute("ycode"); if("tom".equals(userName) && "123".equals(pwd)){ if(!ycode.equalsIgnoreCase(code)){ request.setAttribute("msg", "验证码错误!"); request.getRequestDispatcher("/index.jsp").forward(request, response); return; } pw.print("登录成功!"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
生成验证码
package qingxia.tang; 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; import cn.dsna.util.images.ValidateCode; /** * Servlet implementation class ShowCode */ @WebServlet("/ShowCode") public class ShowCode extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ValidateCode vc=new ValidateCode(110, 25, 4, 9); vc.write(response.getOutputStream()); String code = vc.getCode(); //将code写入session中 request.getSession().setAttribute("ycode", code); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
效果图:
http://localhost:8080/session/index.jsp