🥼JavaWeb~(叁)Cookie/Session验证,JSP开篇
笔记代码
Servlet
HttpServletRequest
例子: 在 index.jsp 提交表单信息给
/login
,此 Servlet 后端读取发送来的表单信息并在页面输出显示.删掉并用 idea 重建 index.jsp,会补齐一些编码信息.
<%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>登录</h1>
<div style="text-align: center">
<form action="/login" method="post">
<label>
用户名:
<input type="text" name="username" placeholder="无"/><br>
</label>
<label>
密码:
<input type="password" name="password" placeholder=""/><br>
</label>
爱好:
<label><input type="checkbox" name="hobbys" value="女孩"/>女孩</label>
<label><input type="checkbox" name="hobbys" value="代码"/>代码</label>
<label><input type="checkbox" name="hobbys" value="动漫"/>动漫</label>
<label><input type="checkbox" name="hobbys" value="美食"/>美食</label><br>
<input type="submit">
</form>
</div>
</body>
</html>src/main/java/servlet/Login.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -7958099474557186407L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf8");
PrintWriter writer = resp.getWriter();
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbys = req.getParameterValues("hobbys");
writer.println("username: " + username);
writer.println("password: " + password);
writer.println("爱好: " + Arrays.toString(hobbys));
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>servlet.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Cookie、Session
Cookie 是服务端在客户端上做的标记,用请求和响应进行传递,用来记录用户的访问信息 (客户端技术)
session 是在服务端上保存用户的访问信息 (服务端技术)
Cookie
例子: 访问
/cookie
时,会显示上一次访问此 URL 的时间 (这个时间通过服务端传给客户端 Cookie 来记录)src/main/java/servlet/HelloCookie.java
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloCookie extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// 获取客户端请求中携带的cookie
Cookie[] cookies = req.getCookies();
boolean isFirst = true; // 标志是否是第一次访问
writer.println("你上一次访问时间是: ");
for (Cookie cookie : cookies) {
if (cookie.getName().equals("LastLoginTime")) {
isFirst = false;
writer.println(cookie.getValue());
}
}
if (isFirst) {
writer.println("这次是第一次访问.");
}
// 响应携带cookie返回给客户端
Cookie lastLoginTime = new Cookie("LastLoginTime", System.currentTimeMillis() + "");// 非字符串+"" = 字符串
resp.addCookie(lastLoginTime);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>cookie</servlet-name>
<servlet-class>servlet.HelloCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>
</web-app>
常用方法
Cookie[] cookies = req.getCookies(); //获得Cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie一些细节
cookie 大小和数量
- 不同浏览器支持的大小和数量不一样,大小都是 4K 左右,数量为每域名几十到无数个不等.
设置存活期:
不设置: 关闭浏览器自动失效
设置为 0,立即失效.
遇到传输的中文乱码问题,可以用 URLEncoder 编码
URLEncoder.encode("齐下无贰","utf-8")
URLDecoder.decode(cookie.getValue(),"UTF-8")
Session
cookie 用来传输一些字符串验证信息,并存放在客户端中.
而 session 可以存放更宽泛的数据(对象,文件等等),其数据存放在服务端.
服务端传给客户端一个名为
JSESSIONID
的 cookie 用来验证 session一个 Seesion 对象独占一个浏览器,不同浏览器有不同的 session
例子: 开启服务器并访问第一个页面
localhost:8080
时,session 就被创建了,然后访问/setSession
设置 person 信息,再访问/getSession
会显示 person 信息.src/main/java/servlet/SetSession.java
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class SetSession extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
//得到Session和writer
HttpSession session = req.getSession();
PrintWriter writer = resp.getWriter();
//给Session中存东西
session.setAttribute("person", new Person("齐下无贰", 100));
//获取Session的ID
String sessionId = session.getId();
//判断Session是不是新创建
if (session.isNew()) {
writer.println("session创建成功,ID: " + sessionId);
} else {
writer.println("session已经在服务器中存在了,ID: " + sessionId);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
src/main/java/servlet/GetSession.java
import javax.servlet.ServletException;
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;
public class GetSession extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4606977948300644893L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
//得到Session和writer
HttpSession session = req.getSession();
PrintWriter writer = resp.getWriter();
// 获取属性并输出
Person person = (Person) session.getAttribute("person");
writer.println(person);
// 移除属性
session.removeAttribute("name");
//手动注销Session
session.invalidate();
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>setSession</servlet-name>
<servlet-class>servlet.SetSession</servlet-class>
</servlet>
<servlet>
<servlet-name>getSession</servlet-name>
<servlet-class>servlet.GetSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setSession</servlet-name>
<url-pattern>/setSession</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>getSession</servlet-name>
<url-pattern>/getSession</url-pattern>
</servlet-mapping>
</web-app>
session 有效期问题
默认失效时间是 30 分钟
是从 session 断开连接(浏览器关闭开始计算的),而不是从 session 连接开始计算
可以在 web.xml 中设置
<session-config>
<session-timeout>1</session-timeout>
</session-config>
JSP
介绍
JSP (Java Server Pages),写法类似 HTML 的动态 Java 页面
最终 JSP 会被转换为一个 Java 类,实际上本质就是
Servlet 的简化实现
.*.jsp -> *.java -> *.class
jsp 转换为的 java 文件核心部分:
//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//JSPService
public void _jspService(HttpServletRequest request,HttpServletResponse response)JSPService 内初始化的对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application; //Context->application
final javax.servlet.ServletConfig config; //config
javax.servlet.jsp.JspWriter out = null; //writer->out
final java.lang.Object page = this; //当前页面->page
HttpServletRequest request //请求
HttpServletResponse response //响应
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;JSP 中可以写 Java 代码,Java 代码会原封不动的正常执行
- HTML 代码在 JSP->Java 时会被转换成
out.write("<html>\r\n");
这种形式.
- HTML 代码在 JSP->Java 时会被转换成
依赖导入
<!-- servlet依赖 --> |
基本语法
<%@ page import="java.util.Date" %> |