javaWEB总结(32):字符编码过滤器


目录结构




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>javaWeb_32</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

login.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>login.jsp</title>
</head>
<body>

	<form action="<%=request.getContextPath() %>/index.jsp" method="post">
		姓名:<input type="text" name="name">
		<input type="submit" value="提交">
	</form>


</body>
</html>


index.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>index.jsp</title>
</head>
<body>

	你好:<%=request.getParameter("name") %>
</body>
</html>

运行后出现乱码



修改index.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>index.jsp</title>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");

%>
	你好:<%=request.getParameter("name") %>
</body>
</html>



运行效果





但是如果页面特别多,每个页面都要这样写,很不方便。


所以要加一个过滤器


项目结构






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>javaWeb_32</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
  </context-param>
  
  <filter>
      <filter-name>EncodingFilter</filter-name>
      <filter-class>com.dao.chu.EncodingFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>EncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

login.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>login.jsp</title>
</head>
<body>

	<form action="<%=request.getContextPath() %>/index.jsp" method="post">
		姓名:<input type="text" name="name">
		<input type="submit" value="提交">
	</form>


</body>
</html>

index.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>index.jsp</title>
</head>
<body>
	你好:<%=request.getParameter("name") %>
</body>
</html>

HttpFilter.java


package com.dao.chu;

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;

/**
 * 
 * <p>
 * Title: HttpFilter
 * </p>
 * <p>
 * Description: http请求定制Filter
 * </p>
 */
public abstract class HttpFilter implements Filter {

	/**
	 * 用于保存init(FilterConfig filterConfig)的FilterConfig对象
	 */
	private FilterConfig filterConfig;

	/**
	 * 直接返回init(FilterConfig filterConfig)的FilterConfig对象
	 */
	public FilterConfig getFilterConfig() {
		return filterConfig;
	}

	/**
	 * 不建议子类直接覆盖,将可能会导致filterConfig成员变量初始化失败
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

		this.filterConfig = filterConfig;
		init();

	}

	/**
	 * 供子类继承的初始化方法,可以通过getFilterConfig获取FilterConfig对象
	 */
	protected void init() {}

	/**
	 * 原生的doFilter方法,在方法内部把ServletRequest和ServletResponse
	 * 转为了HttpServletRequest和HttpServletResponse并调用了 doFilter(HttpServletRequest
	 * httpRequest, HttpServletResponse httpResponse, FilterChain chain)方法
	 * 
	 * 
	 * 若编写Filter的过滤方法不建议直接继承该方法,而应该继承doFilter(ServletRequest request,
	 * ServletResponse response, FilterChain chain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		doFilter(httpRequest, httpResponse, chain);

	}

	/**
	 * 抽象方法,为http请求定制,必需实现的方法
	 * 
	 */
	public abstract void doFilter(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse, FilterChain chain)
			throws IOException, ServletException;

	/**
	 * 空的destroy方法
	 */
	@Override
	public void destroy() {}

}

EncodingFilter.java


package com.dao.chu;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter extends HttpFilter {
	
	private String encoding;
	
	/**
	 * 初始化Filter时,加载ecoding变量
	 */
	@Override
	protected void init() {
		encoding=getFilterConfig().getServletContext().getInitParameter("encoding");
	}

	@Override
	public void doFilter(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse, FilterChain chain)
			throws IOException, ServletException {
		
		httpRequest.setCharacterEncoding(encoding);
		chain.doFilter(httpRequest, httpResponse);

	}

}

运行后也没有乱码,并且更加符合要求。


  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值