seession是一个为了保存用户状态而创建的一个特殊的对象
其本质上相当于Map,里面可以存放多个键值对,是以key-value进行存放的(key必须是一个字符串,value是一个对象)
该对象主要保持相应数据的唯一性(一般用于存储主键)
session的常用方法
1、获取session对象的方法------request.getSession();
2、存放相关属性的方法------session.setAttribute(String s,Object o);
3、从session中获取存放的属性方法------session.getAttribute(String s);
4、删除session属性的方法------session.removeAttribute(String s);
5、得到session所对应的id------session.getId();
6、使得session立即无效------session.invalidate();
7、设置session存活的最长时间------session.setMaxInactiveInterval(int i);
下面来一个Session实例帮助理解:
//mainServlet.java
package com.example.servlet;
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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/mainServlet")
public class mainServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
HttpSession session=req.getSession();
Object username=session.getAttribute("username");
PrintWriter pw=resp.getWriter();
if(username!=null){
pw.write("登录成功!");
}else{
pw.write("登录失败!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
<!--index.jsp-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<center>
<form action="Session" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" placeholder="请输入用户名"></td>
</tr>
<p>
<tr>
<td>密码:</td>
<td><input type="text" name="password" placeholder="请输入密码"></td>
</tr>
<p>
<tr>
<button type="submit" value="提交">提交</button>
</tr>
</form>
</center>
//Session.java
package com.example.mapper;
import com.example.pojo.User;
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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/Session")
public class Session extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
if(username.equals("tom")&&password.equals("tom123")){
HttpSession session=req.getSession();
session.setMaxInactiveInterval(60*60*24);
session.setAttribute("username",username);
resp.sendRedirect(req.getContextPath()+"/mainServlet");
}else{
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
pw.write("账号或密码错误!");
pw.write("<a href='"+req.getContextPath()+"/index.jsp'></a>");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
session用来判断啥的还是很好用的!
与Cookie的比较
可以看出来,与Cookie相比较的话,Session更具有安全性,范围更广,可以在不同页面共享数据,使用更为方便;
但是有时在使用Session时,也需要Cookie的配合的,每一种语言都有自己的好处的嗷!
标签:resp,req,用法,session,import,servlet,javax From: https://www.cnblogs.com/liuzijin/p/16820233.html