/*----------------------Demo1.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>
<title>Insert title here</title>
</head>
<body>
能别报错嘛!!?
<form action="${pageContext.request.contextPath }/servlet/ServletDemo1" method="post">
<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
/*----------------------------------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 ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
/*---------------------------------过滤器----------------------------------------------------------------------------------------*/
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 CharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
private String defaultEncoding = "utf-8";
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//用户请求至浏览器,请求是带HTTP协议,需要强转
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
//获取XML里的配置信息编码
String encode = filterConfig.getInitParameter("encodeing");
//判断配置文件里是否有配置,没有的话就把默认定义的编码指定给encode
if(encode==null)
encode = defaultEncoding;
//设置
request.setCharacterEncoding(encode);
response.setContentType("text/html;charset="+encode);
//把增强的request传给servlet层
MyRequest myreq = new MyRequest(req);
chain.doFilter(myreq, response);
//chain.doFilter(new MyRequest(req), res);//简写
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
/*---------------------------------增强request----------------------------------------------------------------------------------------*/
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
public String getParameter(String name){
//获取用户提交的数据
String value = request.getParameter(name);
//判断是否为空
if( value==null )
return value;
try {//判断是否为GET提交方式
if(request.getMethod().equalsIgnoreCase("get")){
value = new String(value.getBytes("ISO-8859-1"),request.getCharacterEncoding());
}
return value;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException();
}
}
}