纯JSP实现的简单登录示例
程序员文章站
2023-11-13 15:54:10
本文实例讲述了纯jsp实现的简单登录的方法。分享给大家供大家参考,具体如下:
文件共有四个web.xml、login.jsp、logout.jsp、welcome.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源码清单:
<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源码清单:
<%@ 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源码清单:
<%@ page contenttype="text/html;charset=utf-8" %> <html> <% session.setattribute("login", ""); %> <body> <h2>你已经退出登录</h2> </body> </html>
welcome.jsp源码清单:
<%@ page contenttype="text/html" pageencoding="utf-8" import="java.util.*"%> <html> <body> <h2>欢迎页面(测试session)</h2> <% 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%></br> <input type=button value="退出" onclick="javascript:location.href='logout.jsp'"> <% } else { %> <jsp:forward page="login.jsp"/> <% } %> </body> </html>
希望本文所述对大家jsp程序设计有所帮助。
上一篇: Python中字符串的格式化方法小结