两个小测试而已。 1、jsp页面webtest.jsp的代码如下: <html> <head> <title></title> </head> <body> <form name="" action="GetUserNameServlet" method="POST"> <input type="text" name="username"/> <input type="submit" value="submit"/> </form> <a href="GetUserNameServlet?username=哈哈">哈哈</a> </body></html> servlet的代码如下: public class GetUserNameServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("username")); request.setAttribute("username", request.getParameter("username")); RequestDispatcher dispatcher = request.getRequestDispatcher("resultjsp.jsp"); dispatcher.forward(request, response); }} 转向到resultjsp.jsp的页面代码如下: <html> <head> <title></title> </head> <body> <%=request.getAttribute("username")%> </body></html> 现在要完成的任务是:在webtest.jsp的文本框中输入汉字和通过点击超级链接都不能出现乱码。 首先,要做一个filter,网上源码很多这里就不列举了,这个filter需要把编码格式转为GB2312。 其次,在两个jsp页面的最顶端加入如下代码: <%@ page contentType="text/html; charset=GB2312" %> 最后,在tomcat的server.xml文件中找到Connector标签,并添加useBodyEncodingForURI="true"。注:tomcat5.0需要修改,4.0就不用,默认就有这句话。 重启tomcat后,就达到目的了。 2、解决freemarker的乱码问题 依照上面那个例子,只需替换jsp页面为ftl页面,修改方法为:将.jsp改为.ftl,并将页面中 <%@ page contentType="text/html; charset=GB2312" %> 这行代码去掉即可,最后在修改好的resultftl.ftl中,将 <%=request.getAttribute("username")%> 改成 ${username} 别忘了把servlet中转发的页面也改了。 修改完上面那些,最后一步就是在web.xml中将引入freemarker的那句编码格式改成GB2312就OK了。 附上我忘记从那里摘得转化编码的filter: public class EncodingFilter extends HttpServlet implements Filter { private String encoding = null; protected FilterConfig filterConfig = null; private boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String strencoding = selectEncoding(request); if (strencoding != null) { request.setCharacterEncoding(strencoding); } } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } protected String selectEncoding(ServletRequest request) { return (this.encoding); }} web.xml中这样配置: <filter> <filter-name>EncodingFilter</filter-name> <display-name>EncodingFilter</display-name> <description>EncodingFilter</description> <filter-class>com.webtest.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>