java修改request中的参数_修改Request中的数据
修改参数
今天⼀位⽹友开发中遇到⼀个需求,他需要在Request中修改传递过来的数据。开始的时候他在中修改,在中可以获取到从前台request中
传递过来的数据。他写法⼤致如下:⾃定义继承HandlerInterceptorAdapter
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//在这⾥request中获取参数并修改。
return true;java修改html文件
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
不过在controller 中获取在中修改的参数,发现参数还是以前⽼的数据,也就是说在中修改request中出传递过来的数据并没有⽣效。
数据没有⽣效 参考
于是我看了 HttpServletRequestWrapper 源码,在Filter中测试了,发现可以成功修改传递过来的参数
public class HTMLFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
FilteredRequest requestWrapper = new FilteredRequest((HttpServletRequest) request);
chain.doFilter(new FilteredRequest(request), response);
}
static class FilteredRequest extends HttpServletRequestWrapper {
public FilteredRequest(ServletRequest request) {
Controller中查看传递过来的数据
接下来我们分析下代码
Controller 中的 Parameter("paramName") 这个调⽤的是什么?我们看下源码发现调⽤的是package javax.servlet 包 下⾯的 ServletRequest 抽象接⼝ServletRequest 中的
public abstract String getParameter(String paramString);
package javax.servlet;
public abstract String getProtocol();
public abstract String getScheme();
public abstract String getServerName();
public abstract int getServerPort();
public abstract BufferedReader getReader() throws IOException;
public abstract String getRemoteAddr();
public abstract String getRemoteHost();
public abstract void setAttribute(String paramString, Object paramObject);
public abstract void removeAttribute(String paramString);
public abstract Locale getLocale();
public abstract Enumeration getLocales();
public abstract boolean isSecure();
public abstract RequestDispatcher getRequestDispatcher(String paramString);
/**
* @deprecated
*/
public abstract String getRealPath(String paramString);
public abstract int getRemotePort();
public abstract String getLocalName();
public abstract String getLocalAddr();
public abstract int getLocalPort();
public abstract ServletContext getServletContext();
public abstract AsyncContext startAsync() throws IllegalStateException;
public abstract AsyncContext startAsync(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws IllegalStateException;
public abstract boolean isAsyncStarted();
public abstract boolean isAsyncSupported();
public abstract AsyncContext getAsyncContext();
public abstract DispatcherType getDispatcherType();
}
接下来我们看下Filter的实现类,
public String getParameter(String paramName) {
String value = Parameter(paramName);
return value + "modify";
}
我们着重看Parameter 这个⽅法,我们点进去看源码发现的是 ServletRequestWrapper 类下⾯的
public String getParameter(String name)
{
Parameter(name);
}
然⽽ public class ServletRequestWrapper implements ServletRequest
综上所述源头来源于 ServletRequest 我们在Controller 中的 Parameter("paramName") 最后就是的 ServletRequest 我们来看看HttpServletRequestWrapper 类
public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest
{
public HttpServletRequestWrapper(HttpServletRequest request)
{
super(request);
}
private HttpServletRequest _getHttpServletRequest() {
return (Request();
}
public String getAuthType()
{
return _getHttpServletRequest().getAuthType();
}
public Cookie[] getCookies()
{
return _getHttpServletRequest().getCookies();
}
public long getDateHeader(String name)
{
return _getHttpServletRequest().getDateHeader(name);
}
public String getHeader(String name)
{
return _getHttpServletRequest().getHeader(name);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。