JSP登录中Session的用法实例详解
程序员文章站
2022-03-08 14:14:45
本文实例讲述了jsp登录中session的用法。分享给大家供大家参考,具体如下:登录页面<%@ page language="java" contenttype="text/html; char...
本文实例讲述了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程序设计有所帮助。
上一篇: 红米Note 9系列全球发布会官宣:10X要来?4月30日见
下一篇: 小程序新版订阅消息模板消息