本文实例讲述了纯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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]