zoukankan      html  css  js  c++  java
  • HttpServlet service方法

    1、MyServlet
    容器会根据请求方式(GET/POST)来选择调用doGet/doPost
    package com.dispatch.demo;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     *   容器会根据请求方式(GET/POST)来选择调用doGet/doPost
     */
    public class MyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doGet(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    package com.dispatch.demo;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     *   由service方法调用doGet/doPost
     */
    public class MyServlet extends HttpServlet {
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //super.service(req, resp);
            System.out.println("如果将service方法重新,而且注释掉super.service(req, resp)方法调用,这样doGet和doPost都不会执行。");
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doGet(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    

      

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        
        <servlet>
            <servlet-name>myServlet</servlet-name>
            <servlet-class>com.dispatch.demo.MyServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>myServlet</servlet-name>
            <url-pattern>/myServlet</url-pattern>
        </servlet-mapping>
    
    </web-app>
  • 相关阅读:
    React Native 开发豆瓣评分(三)集成 Redux
    React Native 开发豆瓣评分(二)路由配置
    React Native 开发豆瓣评分(一)环境搭建&配置模拟器
    VSCode 搭建 React Native 环境
    webpack4 + ejs 构建多页应用
    react-native 沉浸式状态栏
    react-native——tab配置及跳转
    uni-app 入门之 nvue (weex) 爬坑记
    javascript中bind()、call()、apply()的使用
    mysql数据库中文乱码配置文件解决以及常见mysql命令
  • 原文地址:https://www.cnblogs.com/chenweichu/p/8372545.html
Copyright © 2011-2022 走看看