JSP登录中Session的用法实例详解
本文实例讲述了jsp登录中session的用法。分享给大家供大家参考,具体如下:
登录页面
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <div style="float:left;margin-top:100px;margin-left:200px;width:400px;height:300px;background:gray;"> <form action="indexservlet" method="post"> <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px"> <div style="margin-left:70px;float:left;line-height:30px">账号:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="user"/> </div> <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px"> <div style="margin-left:70px;float:left;line-height:30px">密码:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="password"/> </div> <div style="float:left;margin-top:50px;width:400px;height:30px;background:gray;"> <input style="float:left;width:60px;height:30px;margin-left:170px;border:none;" type="submit" name="ok" value="登录"/> </div> </form> </div> </body> </html>
检测账号密码以及设置session的indexservlet
import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * servlet implementation class indexservlet */ @webservlet("/indexservlet") public class indexservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public indexservlet() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub response.getwriter().append("served at: ").append(request.getcontextpath()); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub request.setcharacterencoding("utf-8"); string user = request.getparameter("user"); string password = request.getparameter("password"); string path = request.getcontextpath(); httpsession session=request.getsession(); if ("1".equals(user) && "1".equals(password)) { session.setattribute("name", user); response.sendredirect(path + "/success.jsp"); }else{ response.sendredirect(path + "/index.jsp"); } } }
成功登录页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <% string path = request.getcontextpath(); %> <% object name = session.getattribute("name"); if(name==null){ response.sendredirect(path+"/index.jsp"); } %> <html> <head> <title>成功页面</title> </head> <body> 恭喜你,骚年,<%=session.getattribute("name") %>,成功登陆了! <a href="out.jsp" rel="external nofollow" >注销</a> </body> </html>
注销功能的jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <% string path = request.getcontextpath(); %> <% session.removeattribute("name"); response.sendredirect(path+"/index.jsp"); %> </body> </html>
希望本文所述对大家jsp程序设计有所帮助。
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法