JSP简单登录实例 不涉及数据库的登录实例



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源码清单

[html] view plaincopyprint?
01.<web-app xmlns=”http://java.sun.com/xml/ns/j2ee”
02.xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
03.xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”
04.version=”2.4″>
05. <welcome-file-list>
06. <welcome-file>welcome.jsp</welcome-file>
07. </welcome-file-list>
08.</web-app>
<web-app xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”
version=”2.4″>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp源码清单


[html] view plaincopyprint?
01.<%@ page contentType=”text/html;charset=UTF-8″ %>
02.<html>
03. <head>
04. <title>JSP简单登录实例</title>
05. </head>
06.
07. <body>
08. <h2>请登录</h2>
09.
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>
<%@ page contentType=”text/html;charset=UTF-8″ %>
<html>
<head>
<title>JSP简单登录实例</title>
</head>

<body>
<h2>请登录</h2>

<form method=”POST” >
Login Name: <input type=”text” name=”Name”><br>
Login Password: <input type=”text” name=”Password” ><br>
<input type=”submit” value=”Send”><br>
<form>

<%
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 {
%>
登录失败:用户名或密码不正确~
<%
}
}
%>
</body>
</html>
logout.jsp源码清单

[html] view plaincopyprint?
01.<%@ page contentType=”text/html;charset=UTF-8″ %>
02.<html>
03.
04. <%
05. session.setAttribute(“Login”, “”);
06. %>
07.
08. <body>
09. <h2>你已经退出登录</h2>
10. </body>
11.
12.</html>
<%@ page contentType=”text/html;charset=UTF-8″ %>
<html>

<%
session.setAttribute(“Login”, “”);
%>

<body>
<h2>你已经退出登录</h2>
</body>

</html>
welcome.jsp源码清单

[html] view plaincopyprint?
01.<%@ page contentType=”text/html” pageEncoding=”UTF-8″ import=”java.util.*”%>
02.
03.<html>
04. <body>
05. <h2>欢迎页面(测试session)</h2>
06.
07. <%
08.
09. 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>