url override and HttpSession implements session for real
package com.test; 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 java.io.IOException;
import java.io.PrintWriter; @WebServlet("/page1")
public class Page1 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>问卷调查</title>");
out.println("</head>");
out.println("<body>"); String url = response.encodeURL("page2");
out.println("<form action=\"" + url + "\" method=\"get\">");
out.println("問題一:<input type='text' name='p1q1'><br>");
out.println("問題二:<input type='text' name='p1q2'><br>");
out.println("<input type='submit' name='page' value='提交'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
package com.test; 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("/page2")
public class Page2 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession(); PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>问卷调查</title>");
out.println("</head>");
out.println("<body>"); String url = response.encodeURL("page3");
out.println("<form action=\"" + url + "\" method=\"get\">");
System.out.println("第一个sessionId: " + session.getId());
session.setAttribute("p1q1", request.getParameter("p1q1"));
session.setAttribute("p1q2", request.getParameter("p1q2"));
out.println("問題三:<input type='text' name='p2q1'> <br>");
out.println("<input type='submit' name='page' value='提交'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
package com.test; 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("/page3")
public class Page3 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession(); PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>问卷调查</title>");
out.println("</head>");
out.println("<body>"); System.out.println("第二个sessionId: " + session.getId());
out.println(session.getAttribute("p1q1") + "<br>");
out.println(session.getAttribute("p1q2") + "<br>");
out.println(request.getParameter("p2q1") + "<br>"); out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
测试地址
127.0.0.1:8080/page1
url override and HttpSession implements session for real的更多相关文章
- url override and HttpSession implements session for form
url 重写结合HttpSession实现会话管理之 form 提交 package com.test; import javax.servlet.ServletException; import j ...
- url override and HttpSession implements session
背景 HttpSession默认使用Cookie存储Session ID,如果在用户禁用浏览器Cookie的功能后,仍打算运用HttpSession来进行会话管理,那么可以搭配URL重写来实现. 实现 ...
- HttpSession implements session
体验 使用HttpSession进行会话管理,完全可以忽略HTTP无状态的事实. HttpSession会话管理原理 使用HttpSession进行会话管理十分方便,让Web应用程序看似可以“记得”浏 ...
- url override implements session
url 重写实现会话概述 URL重写是对GET请求参数的应用,当服务器响应浏览器上一次请求时,将某些相关信息以超链接方式响应给浏览器,超链接中包括请求参数信息. 特点:URL必须以GET方式发送请求. ...
- 隐藏表单域、URL重写、cookie、session
隐藏表单域: 隐藏域是用来收集或发送信息的不可见元素,对于网页的访问者来说,隐藏域是看不见的.当表单被提交时,隐藏域就会将信息用你设置时定义的名称和值发送到服务器上. 代码格式:<input t ...
- Django学习笔记之URL与视图cookie和session
cookie和session cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了 ...
- hidden field implements session
隐藏域实现会话管理概述 如果你正在制作一个网络问卷,由于问卷内容很长,因此必须分几个页面,上一页面作答完后,必须请求服务器显示下一个页面. 但是在HTTP协议中,服务器并不会记得上一次请求的状态.既然 ...
- cookie implements session
cookie实现会话 服务器调用response.addCookie()设置set-cookie响应头后,浏览器收到这个响应头与数值后,会将它以文件的形式存储于本地PC上.当浏览器再次访问同一Web服 ...
- session 机制和 httpsession 详解 (转载)
https://www.cnblogs.com/bjanzhuo/archive/2013/02/27/3575884.html 一.术语session 在我的经验里,session这个词被滥用的程度 ...
随机推荐
- Python多线程-Barrier(障碍对象)
Barrier(parties, action=None, timeout=None) 每个线程通过调用wait()尝试通过障碍,并阻塞,直到阻塞的数量达到parties时,阻塞的线程被同时全部释放. ...
- ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: Cannot run program "hive": error=2, No such file or directory
原因是hive没有设置环境变量 1,vim /etc/profile (切换root用户) 2.source /etc/profile
- mpvue图片轮播遇到的问题
小程序官方写法: <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" i ...
- Go常量与运算符
常量的定义 常量的值在编译时就已经确定 常量的定义格式与变量基本相同 等号右侧必须是常量或者常量表达式 常量表达式中的函数必须是内置函数 package main import ( "fmt ...
- 微服务之kong+consul(二)
一.kong 1.使用kong来做服务网关,目前kong使用的是0.13版本现在地址:https://docs.konghq.com/install,kong的社区版没有dashboard,可以使用k ...
- ElasticSearch 学习记录之Text keyword 两种基本类型区别
ElasticSearch 系列文章 1 ES 入门之一 安装ElasticSearcha 2 ES 记录之如何创建一个索引映射 3 ElasticSearch 学习记录之Text keyword 两 ...
- JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- C#基础知识回顾--串行化与反串行化
串行化是指存储和获取磁盘文件.内存或其他地方中的对象.在串行化时,所有的实例数据都保存到存储介质上, 在取消串行化时,对象会被还原,且不能与其原实例区别开来.只需给类添加Serializable属性, ...
- java中那些类是线程安全的?
Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...
- Idea的Maven项目引入模块
File->Project Structures->Modules 点击那个加号,选择Import Module