zoukankan      html  css  js  c++  java
  • 建立和使用一个RequestAttribute监听者(Head first servlet+Jsp)(实验三)

    package com.example;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequestAttributeEvent;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class TestServletContextType
     */
    @WebServlet("/TestServletContextType")
    public class TestServletContextType extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public TestServletContextType() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setAttribute("a","b");
            request.setAttribute("a", "c");
            request.removeAttribute("a");
        }
        
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    监听者类:

    package com.example;
    
    import javax.servlet.ServletRequestAttributeEvent;
    import javax.servlet.ServletRequestAttributeListener;
    
    public class MyservletAttributeListen implements ServletRequestAttributeListener {
    
        @Override
        public void attributeAdded(ServletRequestAttributeEvent ev) {
            System.out.println("A:"+ev.getName()+"->"+ev.getValue());
    
        }
    
        @Override
        public void attributeRemoved(ServletRequestAttributeEvent ev) {
            System.out.println("M:"+ev.getName()+"->"+ev.getValue());
    
        }
    
        @Override
        public void attributeReplaced(ServletRequestAttributeEvent ev) {
            System.out.println("P:"+ev.getName()+"->"+ev.getValue());
    
        }
    
    }

    web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>FirstListener</display-name>
      <servlet>
        <servlet-name>ListenerTest</servlet-name>
        <servlet-class>com.example.ListenerTest</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>ListenerTest</servlet-name>
        <url-pattern>/ListenTest.do</url-pattern>
      </servlet-mapping>
      
      <servlet>
        <servlet-name>TestServletContextType</servlet-name>
        <servlet-class>com.example.TestServletContextType</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>TestServletContextType</servlet-name>
        <url-pattern>/testtype.do</url-pattern>
      </servlet-mapping>
      
      <servlet>
        <servlet-name>TestServletContextType2</servlet-name>
        <servlet-class>com.example.TestServletContextType2</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>TestServletContextType2</servlet-name>
        <url-pattern>/testtype1.do</url-pattern>
      </servlet-mapping>
      
      <context-param>
        <param-name>breed</param-name>
        <param-value>Greet Dane</param-value>
      </context-param>
      <listener>
        <listener-class>com.example.MyservletAttributeListen</listener-class>
      </listener>
    </web-app>

    测试结果:

    注:如果属性被替换,getValue方法返回的是原来的属性。

  • 相关阅读:
    ASP.Net核心对象HttpRequest
    HTTP状态码和常用对照表
    Http协议之Get和Post的区别
    Http协议之Request和Response
    HTTP协议的几个概念
    表单内容提交到数据库案例
    关于表单提交的规则
    HttpHandler简介
    DHCP(五)
    DHCP(四)
  • 原文地址:https://www.cnblogs.com/zoeyqq/p/6861211.html
Copyright © 2011-2022 走看看