JavaWeb之Filter实现全站编码

时间:2021-10-19 13:13:07

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<a href="${pageContext.request.contextPath}/CharacterServlet?username=小小的太阳&password=123456">Get提交</a>

	<form action="${pageContext.request.contextPath}/CharacterServlet" method="post">
		姓名:<input type="text" name="username"><br> 
		密码:<input type="text" name="password"><br> 
			<input type="submit" value="提交"> 
			<input type="reset" value="重置">
	</form>

</body>
</html>

CharacterServlet.java

package zh.servlet.demo;

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 CharacterServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		response.getWriter().write(username+","+password);
		
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
		
	}

}

MyRequest.java

package zh.servlet.demo;

import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class MyRequest extends HttpServletRequestWrapper {

	public MyRequest(HttpServletRequest request) {
		super(request);
	}

	@Override
	public String getParameter(String name) {

		String value = super.getParameter(name);
		if (value == null) {
			return null;
		}
		// 解决get请求中文乱码问题
		if ("get".equalsIgnoreCase(super.getMethod())) {
			try {
				value = new String(value.getBytes("iso-8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return value;
	}

}

CharacterFilter.java

package zh.filter.demo;

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;

import zh.servlet.demo.MyRequest;

public class CharacterFilter implements Filter {

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		// 强转
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		// 解决响应编码问题
		res.setContentType("text/html;charset=utf-8");
		// 解决post请求中文乱码问题
		req.setCharacterEncoding("utf-8");
		// 调包req,对其进行包装,解决get请求中文乱码问题
		MyRequest myReq = new MyRequest(req);
		chain.doFilter(myReq, response);
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Javaweb01</display-name>
  
  <servlet>
    <servlet-name>CharacterServlet</servlet-name>
    <servlet-class>zh.servlet.demo.CharacterServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CharacterServlet</servlet-name>
    <url-pattern>/CharacterServlet</url-pattern>
  </servlet-mapping>
  
  <filter>
  	<filter-name>CharacterFilter</filter-name>
  	<filter-class>zh.filter.demo.CharacterFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

JavaWeb之Filter实现全站编码

点击超链接,结果

JavaWeb之Filter实现全站编码

表单输入,结果

JavaWeb之Filter实现全站编码

JavaWeb之Filter实现全站编码