Javaweb解决全站字符乱码问题(POST和GET)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wyf2017/article/details/80041835

乱码问题:

       1. 获取请求参数中的乱码问题

        POST请求:request.setCharaterEncoding("utf-8")

        GET请求:new String(request.getParameter("xxx").getBytes("iso-8859-1"),"utf-8");

        2.响应的乱码问题

        request.setContextType("text/html;charset=utf-8");

    分析:基本上在每个servlet中 都需要解决乱码问题,所以应把这个工作放到过滤器中来完成

下面就通过一个案例来解释下如何完成这歌乱码问题

(1)首先创建一个index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>//这个需要你自己导jstl-1.2.jar
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<a href="<c:url value='/AServlet?username=张三'/>">点击这里</a>
<form action="<c:url value='/AServlet'/>" method="post">
用户名:<input type="text" name="username" value="李四"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

(2)创建一个名叫AServlet的servlet

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 AServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setContentType("text/html;charset=utf-8");
			String name = request.getParameter("username");
			response.getWriter().println(name);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String name = request.getParameter("username");
		response.getWriter().println(name);
	}

}

(3)点击运行,会出现下面的乱码


(4)解决方法

一.创建一个叫EncodingFilter的过滤器,在创建的过程中将pattern中设置为/*这样一来写好的过滤器就可以作用于所有路径下的serlvet,从来代码的可重复性

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;
public class EncodingFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//处理post请求编码问题
		request.setCharacterEncoding("utf-8");
		/**
		 *处理get请求编码问题
		 * 调包request
		 * 首先创建一个request的修饰类,然后在放行的时候,使用我们自己的request
		 */
		HttpServletRequest req = (HttpServletRequest) request;
		if(req.getMethod().equals("GET")) {
			EncodingRequest er = new EncodingRequest(req);
			chain.doFilter(er, response);
		}else if(req.getMethod().equals("POST")) {
			chain.doFilter(request, response);
		}
	}
	public void init(FilterConfig fConfig) throws ServletException {
	}

}

二.在创建一个EncodingRequest.java

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class EncodingRequest extends HttpServletRequestWrapper{
	private HttpServletRequest req;
	public EncodingRequest(HttpServletRequest request) {
		super(request);
		this.req = request;
	}
	/**
	 * 这就是需要些的request的装饰类,在EncodingFilter中通过创建EncodingRequest对象来调用getParameter()方法从而解决Get响应出现乱码问题
	 */
	public String getParameter(String name) {
		String value = req.getParameter(name);
		try {
			value = new String(value.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return value;
	}

}

5.接下来就是显示结果的时候了



6.通过这个案例我么在今后的开发中如果需要解决乱码问题就可以写着几个了来完成,其次我们需要在web.xml中配置下文件



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页