最近在弄一个SSH项目,前期已经做好了,现在的需求是进行单点登陆实现,涉及到重定向跳转(带有参数那种)情况,但是不能在地址栏上出现参数的信息,需要进行参数的隐藏跳转。由于时间比较急,本人没来得及开发一个小工具,这次用的别人以前写好的工具类进行参数隐藏。放在这里好让自己积累一些工具类,也方便大家参考!好了,直接上代码:

package com.example.Utils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpClientPostFs {
Map<String, String> parameter=new HashMap<String, String>();
HttpServletResponse response;

public HttpClientPostFs()
{
}
public HttpClientPostFs(HttpServletResponse response)
{
this.response=response;
}
public void setParameter(String key, String value)
{
this.parameter.put(key, value);
}
public void sendByPost(String url) throws IOException
{
this.response.setContentType("text/html");
PrintWriter out = this.response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>sender</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<form name=\"submitForm\" action=\""+url+"\" method=\"post\">");
Iterator<String> it=this.parameter.keySet().iterator();
while(it.hasNext())
{
String key=it.next();
out.println("<input type=\"hidden\" name=\""+key+"\" value=\""+this.parameter.get(key)+"\"/>");
}
out.println("</from>");
out.println("<script>window.document.submitForm.submit();</script> ");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
上面的是一个工具类的所有内容,下面是调用实现参数隐藏跳转代码展示:
HttpClientPostFs http = new HttpClientPostFs(ServletActionContext.getResponse());
http.setParameter("usercode", "123456");//将参数封装到这个里面,以键值对的形式存在
http.setParameter("password", "123456");
http.sendByPost(url);//进行跳转
版权声明:本文为eagle-lin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/eagle-lin/p/9866481.html