前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >讲讲session是怎么工作的,session的用法?

讲讲session是怎么工作的,session的用法?

作者头像
马克java社区
修改2021-07-07 10:25:26
6300
修改2021-07-07 10:25:26
举报
文章被收录于专栏:java大数据

6.session的用法

马克-to-win:到现在为止,我们学会了一次单独的请求和响应之间传递参数。但是如何跨越几次请求响应之间传递参数呢?比如我以马克的身份登录,这是 一次请求响应。之后买书又是一次请求响应。如何买书的时候还记得买书的人是马克,而不是张三呢?马克这个参数存在哪呢?这是跨越两次访问。Sun公司为我 们提供了HttpSession这个接口。HttpSession session = request.getSession();通过这句话,你可以得到一个与你的浏览器绑定的session对象,存在Tomcat里。这 个session对象只认你这个浏览器,之后只要是你这个浏览器发出的请求,无论跨越多少次请求响应,这个session对象就对它开放,其它浏览器不能 访问。通过session.setAttribute()可以往session里面存值,session.getAttribute可以取值。问题是 session是如何识别你的浏览器呢?初学者可忽略:靠Cookie或者URL改写:如果浏览器支持Cookie,则使用Cookie;如果浏览器不支持Cookie或者Cookie功能被关闭,则自动使用URL改写方法。拿cookie来说(通常客户很少见关闭cookie,即使你关了,我也可以发现,之后提醒你打开或编程序重写URL),服务器往客户端写东西时,cookie会带上sessionid。当客户端再次访问服务器时,同一path下,会自动在html请求头中带上cookie信息,服务器可以在_COOKIE域中得取到想要的sessionid。

有时我们在网络购物时,如果有一段时间没有碰电脑,当我们再继续购物时,会接到session过期的错误信息。这是因为任何session对象,天生就有能过期的特性。我们可以通过类的方法改变失效时长。

根据刚才的场景,我做了一个例子。由两支组成,每一支都由一个html和一个Servlet组成。客户姓名由register.html提交给 MarkToWinServletHello1。之后MarkToWinServletHello1把姓名存在session当中。之后用 response.sendRedirect("buy.html");做了一个自动跳转到buy.html。之后开始买东西。 【新手可忽略】sendRedirect的实现方法是通过修改回写回客户端的html网页的HTTP协议的HEADER部分,(比如response.setHeader("Location", "NewURL");)对浏览器下达重定向指令的,让浏览器对在location中指定的URL提出请求,使浏览器显示重定向网页的内容。

例:6.1

register.html:

<html>

<body>

<FORM ACTION="MarkToWinServletHello1"

METHOD="POST">

First Name:

<INPUT TYPE="TEXT" NAME="firstName"><BR>

<INPUT TYPE="SUBMIT" VALUE="Submit Order">

</CENTER>

</FORM>

</body>

</html>

ServletHello1.java 

package com;

import java.io.IOException;

import java.io.PrintWriter;

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 ServletHello1 extends HttpServlet {

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

String fn=request.getParameter("firstName");

/*true means whether right now it has session or not, if it does not have, it will create a new one,if it has already, it use the current one.*/

HttpSession session = request.getSession(true);

/*将客户姓名存入服务器的session中*/

session.setAttribute("name", fn);

response.sendRedirect("buy.html");

}

}

buy.html:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<FORM ACTION="Buy" METHOD="POST">

What to buy

<INPUT TYPE="TEXT" NAME="thing"><BR>

<INPUT TYPE="SUBMIT" VALUE="Submit Order">

</CENTER>

</FORM>

</body>

</html>

Buy.java:

package com;

import java.io.IOException;

import java.io.PrintWriter;

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 Buy extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String thi=request.getParameter("thing");

/*true means whether right now it has session or not, if it does not have, it will create a new one,

if it has already, it use the current one.*/

HttpSession session = request.getSession(true);

/*将客户姓名,从服务器的session中取出来*/

String nam=(String) session.getAttribute("name");

PrintWriter pw=response.getWriter();

pw.println(nam+" want to buy "+thi);

}

}

更多请看:https://blog.csdn.net/qq_44594371/article/details/103155812

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档