JAVAWEB学习文章索引点这里
由于接受参数的时候可能有中文字符,所以需要设置字符编码。但是如果所有的页面都进行设置字符编码这样代码的重复量很大。所以我们直接在过滤器中统一进行设置。
首先写一个登录页面:
<%@ 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>
<title>Insert title here</title>
</head>
<body>
<a href="CharacterServlet?name=张三&password=123">使用GET方式传递参数</a>
<hr/>
使用post方式传递尝试
<form method="post" action="CharacterServlet">
用户:<input name="name" value="张三"/><br/>
密码:<input name="password" value="123"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
然后写其对应的servlet
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.EncodeTest;
@WebServlet("/CharacterServlet")
public class CharacterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
response.getWriter().println(name +".."+password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
对这个servlet进行过滤,不过设置为/*指定为全站过滤
package com.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
@WebFilter("/*")
public class CharacterFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request2 = (HttpServletRequest)request;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//将request进行包装
MyRequest myRequest = new MyRequest(request2);
chain.doFilter(myRequest, response);
}
}
class MyRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
//增强getParameter方法
@Override
public String getParameter(String name) {
String value = request.getParameter(name);
if(value == null) {
return null;
}
//如果已经是utf-8的编码就不在执行下面的代码了
if("utf-8".equals(EncodeTest.test(value))) {
return value;
}
String method = request.getMethod();
//判断是不是get方法
if("GET".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
/**
* 用于测试字符集编码
* */
class EncodeTest {
private static String str2;
public static String test(String str){
if(str == null)
return null;
str2 = str;
try {
str2 = new String(str2.getBytes("iso-8859-1"), "iso-8859-1");
if(str.equals(str2)) {
return "iso-8859-1";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
str2 = str;
try {
str2 = new String(str2.getBytes("utf-8"), "utf-8");
if(str.equals(str2)) {
return "utf-8";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}