Servlet中以HashMap存放临时变量,解决跳转新页面请求参数过多时浏览器地址栏超长
具体使用方法如下:
1、在跳转之前将需要的参数串encodeURIComponent后作为参数value,UUID作为key一起POST到Servlet保存到HashMap中;
2、在Servlet发POST接口返回true后将之前的UUID传递到新页面;
3、在新页面拿到UUID后调用POST接口请求上一个页面保存进HashMap中的参数串并做解析处理,根据实际情况斟酌使用decodeURIComponent;
注:该Servlet的GET接口返回当前HashMap中保存的参数键值对;
POST接口接受3个参数:operateType(必填)、key(必填)、value(新增时必填)
operateType参数取值为ADD、DELETE、QUERY、QUERYANDDELETE,分别为增、删、查、查并删(查询出来后立刻删除并返回)
package com.nihaorz.common.util; import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; public class ParamsPool extends HttpServlet { private Map<String, String> paramsMap = new HashMap<String, String>(); /**
* Constructor of the object.
*/
public ParamsPool() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>ParamsPool</TITLE><STYLE>");
out.println("TABLE{word-break: break-all;word-wrap: break-word;margin-right:20px;margin-bottom: 10px;} TR TH{padding: 5px 10px;} TR TD{padding: 5px 10px;}");
out.println("</STYLE></HEAD><BODY>");
out.print("<h4>参数池中包含【" + paramsMap.size() + "】个参数</h4>");
if (paramsMap.size() > 0) {
out.print("<TABLE border='1'>");
out.print("<TR><TH style='width: 50px;'>序号</TH><TH style='width: 100px;'>键</TH><TH style='width: 200px;'>值</TH></TR>");
Iterator<String> it = paramsMap.keySet().iterator();
int i = 1;
while (it.hasNext()) {
String key = it.next();
String val = paramsMap.get(key);
out.print("<TR><TD>" + (i++) + "</TD><TD>" + key + "</TD><TD>"
+ val + "</TD></TR>");
}
out.print("</TABLE>");
}
out.println("</BODY>");
out.println("</HTML>");
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
String operateType = request.getParameter("operateType");
String key = request.getParameter("key");
String value = request.getParameter("value");
System.out.println("operateType:" + operateType);
System.out.println("key:" + key);
System.out.println("value:" + value);
JSONObject jsonObject = new JSONObject();
jsonObject.put("result", false);
if (key != null && !key.equals("")) {
if ("ADD".equals(operateType)) {
paramsMap.put(key, value);
jsonObject.put("result", true);
} else if ("DELETE".equals(operateType)) {
paramsMap.remove(key);
jsonObject.put("result", true);
} else if ("QUERY".equals(operateType)) {
String result = paramsMap.get(key);
jsonObject.put("result", true);
jsonObject.put("data", result);
} else if ("QUERYANDDELETE".equals(operateType)) {
String result = paramsMap.get(key);
paramsMap.remove(key);
jsonObject.put("result", true);
jsonObject.put("data", result);
}
}
PrintWriter out = null;
try {
out = response.getWriter();
out.write(jsonObject.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
Servlet中以HashMap存放临时变量,解决跳转新页面请求参数过多时浏览器地址栏超长的更多相关文章
- selenium中webdriver跳转新页面后定位置新页面的两种方式
刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...
- 从上一个页面跳入新页面时,如何拿URL中的参数
var url = document.URL; //获取当前页面的url var urlA = url.split('?');//以url中的问号进行分割; var goodscode = urlA[ ...
- 重构改善既有代码设计--重构手法04:Replace Temp with Query (以查询取代临时变量)
所谓的以查询取代临时变量:就是当你的程序以一个临时变量保存某一个表达式的运算效果.将这个表达式提炼到一个独立函数中.将这个临时变量的所有引用点替换为对新函数的调用.此后,新函数就可以被其他函数调用. ...
- 临时变量不能作为非const引用
转自:http://blog.csdn.net/u011068702/article/details/64443949 1.看代码 2.编译结果 3.分析和解决 就拿f(a + b)来说,a+b的值会 ...
- SAS笔记(4) FIRST.和LAST.临时变量
FIRST.和LAST.临时变量是SAS很有特色的一点,我在R和Python中暂时没有发现类似的功能(也许它们也有这个功能,我不知道而已).考虑这样一种场景:我们有患者就诊的数据,每一条观测对应一个患 ...
- C++11引用临时变量的终极解析
工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解. 这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正. **** ...
- 重构手法之Replace Temp with Query(以查询取代临时变量)
返回总目录 6.4Replace Temp with Query(以查询取代临时变量) 概要 你的程序以一个临时变量保存某一表达式的运算结果. 将这个表达式提炼到一个独立函数中.将这个临时变量的所有引 ...
- servlet中的request和response
request对象 1.什么是请求 a.浏览器向服务器发送数据就是请求. 一.request功能1--获取数据 1.获取浏览器相关的信息 getRequestURL方法 -- 返回客户端发出请求完整U ...
- Azure Terraform(十一)Azure DevOps Pipeline 内的动态临时变量的使用
思路浅析 在我们分析的 Azure Terraform 系列文中有介绍到关于 Terraform 的状态文件远程存储的问题,我们在 Azure DevOps Pipeline 的 Task Job ...
随机推荐
- C#开发微信门户及应用(18)-微信企业号的通讯录管理开发之成员管理
在上篇随笔<C#开发微信门户及应用(17)-微信企业号的通讯录管理开发之部门管理>介绍了通讯录的部门的相关操作管理,通讯录管理包括部门管理.成员管理.标签管理三个部分,本篇主要介绍成员的管 ...
- html
有目标的学知识才行 要学习html语言,我突然发现我好像可以在markdown文本编辑器中编辑html标签,既然这样我就多玩玩.markdown完全兼容html,这真的是一个好事情.好像也有功能在ma ...
- java.lang.Class.isAssignableFrom()用法解析
一.概述: 此方法主要用来判断 "参数类" 是否是 "源类" 的子类.接口实现类,或者与 "源类" 相同,在此情况下返回 true; 二.格 ...
- 设计模式02迭代器(java)
先贴代码,有空来写内容. 1.定义集合 import java.util.List; import java.util.ArrayList; //coollection是我自己定义的一个集合,因为要写 ...
- 使用tomcat manager 管理和部署项目
在部署tomcat项目的时候,除了把war文件直接拷贝到tomcat的webapp目录下,还有一种方法可以浏览器中管理和部署项目,那就是使用tomcat manager. 默认情况下,tomcat m ...
- [FromBody]与[FromUrl]
我们都知道,前台请求后台控制的方法有get方法和post方法两种, get:只支持ulr传数据,不管你是手动把参数拼接在Url里面还是写在data里面,只要是用get方法,都会自动绑定到url里面的形 ...
- ng-option指令使用记录,设置默认值需要注意
ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...
- 批处理bat 命令
1.批处理常用符号: - echo 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置 语法:@echo [{ on|off }] echo{"显示 ...
- 树莓派:raspberry pi 3b - NOOBS
NOOBS - 多操作系统安装器,可以将不同支持树莓派的流行操作系统安装在一张SD卡中并提供一个启动管理工具,安装的不同操作系统相互独立,互不影响,是一种比较有意思的玩法. 从版本1.3.1开始到1. ...
- centos编译安装mysql
groupadd mysql #添加mysql组useradd -g mysql -s /sbin/nologin mysql #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系 ...