www.rubbishsort.com 欢迎访问公益网站垃圾分类网
request中的setCharacterEncoding方法作用是用指定的编码集去覆盖request对象中的默认的"ISO-8859-1"编码集,这样request.getParameter("")方法就会用新的编码集去解码,但是这个方法有一个条件,就是必须在第一次使用request时就要调用这个方法来设置编码集,否则该方法就会无效。
request.setCharacterEncoding("utf-8"); 这句话只是针对String username = request.getParameter("username"); 这句话生效的,不是对其他的生效
错误示例:
<%
if(request.getParameter("function").equals("validation")){
request.setCharacterEncoding("utf-8");
userName = request.getParameter("userName");
}
%>
<%= userName%>
若userName是获取中文的话,<%=userName%>输出的依然是乱码,即request的setCharacterEncoding方法没有起到作用。因为request在if的判断条件中就被使用了,后面的setCharacterEncoding方法都会失效。正确的方法应该如下:
这样userName获取中文就不会是乱码了
<%
request.setCharacterEncoding("utf-8");
if(request.getParameter("function").equals("validation")){
userName = request.getParameter("userName");
}
%>
<%= userName%>
servlet:
POST:request.setCharacterEncoding("utf-8");(必须有)
response.setContentType("text/html;charset=utf-8");(必须有)
GET:
response.setContentType("text/html;charset=utf-8");(必须有)
String username = request.getParameter(“username”); (默认使用的是ISO-8859-1)
username = new String(username.getBytes(“ISO-8859-1”), “utf-8”);
//解决方案就是解了重编 不用这个了request.setCharacterEncoding("utf-8");
举例子:
package cn.itcast.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
response.getWriter().println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
response.getWriter().println(username);
}
}
request.setCharacterEncoding("utf-8"); 这句话只是针对String username = request.getParameter("username"); 这句话生效的,不是对其他的生效
配置一样的但是只是对post 生效,对get不生效
get 请求
如果相对doget 也修正的的话
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1"), "utf-8");
response.getWriter().println(username);
}
如果采用过滤器的方式一起都修正了
点击这里
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
response.getWriter().println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
response.getWriter().println(username);
}
}
package cn.itcast.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 处理post请求编码问题
request.setCharacterEncoding("utf-8");
HttpServletRequest req = (HttpServletRequest) request;
/*
* 处理GET请求的编码问题(不能用这个)
*/
// String username = request.getParameter("username");
// username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
/*
* 调包request
* 1. 写一个request的装饰类
* 2. 在放行时,使用我们自己的request
*/
if(req.getMethod().equals("GET")) {
//由于这里是对request。getParameter() 方法进行的方法进行重写,写一个request 的装饰类EncodingRequest
EncodingRequest er = new EncodingRequest(req);
//相当于调用的是AServlet中的service方法,所以dofilter方法中的request,
//response参数就是相当于AServlet 中的request,response
chain.doFilter(er, response);
} else if(req.getMethod().equals("POST")) {
//相当于调用的是AServlet中的service方法,所以dofilter方法中的request,
//response参数就是相当于AServlet 中的request,response
chain.doFilter(request, response);
}
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
AServlet
cn.itcast.web.servlet.AServlet
AServlet
/AServlet
index.jsp
EncodingFilter
EncodingFilter
cn.itcast.web.filter.EncodingFilter
EncodingFilter
/*
写一个装饰类
由于是对对象的增强那么不可以直接继承request 对象吗
可以知道request 的对象是org.apache.catalina.connector.RequestFacade
那么在装饰兑现的时候可以不可以继承这个对象呢,不可以,
原因是因为这个request 对象是org.apache.catalina.connector.RequestFacade 显而易见是apache 对象提供的,那么如果以后换了一个服务器种类不是apache 换成weblogic 的话,就不行了,所以只能是继承HttpRequest ,但是这个HttpServletRequest是一个接口,所以还有实现其中的所有的方法,在javaweb 中有一个专门用来调包用的,因为这个是一个类,HttpServletRequestWrapper这个类已经实现了HttpServletRequest
一开始就是这样的(对象的增强)
public class EncodingRequest implements HttpServletRequest {
private HttpServletRequest request;
public EncodingRequest(HttpServletRequest request) {
super();
this.request = request;
}
public AsyncContext getAsyncContext() {
return request.getAsyncContext();
}
public Object getAttribute(String arg0) {
return request.getAttribute(arg0);
}
实际上是这样的
package cn.itcast.web.filter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 装饰reqeust
* @author cxf
*
*/
public class EncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest req;
public EncodingRequest(HttpServletRequest request) {
super(request);
this.req = request;
}
public String getParameter(String name) {
String value = req.getParameter(name);
// 处理编码问题
try {
value = new String(value.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return value;
}
}