session应用----登录验证小案例

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <base href="<%=basePath%>">
10     <title>My JSP 'index.jsp' starting page</title>
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15     <meta http-equiv="description" content="This is my page">
16   </head>
17   <script type="text/javascript">
18           function func() {
19             document.getElementById("ima1").src="VerifyCodeServlet?a="+new Date().getTime();
20         }
21   </script>
22   <body>
23   <%
24           String msg="";
25           String s=(String)request.getAttribute("msg");
26           if(s!=null) msg=s;
27    %>
28   <h1>登录</h1>
29   <font color="red"><%=msg %></font>
30         <form action="AServlet" method="post">
31             用户名:<input type="text" name="uname"><br>
32             验证码:<input type="text" name="verifyCode" size="4">
33             <img id="ima1" src="VerifyCodeServlet"/>
34             <a href="javascript:func();">看不清,换一张</a><br>
35             <input type="submit" value="提交">
36         </form>
37   </body>
38 </html>
index.jsp
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <base href="<%=basePath%>">
10     <meta http-equiv="pragma" content="no-cache">
11     <meta http-equiv="cache-control" content="no-cache">
12     <meta http-equiv="expires" content="0">    
13     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
14     <meta http-equiv="description" content="This is my page">
15   </head>
16   <%
17       String uname=(String)request.getSession().getAttribute("uname");
18       if(uname==null)
19       {
20           request.setAttribute("msg", "请先登录!");
21           response.sendRedirect("index.jsp");
22       }
23    %>
24   <body>
25       欢迎<%=uname %>
26   </body>
27 </html>
suc.jsp
 1 package cn.yzu;
 2 import java.awt.image.BufferedImage;
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 public class VerifyCodeServlet extends HttpServlet {
10     public void doGet(HttpServletRequest request, HttpServletResponse response)
11             throws ServletException, IOException {
12          VerifyCode verifyCode = new VerifyCode();
13          BufferedImage bi = verifyCode.getImage();
14          request.getSession().setAttribute("vc", verifyCode.getText());
15          verifyCode.output(bi,response.getOutputStream());
16     }
17 
18 }
VerifyCodeServlet
 1 package cn.yzu;
 2 import java.io.IOException;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 public class AServlet extends HttpServlet {
 8     public void doPost(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         String uname=request.getParameter("uname");
11         String verifyCode=request.getParameter("verifyCode");
12         if(uname.equals("fmy")&&verifyCode.equalsIgnoreCase((String) request.getSession().getAttribute("vc")))
13         {
14             request.getSession().setAttribute("uname", uname);
15             response.sendRedirect("suc.jsp");
16         }
17         else
18         {
19             request.setAttribute("msg", "用户名或者验证码错误!");
20             request.getRequestDispatcher("index.jsp").forward(request, response);
21         }
22     }
23 }
AServlet

运行界面:

 

注:verifyCode类见博客:http://www.cnblogs.com/fengmingyue/p/5987814.html

posted @ 2016-10-26 15:59  凌晨。。。三点  阅读(312)  评论(0编辑  收藏  举报