Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法)

准备一个Servlet(Lesson2/lesson6_2.Sample7.java)

package lesson6_2;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

//@WebServlet("/Sample7")←此处如果使用webservlet,可以不用在web.xml配置Servlet,但是Filter必须配置
public class Sample7 extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
          try {
            //コンテンツタイプの設定
              response.setContentType("text/html; charset=utf-8");

              PrintWriter pw = response.getWriter();
              pw.println(
                        "お選びください
\n"
+ "
\n"
+ "乗用車
\n"
+ "トラック
\n"
+ "オープンカー
\n"
); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }

Filter代码段:(Lesson2/lesson6_2.SampleFilter.java)

package lesson6_2;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;


/**
 * Servlet Filter implementation class SampleFilter
 */
//@WebFilter("/SampleFilter")
public class SampleFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here
        response.setContentType("text/html; charset=UTF-8");

        PrintWriter pw=response.getWriter();
        pw.println("\n"+
                    "SampleFilter\n"+
                    "
\n"+ "

こんにちわ

"
+ "
\n"
); // pass the request along the filter chain chain.doFilter(request, response); pw.println("
ありがとうございました.\n"
+ "
\n"
+ "" ); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) { // TODO Auto-generated method stub } }

web.xml配置(WEB-INF/web.xml)


<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">
  <display-name>Lesson2display-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

  <filter>
  <filter-name>SampleFilterfilter-name>
  <filter-class>lesson6_2.SampleFilterfilter-class>
  filter>

  <filter-mapping>
  <filter-name>SampleFilterfilter-name>
  <url-pattern>/Sample7url-pattern>
  filter-mapping>

  <servlet>
  <servlet-name>Sample7servlet-name>
  <servlet-class>lesson6_2.Sample7servlet-class>
  servlet>

  <servlet-mapping>
  <servlet-name>Sample7servlet-name>
  <url-pattern>/Sample7url-pattern>
  servlet-mapping>
web-app>

执行结果:

在浏览器中访问:http://localhost:8080/Lesson2/Sample7
Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法)_第1张图片

说明:

\里\配置的是需要拦截的url,当访问该url的时候,首先调用Filter(过滤器)里的内容,然后根据过滤器的方法继续执行。

你可能感兴趣的:(Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法))