java修改请求参数的值
1个回答
2016-11-16 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
网上查了一些资料,解决方案是使用Wrapper,重写HttpServletRequestWrapper,如下:
package com.gdcn.bpaf.security.yale;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
@SuppressWarnings("unchecked")
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map params;
public ParameterRequestWrapper(HttpServletRequest request, Map newParams) {
super(request);
this.params = newParams;
}
public Map getParameterMap() {
return params;
}
public Enumeration getParameterNames() {
Vector l = new Vector(params.keySet());
return l.elements();
}
public String[] getParameterValues(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
return (String[]) v;
} else if (v instanceof String) {
return new String[] { (String) v };
} else {
return new String[] { v.toString() };
}
}
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else if (v instanceof String) {
return (String) v;
} else {
return v.toString();
}
}
}
�0�22.使用
//然后在另一个地方使用时,按下面这样用,假如我要修改username的值
HashMap m=new HashMap(request.getParameterMap());
/*
关键部分,如果不转换成String数组,如果在下面这样是有时:
eg:
HttpSession session = request.getSession();
Map map = request.getParameterMap();
Set keySet = map.keySet();
for (Iterator iterator = keySet.iterator(); iterator.hasNext();)
{
String key = (String)iterator.next();
String values[] = (String[])map.get(key); //这个地方就会报错,因为username一个String不能转换成String数组
if (values.length == 1)
session.setAttribute(key, values[0]);
else
session.setAttribute(key, values);
}
异常
就会出现java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
package com.gdcn.bpaf.security.yale;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
@SuppressWarnings("unchecked")
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map params;
public ParameterRequestWrapper(HttpServletRequest request, Map newParams) {
super(request);
this.params = newParams;
}
public Map getParameterMap() {
return params;
}
public Enumeration getParameterNames() {
Vector l = new Vector(params.keySet());
return l.elements();
}
public String[] getParameterValues(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
return (String[]) v;
} else if (v instanceof String) {
return new String[] { (String) v };
} else {
return new String[] { v.toString() };
}
}
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else if (v instanceof String) {
return (String) v;
} else {
return v.toString();
}
}
}
�0�22.使用
//然后在另一个地方使用时,按下面这样用,假如我要修改username的值
HashMap m=new HashMap(request.getParameterMap());
/*
关键部分,如果不转换成String数组,如果在下面这样是有时:
eg:
HttpSession session = request.getSession();
Map map = request.getParameterMap();
Set keySet = map.keySet();
for (Iterator iterator = keySet.iterator(); iterator.hasNext();)
{
String key = (String)iterator.next();
String values[] = (String[])map.get(key); //这个地方就会报错,因为username一个String不能转换成String数组
if (values.length == 1)
session.setAttribute(key, values[0]);
else
session.setAttribute(key, values);
}
异常
就会出现java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询