码迷,mamicode.com
首页 > Web开发 > 详细

纯JSP简单登录实例

时间:2017-12-03 22:54:43      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:article   set   char   button   redirect   new   用户   word   目录结构   

 

记一下,免得以后忘记了,又要去查。

文件共有四个web.xml、login.jsp、logout.jsp、welcome.jsp四个文件

测试环境:Tomcat 6.0.x

假设项目名称是LoginSample,我的目录结构是这样的

...\webapps\LoginSample\WEB-INF\web.xml

...\webapps\LoginSample\login.jsp

...\webapps\LoginSample\logout.jsp

...\webapps\LoginSample\welcome.jsp

----------------------------------------------------------------------------------------------

web.xml源码清单

[html] view plain copy
 
  1. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
  4. version="2.4">  
  5.     <welcome-file-list>  
  6.         <welcome-file>welcome.jsp</welcome-file>  
  7.     </welcome-file-list>  
  8. </web-app>  


login.jsp源码清单

[html] view plain copy
 
  1. <%@ page contentType="text/html;charset=UTF-8" %>  
  2. <html>  
  3.  <head>  
  4.    <title>JSP简单登录实例</title>  
  5.  </head>  
  6.    
  7.  <body>  
  8.   <h2>请登录</h2>  
  9.   
  10.   <form method="POST" >  
  11.    Login Name: <input type="text" name="Name"><br>  
  12.    Login Password: <input type="text" name="Password" ><br>  
  13.    <input type="submit" value="Send"><br>  
  14.   <form>  
  15.   
  16.   <%  
  17.       if (request.getParameter("Name") != null   
  18.               && request.getParameter("Password") != null) {    
  19.           String Name = request.getParameter("Name");  
  20.           String Password = request.getParameter("Password");  
  21.           if (Name.equals("a") && Password.equals("a")) {     
  22.               session.setAttribute("Login", "OK");  
  23.               session.setAttribute("myCount", new Integer(1));  
  24.               response.sendRedirect("welcome.jsp");  
  25.           }  
  26.           else {     
  27.               %>  
  28.               登录失败:用户名或密码不正确~  
  29.               <%    
  30.           }   
  31.       }  
  32. %>  
  33.  </body>  
  34. </html>  


logout.jsp源码清单

[html] view plain copy
 
  1. <%@ page contentType="text/html;charset=UTF-8" %>  
  2. <html>  
  3.    
  4.  <%  
  5.     session.setAttribute("Login", "");  
  6.  %>  
  7.   
  8.  <body>  
  9.   <h2>你已经退出登录</h2>  
  10.  </body>  
  11.   
  12. </html>  


welcome.jsp源码清单

[html] view plain copy
 
  1. <%@ page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%>  
  2.   
  3. <html>   
  4.  <body>  
  5.   <h2>欢迎页面(测试session)</h2>  
  6.     
  7.   <%  
  8.     
  9.   String  Login = (String)session.getAttribute("Login");  
  10.   int     nCount=0;  
  11.   
  12.   
  13.   if (Login != null && Login.equals("OK")) {  
  14.       Integer myCount = (Integer)session.getAttribute("myCount");  
  15.       if(myCount!=null)  
  16.       {  
  17.           nCount = myCount.intValue();    
  18.           nCount = nCount + 1;  
  19.           session.setAttribute("myCount",new Integer(nCount));  
  20.       }  
  21.       %>  
  22.       登录成功,myCount=<%=nCount%></br>  
  23.       <input type=button value="退出" onclick="javascript:location.href=‘logout.jsp‘">  
  24.       <%  
  25.   }  
  26.   else {  
  27. %>  
  28.      <jsp:forward page="login.jsp"/>  
  29. <%  
  30.    }  
  31.    %>  
  32.      
  33.    </body>  
  34. </html>  


 from: http://blog.csdn.net/lee353086/article/details/8080933

 

纯JSP简单登录实例

标签:article   set   char   button   redirect   new   用户   word   目录结构   

原文地址:http://www.cnblogs.com/ec04/p/7967461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!