具体使用方法如下:

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存放临时变量,解决跳转新页面请求参数过多时浏览器地址栏超长的更多相关文章

  1. selenium中webdriver跳转新页面后定位置新页面的两种方式

    刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...

  2. 从上一个页面跳入新页面时,如何拿URL中的参数

    var url = document.URL; //获取当前页面的url var urlA = url.split('?');//以url中的问号进行分割; var goodscode = urlA[ ...

  3. 重构改善既有代码设计--重构手法04:Replace Temp with Query (以查询取代临时变量)

    所谓的以查询取代临时变量:就是当你的程序以一个临时变量保存某一个表达式的运算效果.将这个表达式提炼到一个独立函数中.将这个临时变量的所有引用点替换为对新函数的调用.此后,新函数就可以被其他函数调用. ...

  4. 临时变量不能作为非const引用

    转自:http://blog.csdn.net/u011068702/article/details/64443949 1.看代码 2.编译结果 3.分析和解决 就拿f(a + b)来说,a+b的值会 ...

  5. SAS笔记(4) FIRST.和LAST.临时变量

    FIRST.和LAST.临时变量是SAS很有特色的一点,我在R和Python中暂时没有发现类似的功能(也许它们也有这个功能,我不知道而已).考虑这样一种场景:我们有患者就诊的数据,每一条观测对应一个患 ...

  6. C++11引用临时变量的终极解析

    工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解. 这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正.   **** ...

  7. 重构手法之Replace Temp with Query(以查询取代临时变量)

    返回总目录 6.4Replace Temp with Query(以查询取代临时变量) 概要 你的程序以一个临时变量保存某一表达式的运算结果. 将这个表达式提炼到一个独立函数中.将这个临时变量的所有引 ...

  8. servlet中的request和response

    request对象 1.什么是请求 a.浏览器向服务器发送数据就是请求. 一.request功能1--获取数据 1.获取浏览器相关的信息 getRequestURL方法 -- 返回客户端发出请求完整U ...

  9. Azure Terraform(十一)Azure DevOps Pipeline 内的动态临时变量的使用

    思路浅析 在我们分析的 Azure Terraform 系列文中有介绍到关于 Terraform 的状态文件远程存储的问题,我们在  Azure DevOps Pipeline 的 Task Job ...

随机推荐

  1. js正则表达式整理

    一.数字类 数字:^[0-9]*$ 正数.负数.和小数:^(\-|\+)?\d+(\.\d+)?$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ 非零开头的最多带两位小数的数字:^([1-9 ...

  2. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  3. Linux(十)___iptables防火墙

    一.防火墙的作用 三.防火墙的分类 三.iptables基本语法: 表: 常用filter,nat用于地址映射转换. 配置文件: /etc/sysconfig/iptables 过滤表信息 . 查看i ...

  4. 常见容易遗漏的html标签

    <link href="favicon.ico" mce_href="/favicon.ico" rel="bookmark" typ ...

  5. 利用CSS背景颜色属性使父级div背景透明同时避免子级标签透明。

    实现背景色透明效果的代码 实现各个浏览器中具备良好的透明特性的效果,IE中使用私有滤镜filter,高端浏览器使用CSS3中的rgba属性. 输入十六进制的颜色值以及透明度,自动在IE的过渡滤镜以及C ...

  6. 移动站适配rel=alternate PC页和H5页适配标注

    鉴于移动化大潮的汹涌和H5页的炫丽普及,百度针对PC页与H5页的跳转适配方式推出了最优方案:1.在pc版网页上,添加指向对应移动版网址的特殊链接rel="alternate"标记, ...

  7. javascript-组合模式

    组合模式笔记 组合模式又称部分-整体模式,将对象组合成树形结构以表示'部分整体'的层次结构 组合模式使得用户对单个对象和组合对象的使用具有一致性 demo实例 :表单模块 要调用到前面学习到的寄生组合 ...

  8. MySQL解压版配置步骤

    mysql-5.7.14-winx64\bin配置到Path中 在解压路径下复制my-default.ini,修改名称为my.ini 在my.ini添加如下 [mysqld] basedir=C:\\ ...

  9. 总结Cnblogs支持的常用Markdown语法

    一.什么是Markdown Markdown是一种可以使用普通文本编辑器编写的标记语言, Markdown的语法简洁明了.学习容易,而且功能比纯文本更强,因此有很多人用它写博客.世界上最流行的博客平台 ...

  10. Couchbase 环境搭建与使用(C#)

    Couchbase Couchbase Server (前身是 Membase) 是一个分布式的面向文档的 NoSQL 数据库管理系统,该系统联合了 CouchDB 的简单和可靠以及 Memcache ...