1. HttpSession是什么
HttpSession是由JavaWeb提供的,用来会话跟踪的类。session是服务器端对象,保存在服务器端
HttpSession是Servlet三大域对象之一(request、session、application(ServletContext))
2. HttpSession的作用
会话范围:会话范围是某个用户从首次访问服务器开始,到该用户关闭浏览器结束!
*会话:一个用户对服务器的多次连贯性请求!所谓连贯性请求,就是该用户多次请求中间没有关闭浏览器!
服务器会为每个客户端创建一个session对象,它们被服务器保存到一个Map(session缓存)
*Servlet中得到session对象:HttpSession session = request.getSession();
*Jsp中得到session对象:session是jsp内置对象之下
session域相关方法:
——void setAttribute(String name, Object value);
——Object getAttribute(String name);
——void removeAttribute(String name);
==========================================================================
保存用户登录信息
Login.jsp:提供登录页面&&显示提示信息
LoginServlet:判断用户名与密码时候合法
if(合法)保存用户信息到session,进入MyJsp.jsp
else 保存错误提示到request
MyJsp.jsp:登录后直接进入界面
MyCentre.jsp:登录后才能进入
==========================================================================
HttpSession是由JavaWeb提供的,用来会话跟踪的类。session是服务器端对象,保存在服务器端
HttpSession是Servlet三大域对象之一(request、session、application(ServletContext))
2. HttpSession的作用
会话范围:会话范围是某个用户从首次访问服务器开始,到该用户关闭浏览器结束!
*会话:一个用户对服务器的多次连贯性请求!所谓连贯性请求,就是该用户多次请求中间没有关闭浏览器!
服务器会为每个客户端创建一个session对象,它们被服务器保存到一个Map(session缓存)
*Servlet中得到session对象:HttpSession session = request.getSession();
*Jsp中得到session对象:session是jsp内置对象之下
session域相关方法:
——void setAttribute(String name, Object value);
——Object getAttribute(String name);
——void removeAttribute(String name);
==========================================================================
保存用户登录信息
Login.jsp:提供登录页面&&显示提示信息
LoginServlet:判断用户名与密码时候合法
if(合法)保存用户信息到session,进入MyJsp.jsp
else 保存错误提示到request
MyJsp.jsp:登录后直接进入界面
MyCentre.jsp:登录后才能进入
==========================================================================
Login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/Session/servlet/Login" method="post">
<table>
<tr><td>用户名:</td><td><input type="text" name="username"></td></tr>
<tr><td>密码:</td><td><input type="password" name="password"></td></tr>
</table>
<%
String message="";
String mge=(String)request.getAttribute("message");
if(mge!=null) message=mge;
%>
<%=message %>
<font color="red"></font>
<input type="submit" value="提交"/>
</form>
</body>
</html>
LoginServlet:
package cn.Session.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理中文问题
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username.equalsIgnoreCase("Tom")&&password.equalsIgnoreCase("147258")){
//登录成功
/*保存用户信息到session中
* 重定向到MyJsp.jsp
* */
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("/Session/login/MyJsp.jsp");
}else{//不成功
/*
* 保存错误信息到request中
* 并转发到Login.jsp
* */
System.out.println("????????");
request.setAttribute("message", "用户名与密码不匹配");
RequestDispatcher qr = request.getRequestDispatcher("/login/Login.jsp");
qr.forward(request, response);
}
}
}
MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String username=(String)session.getAttribute("username");
if(username==null){
request.setAttribute("message", "请先登录");
RequestDispatcher rd=request.getRequestDispatcher("/login/Login.jsp");
rd.forward(request, response);
}
%>
<%=username %>登录成功。<a href="#">进入个人中心</a>
</body>
</html>
MyCentre.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyCentre.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
String username=(String)session.getAttribute("username");
if(username==null){
request.setAttribute("message", "请先登录");
RequestDispatcher rd=request.getRequestDispatcher("/login/Login.jsp");
rd.forward(request, response);
}
%>
<body>
This is my JSP page. <br>
</body>
</html>