分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

纯JSP简单登录实例

发布时间:2023-09-06 01:28责任编辑:熊小新关键词:暂无标签

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

文件共有四个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简单登录实例

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved