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 } }