java之过滤器
2018-08-05 07:46:37来源:博客园 阅读 ()
form.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.util.*"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <center><h3>用户登录</h3></center> 10 <body style="text-align: center;"> 11 <a href="<%=request.getContextPath()%>/CharacterServlet?name= 12 传智播客&password=123456">点击超链接</a> 13 <form action="<%=request.getContextPath() %>/CharacterServlet" method="get"> 14 <table border="1" width="600px" cellpadding="0" cellspacing="0" align="center"> 15 <tr> 16 <td height="30px" align="center">用户名:</td> 17 <td> <input type="text" name="username"/></td> 18 </tr> 19 <tr> 20 <td height="30px" align="center">密 码:</td> 21 <td> <input type="password" name="password"/></td> 22 </tr> 23 <tr> 24 <td height="30px" colspan="2" align="center"> 25 <input type="submit" value="登录" /> 26 <input type="reset" value="重置" /> 27 </td> 28 </tr> 29 </table> 30 </form> 31 </body> 32 </html>
过滤器:
1 /** 2 * 3 */ 4 package cn.itcast.filter; 5 6 import java.io.IOException; 7 import java.io.UnsupportedEncodingException; 8 9 import javax.servlet.Filter; 10 import javax.servlet.FilterChain; 11 import javax.servlet.FilterConfig; 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletRequest; 14 import javax.servlet.ServletResponse; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletRequestWrapper; 18 import javax.servlet.http.HttpServletResponse; 19 20 import com.sun.org.apache.regexp.internal.recompile; 21 22 /**TODO 23 * @author sqy 24 * 2018年8月5日上午9:46:57 25 */ 26 public class CharacterFilter implements Filter{ 27 @Override 28 public void init(FilterConfig filterConfig) throws ServletException { 29 30 31 } 32 33 @Override 34 public void doFilter(ServletRequest req, ServletResponse resp, 35 FilterChain chain) throws IOException, ServletException { 36 HttpServletRequest request = (HttpServletRequest) req; 37 HttpServletResponse response = (HttpServletResponse) resp; 38 //拦截所有请求 解决全站中文编码 39 //指定 request 和response的编码 40 request.setCharacterEncoding("utf-8"); 41 response.setContentType("text/html;charset=utf-8"); 42 43 CharacterRequest characterRequest = new CharacterRequest(request); 44 chain.doFilter(characterRequest, response); 45 46 } 47 48 @Override 49 public void destroy() { 50 51 52 } 53 54 55 56 57 58 59 60 } 61 /** 62 * TODO 针对request对象进行包装 63 * @author sqy 64 * 2018年8月5日上午11:12:18 65 */ 66 class CharacterRequest extends HttpServletRequestWrapper{ 67 68 public CharacterRequest(HttpServletRequest request) { 69 super(request); 70 } 71 72 public String getParameter(String name){ 73 String value = super.getParameter(name); 74 if(value==null){ 75 return null; 76 } 77 String method = super.getMethod(); 78 if("get".equalsIgnoreCase(method)){ 79 try { 80 value = new String(value.getBytes("iso-8859-1"),"utf-8"); 81 } catch (UnsupportedEncodingException e) { 82 e.printStackTrace(); 83 } 84 85 } 86 87 return value; 88 89 } 90 91 }
处理页面请求:
1 /** 2 * 3 */ 4 package cn.itcast.chapter01.servlet; 5 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /**TODO 11 * @author sqy 12 * 2018年8月5日上午9:42:11 13 */ 14 public class CharacterServlet extends HttpServlet{ 15 public void doGet(HttpServletRequest request,HttpServletResponse response){ 16 System.out.println(request.getParameter("username")); 17 System.out.println(request.getParameter("password")); 18 } 19 20 public void doPost(HttpServletRequest request,HttpServletResponse response){ 21 doGet(request, response); 22 } 23 }
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>chapter01</display-name> 4 5 6 <filter> 7 <filter-name>CharacterFilter</filter-name> 8 <filter-class>cn.itcast.filter.CharacterFilter</filter-class> 9 </filter> 10 11 <filter-mapping> 12 <filter-name>CharacterFilter</filter-name> 13 <url-pattern>/*</url-pattern> 14 </filter-mapping> 15 16 <servlet> 17 <servlet-name>CharacterServlet</servlet-name> 18 <servlet-class>cn.itcast.chapter01.servlet.CharacterServlet</servlet-class> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>CharacterServlet</servlet-name> 22 <url-pattern>/CharacterServlet</url-pattern> 23 </servlet-mapping> 24 25 </web-app>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Java 数据类型
下一篇:1.14(java学习笔记)数组
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash