纯JSP简单登录实例

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

文件共有四个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源码清单


	
		welcome.jsp
	


login.jsp源码清单

<%@ page contentType="text/html;charset=UTF-8" %>

 
   JSP简单登录实例
 
 
 
  

请登录

Login Name:
Login Password:

<% if (request.getParameter("Name") != null && request.getParameter("Password") != null) { String Name = request.getParameter("Name"); String Password = request.getParameter("Password"); if (Name.equals("a") && Password.equals("a")) { session.setAttribute("Login", "OK"); session.setAttribute("myCount", new Integer(1)); response.sendRedirect("welcome.jsp"); } else { %> 登录失败:用户名或密码不正确~ <% } } %>


logout.jsp源码清单

<%@ page contentType="text/html;charset=UTF-8" %>

 
 <%
	session.setAttribute("Login", "");
 %>

 
  

你已经退出登录


welcome.jsp源码清单

<%@ page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%>

 
 
  

欢迎页面(测试session)

<% String Login = (String)session.getAttribute("Login"); int nCount=0; if (Login != null && Login.equals("OK")) { Integer myCount = (Integer)session.getAttribute("myCount"); if(myCount!=null) { nCount = myCount.intValue(); nCount = nCount + 1; session.setAttribute("myCount",new Integer(nCount)); } %> 登录成功,myCount=<%=nCount%>
<% } else { %> <% } %>


 

 

你可能感兴趣的:(Java,JavaScript或前端)