JSP登錄中Session的用法實(shí)例詳解
本文實(shí)例講述了JSP登錄中Session的用法。分享給大家供大家參考,具體如下:
登錄頁(yè)面
<%@ 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">賬號(hào):</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>
檢測(cè)賬號(hào)密碼以及設(shè)置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");
}
}
}
成功登錄頁(yè)面
<%@ 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>成功頁(yè)面</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>
希望本文所述對(duì)大家jsp程序設(shè)計(jì)有所幫助。
相關(guān)文章
一個(gè)jsp+AJAX評(píng)論系統(tǒng)
一個(gè)jsp+AJAX評(píng)論系統(tǒng)...2007-05-05
Java Servlet生成JSON格式數(shù)據(jù)并用jQuery顯示的方法
這篇文章主要介紹了Java Servlet生成JSON格式數(shù)據(jù)并用jQuery顯示的方法,涉及jsp基于Servlet操作json數(shù)據(jù)及使用jQuery顯示的相關(guān)技巧,需要的朋友可以參考下2015-12-12
jsp Request獲取url信息的各種方法對(duì)比
下面小編就為大家?guī)?lái)一篇jsp Request獲取url信息的各種方法對(duì)比。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
JavaWeb實(shí)現(xiàn)圖形報(bào)表折線圖的方法
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)圖形報(bào)表折線圖的方法,涉及JSP包的引用、圖形操作、配置文件設(shè)置及字符串操作技巧,需要的朋友可以參考下2016-06-06
詳解hibernate自動(dòng)創(chuàng)建表的配置
這篇文章主要介紹了詳解hibernate自動(dòng)創(chuàng)建表的配置的相關(guān)資料,需要的朋友可以參考下2017-06-06
JSP 動(dòng)態(tài)樹的實(shí)現(xiàn)
在實(shí)際的項(xiàng)目開(kāi)發(fā)中有很多是有用到動(dòng)態(tài)樹,所謂的動(dòng)態(tài)樹就是可以支持無(wú)限級(jí)子節(jié)點(diǎn)。今天我就去給大家實(shí)際演示一下動(dòng)態(tài)樹如何實(shí)現(xiàn)。2009-04-04
response.getWriter().write()向前臺(tái)打印信息亂碼問(wèn)題解決
本節(jié)主要介紹了response.getWriter().write()向前臺(tái)打印信息亂碼問(wèn)題解決方法,需要的朋友可以參考下2014-08-08
springMVC使用jsp:include嵌入頁(yè)面的兩種方法(推薦)
下面小編就為大家?guī)?lái)一篇springMVC使用jsp:include嵌入頁(yè)面的兩種方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

