本文主要是介绍javaweb 解决全站字符编码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先创建一个javaweb的项目,在index.jsp里面修改如下代码.
<body><a href="<c:url value='/AServlet?username=董老师你好呀!'/>">GET</a><form action="<c:url value='/AServlet'/>" method="post">用户名:<input type="text" name="username"/><input type="submit" name="提交"/></form></body>
在浏览器打开如图所示,点击提交按钮就是发送的post请求,点击GET是提交get请求.
然后创建一个AServlet实现doGet和doPost方法.
package cn.com.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 {String username = request.getParameter("username"); //获取username并打印response.getWriter().print("GET-->username: " + username); }public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String username = request.getParameter("username");response.getWriter().print("POST-->username: " + username);}
}
此时若去提交表单会出现字符编码的问题,会出现提交中文时出现乱码,打印出乱码.
然后我们创建一个CharSetFilter的过滤器, /* 处理编码全部过滤.
CharSetFilter的代码实现.
package cn.com.servlet;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 CharSetFilter implements Filter {public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding("utf-8"); //只处理了post请求response.setContentType("text/html;charset=utf-8"); //处理响应的编码chain.doFilter(req, response); //放行}public void init(FilterConfig fConfig) throws ServletException {}
}
至此post请求的编码解决了.
下面处理get的编码请求.
创建一个CharSetRequest的类并继承HttpServletRequest这个接口.
CharSetRequest类的代码如下,CharsetRequest继承HttpServletRequest接口的类创建好后代码会很多,我们修改它的接口,改成装饰者模式,就是继承一个HTTPServletRequestWrapper,从而实现装饰者模式,这个类会帮我们完成很多底层的方法,我们只需要给它传递一些参数即可.
然后通过增强getParameter来实现get的编码问题.
package cn.com.servlet;import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;public class CharSetRequest extends HttpServletRequestWrapper {public CharSetRequest(HttpServletRequest request){super(request); //调用父类的构造器,给父类传递底层对象}/** 增强getParameter()方法* (non-Javadoc)* @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)*/@Overridepublic String getParameter(String name) {String value = super.getParameter(name);//判断当前的请求方式,如果为GET//需要回退,再重解 if (getMethod().equalsIgnoreCase("GET")) {try {value = new String(value.getBytes("iso-8859-1"),"utf-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return value;}
}
最后修改CharSetFilter的过滤器的代码,把request做为底层对象传过去,放行时调包处理.
//创建山寨版的request
通过CharSetRequest req = new CharSetRequest((HttpServletRequest)request);
package cn.com.servlet;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 CharSetFilter implements Filter {public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding("utf-8"); //只处理了post请求response.setContentType("text/html;charset=utf-8"); //处理响应的编码//用来处理get请求//创建山寨版的requestCharSetRequest req = new CharSetRequest((HttpServletRequest)request);//放行时将request改为req,调包处理.chain.doFilter(req, response);}public void init(FilterConfig fConfig) throws ServletException {}
}
至此get的编码处理完成了.
这篇关于javaweb 解决全站字符编码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!