具体使用方法如下:

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. HTML课上小结

    HTML翻译为超文本标记语言<标签名>内容</标签名>静态网页动态网页的区别是看是否从数据中提取数据一般网页由几部分组分组成<html>开始标签 <head& ...

  2. 图表插件Charts.js的使用

    Charts.js的介绍自行百度 首先下载Charts.js,官网:http://chartjs.cn/ charts.js 托管在了github上,下载下来后加解压出src中的文件即可.其中有cha ...

  3. C#计算代码行数

    class Program { static void Main(string[] args) { int totalLineCount = 0; string directory; if(args. ...

  4. css3+jquery制作3d旋转相册

    首先来看一下今天的炫酷效果: 首先分析一下这张图片: 1.每张图片都有倒影 2.这11张图片呈圆形均匀排列 3.可旋转,上下移动(当然这是效果做出来以后,图片是分析不出来的) 那下面就开始吧. 一.准 ...

  5. OC多态

    要点: 1.多种形态,引用的多种形态对于一个引用变量,可以指向任何类的对象.对于一个父类的引用(类与类之间有一种继承关系),可以指向子类,也可以指向本类,指向的类型不同.当通过此引用向对象发送消息,调 ...

  6. low security dvwa--SQL Injection(Blind)

    1.输入单引号,结果如下: 2.输入永真式 ' and 1=1; -- 结果如下: 多次测试,如果输入的条件为假,就会返回1中的结果,为真则返回2中的结果,由此说明这属于SQL盲注. 3.猜解用户名长 ...

  7. Linux Standards Base LSB

    LSB简介 http://www.ibm.com/developerworks/cn/linux/l-lsb-intr/ http://refspecs.linuxbase.org/ http://t ...

  8. python进行mp3格式判断

    python进行mp3格式判断 项目中使用mp3格式进行音效播放,遇到一个mp3文件在程序中死活播不出声音,最后发现它是wav格式的文件,却以mp3结尾.要对资源进行mp3格式判断,那么如何判断呢,用 ...

  9. UITableview delegate dataSource调用探究

    UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...

  10. 多War项目中静态文件的共享方案

    [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 在互联网产品中,一般会有多个项目(Jar.WAR)组成一个产品线.这些WAR项目,因为使用相同的前端架构(jQuery.easyui等) ...