Servlet中以HashMap存放临时变量,解决跳转新页面请求参数过多时浏览器地址栏超长
2024-10-13 14:32:06
具体使用方法如下:
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
} }
最新文章
- NOPI导出加载模板
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)
- 国内常用NTP服务器地址及IP
- c#判断特殊字符?
- (ORA-12899) 10g数据库导入到11g数据库时报错
- Case When Exists SQL
- 图解iPhone开发新手教程
- 关于textView的字数限制
- Qt 小技巧之&ldquo;To-Do 事项&rdquo;
- EcShop后台添加菜单[步骤]
- php 汉字转拼音 [包含20902个基本汉字+5059生僻字]
- php给数组key:value中的key加引号
- Django REST FrameWork中文教程3:基于类的视图
- mysql单独可连接,php连接mysql失败之 Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)
- C# Bitmap生成base64码
- Exp5 MSF基础应用 20164320 王浩
- Asp.Net中对操作Sql Server 简单处理的SqlDB类
- java 虚拟机栈
- openstack之cinder_backup对接ceph存储
- 6.29一个_rcv 面试题