Cookie中用户登录信息登录验证
public class FormServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String form = "<form action='LoginCodeServlet' method='post'>"+
"Name:<input type='text' name='name'/><br/>"+
"Pwd:<input type='password' name='pwd'/><br/>"+
"验证码:<input type='text' name='code'/><img src='ImgServlet'></img><br/>"+
"<input type='submit' value='登录'/> </form>";
out.println(form);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
---------------------------------------------------------------------------------------------------------
public class LoginCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String code = request.getParameter("code");//用户输入的code
String sCode = (String) request.getSession().getAttribute("sCode");//放在session中真实code
if(sCode==null || !sCode.equals(code)){
out.println("验证码错误!");
}else{
//验证码正确,然后再获取用户名和密码,到后台进行用户信息验证
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
if(name!=null && !name.trim().equals("") &&
pwd!=null && !pwd.trim().equals("")){
System.out.println("到后台数据库进行信息验证:"+name+","+pwd);
out.println("登录成功:"+name+","+pwd);
}else{
out.println("验证码正确,但Name或Pwd信息未输全");
}
}
//※验证码用过一次之后,就要让它失效!!!否则容易被黑
request.getSession().removeAttribute("sCode");//清除容器中的属性
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
-------------------------------------------------------------------------------------------------------------------
public class ImgServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpg");
int w=60;
int h=30;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setFont(new Font("a", Font.BOLD, 18));
Random r = new Random();
int a = r.nextInt(10000);
//把正确的验证码存储到session中
request.getSession().setAttribute("sCode", ""+a);
g.drawString(""+a, 0, h);
g.dispose();
ImageIO.write(img, "JPEG", response.getOutputStream());
}
}
----------------------------------------------------------------------------------------------------------------------
<a href="FormServlet">用户登录--验证码技术演示</a>