Jsp乱码

GET乱码
jsp:jsp本身用utf-8编码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8" %>可选
server.xml
    <Connector port="80" protocol="HTTP/1.1"
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443" URIEncoding='UTF-8'/>

 

 

POST乱码
web.xml filter 加在struts前
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
import javax.servlet.Filter;
 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain filterChain) throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest)req;
  HttpServletResponse response = (HttpServletResponse)res;
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //ServletActionContext.getResponse().getWriter().println('chinese');
        //response返回到客户端乱码就设置ContentType("text/html;charset=utf-8");
        response.setContentType("text/html;charset=utf-8");
        filterChain.doFilter(request, response);

 }
 
转向:
在servlet中 PrintWriter out = response.getWriter();

out.println("<script language='javascript'>");
out.println("alert('" + msg + "');");
out.println("window.location='" + url + "'");
out.println("</script>");
 

你可能感兴趣的:(jsp)