目的是让浏览网站的用户所接收到的信息的编码方式统一为UTF-8,防止乱码的产生
1.没加过滤器之前:
拿Jsp工程(名叫web)中的两个Servlet做实验
ChineseServlet.java:
package cn.edu.hpu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ChineseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="我的中国心";
request.setAttribute("data", data);
request.getRequestDispatcher("/result").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
ResultServlet.java
package cn.edu.hpu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResultServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String text=(String)request.getAttribute("data");
PrintWriter out=response.getWriter();
out.write("传过去的那句话是(The word get is):"+text);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
在web.xml中配置:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
ChineseServlet
cn.edu.hpu.servlet.ChineseServlet
ResultServlet
cn.edu.hpu.servlet.ResultServlet
ChineseServlet
/chin
ResultServlet
/result
index.jsp
启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:
?????????The word get is?:?????
出现乱码,说明没有指定编码。
解决办法:
两边servlet同时设置(这里以UTF-8为编码标准):
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
需要显示的时候:
String text=(String)request.getAttribute(“data”)+”是传过来的那句话”;
System.out.println(text);
OutputStream out =response.getOutputStream();
out.write(“”.getBytes());
//用html技术中meta标签模拟了一个http响应头,来控制浏览器的行为
out.write(““.getBytes());
out.write(text.getBytes(“UTF-8”));
out.write(“”.getBytes());
或者直接:
response.setContentType(“text/html;charset=UTF-8”);
以上方法很臃肿,而且非常不简介,无法应对多个界面,所以要使用过滤器来统一设置编码。
2.加过滤器之后:
ChineseServlet.java:
package cn.edu.hpu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ChineseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="我的中国心";
request.setAttribute("data", data);
request.getRequestDispatcher("/result").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
ResultServlet .java:
package cn.edu.hpu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResultServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String text=(String)request.getAttribute("data");
PrintWriter out=response.getWriter();
out.write("传过去的那句话是(The word get is):"+text);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
过滤器:
package cn.edu.hpu.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;
import javax.servlet.http.HttpServletResponse;
public class TestFilter implements Filter {
private String characterEncoding=null;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterchain) throws IOException, ServletException {
request.setCharacterEncoding(characterEncoding);
response.setCharacterEncoding(characterEncoding);
response.setContentType("text/html;charset="+characterEncoding);
//获取URL的路径和参数
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String path=req.getServletPath();
String param=req.getQueryString();
if(path!=null&¶m!=null){
path=path+"?"+param;//全请求路径
}
System.out.println("Path:"+path);
//跳转或带参数跳转
request.getRequestDispatcher(path).forward(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
characterEncoding = filterConfig.getInitParameter("characterEncoding");
}
}
在web.xml中配置:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
TestFilter
cn.edu.hpu.filter.TestFilter
characterEncoding
UTF-8
TestFilter
/*
ChineseServlet
cn.edu.hpu.servlet.ChineseServlet
ResultServlet
cn.edu.hpu.servlet.ResultServlet
ChineseServlet
/chin
ResultServlet
/result
index.jsp
启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:
传过去的那句话是(The word get is):我的中国心
没有发生乱码,说明过滤成功!!
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net