直接上代码:
package pp; 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.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebFilter(urlPatterns={"/*"},initParams={@WebInitParam(name="charset",value="utf-8")}) public class EncodeFilter implements Filter { private String defaultCharset="utf-8"; @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)arg0; req.setCharacterEncoding(defaultCharset); System.out.println("----------filter--------------"); HttpServletResponse res=(HttpServletResponse)arg1; res.setCharacterEncoding(defaultCharset); arg2.doFilter(req, res); } @Override public void init(FilterConfig arg0) throws ServletException { defaultCharset=arg0.getInitParameter("charset"); System.out.println(defaultCharset+"---------------"); } }
package pp; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("all") @WebServlet(urlPatterns={"/abc.do","/def.do"},initParams={@WebInitParam(name="PI",value="2.222222")}) public class UserSevlet extends HttpServlet { private String value; public void init() throws ServletException{ value=this.getInitParameter("PI"); } protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ this.doPost(req, resp); } protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ //首先获取index.jsp的输入值 String account=req.getParameter("account"); System.out.println(account); req.setAttribute("account", account); req.getRequestDispatcher("/ok.jsp").forward(req, resp); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="<%=basePath%>def.do" method="post"> <input type="text" name="account"/> <input type="submit" value="注册"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 您刚才注册信息是:${requestScope.account} </body> </html>
工程目录: