标签:tps cat ping http span splay util eof ack
java web基础 --- URL重定向Filter
httpRequest.getRequestDispatcher("/helloWorld").forward(httpRequest, response);
服务器端转发,不改变浏览器端URL地址,常用
httpResponse.sendRedirect(uri);
浏览器端重定向,改变URL在浏览器地址栏
URLOverWriteFilter.java
1 package com.web1.util; 2 3 import java.io.IOException; 4 5 import javax.print.attribute.standard.ReferenceUriSchemesSupported; 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.annotation.WebFilter; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 /** 17 * Servlet Filter implementation class URLOverWriteFilter 18 */ 19 @WebFilter("/URLOverWriteFilter") 20 public class URLOverWriteFilter implements Filter { 21 22 private FilterConfig config; 23 /** 24 * Default constructor. 25 */ 26 public URLOverWriteFilter() { 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see Filter#destroy() 32 */ 33 public void destroy() { 34 // TODO Auto-generated method stub 35 } 36 37 /** 38 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 39 */ 40 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 41 //do something 42 String uri = ""; 43 HttpServletRequest httpRequest = null; 44 if (request instanceof HttpServletRequest) { 45 httpRequest = (HttpServletRequest)request; 46 System.out.println("URL:" + httpRequest.getRequestURL().toString()); 47 System.out.println("URI:" + httpRequest.getRequestURI().toString()); 48 uri = httpRequest.getRequestURI(); 49 uri = uri.replace("home.html", "helloWorld"); 50 } 51 if (httpRequest.getRequestURI().indexOf("home.html") >= 0) { 52 /*if (response instanceof HttpServletResponse) { 53 HttpServletResponse httpResponse = (HttpServletResponse)response; 54 httpResponse.sendRedirect(uri); 55 }*/ 56 httpRequest.getRequestDispatcher("/helloWorld").forward(httpRequest, response); 57 } 58 else{ 59 //go to next Filter 60 chain.doFilter(request, response); 61 } 62 } 63 64 /** 65 * @see Filter#init(FilterConfig) 66 */ 67 public void init(FilterConfig fConfig) throws ServletException { 68 this.config = fConfig; 69 } 70 71 }
web.xml
按照filter-mapping的顺序
<?xml version="1.0" encoding="UTF-8"?> <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" metadata-complete="true" version="3.0"> <description> Hello World Servlet </description> <display-name>Hello World Servlet</display-name> <filter> <filter-name>Encode Filter</filter-name> <filter-class>com.web1.util.RequestEncodeFilter</filter-class> </filter> <filter-mapping> <filter-name>Encode Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>URLOverFilter Filter</filter-name> <filter-class>com.web1.util.URLOverWriteFilter</filter-class> </filter> <filter-mapping> <filter-name>URLOverFilter Filter</filter-name> <url-pattern>/home.html</url-pattern> </filter-mapping> <servlet> <servlet-name>helloWorld</servlet-name> <servlet-class>com.web1.hello.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloWorld</servlet-name> <url-pattern>/helloWorld</url-pattern> </servlet-mapping> <servlet> <servlet-name>helloImage</servlet-name> <servlet-class>com.web1.hello.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloImage</servlet-name> <url-pattern>/helloImage</url-pattern> </servlet-mapping> </web-app>
标签:tps cat ping http span splay util eof ack
原文地址:http://www.cnblogs.com/Jeremy2001/p/6287427.html