zoukankan      html  css  js  c++  java
  • MVC案例——问题一:多个请求使用一个servlet

    方法一:

    CustomerServlet

    package com.mvcapp.servlet;
    
    import com.sun.net.httpserver.HttpServer;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    
    public class CustomerServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req,resp);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getParameter("method");
            switch (method){
                case "add": add(req,resp);break;
                case "delete": delete(req,resp);break;
                case "update": update(req,resp);break;
            }
        }
    
        private void add(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("add");
        }
        private void delete(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("delete");
        }
        private void update(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("update");
        }
    }
    

      

    JSP代码

    <%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/4
      Time: 17:23
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <a href="CustomerServlet?method=add">ADD</a>
    <br>
    <a href="CustomerServlet?method=update">UPDATE</a>
    <br>
    <a href="CustomerServlet?method=delete">DELETE</a>
    
    </body>
    </html>
    

      

    注册servlet代码:

      <servlet>
        <servlet-name>CustomerServlet</servlet-name>
        <servlet-class>com.mvcapp.servlet.CustomerServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>CustomerServlet</servlet-name>
        <url-pattern>/CustomerServlet</url-pattern>
      </servlet-mapping>
    

      

    问题:

    1.当添加一个请求时,需要在Servlet中修改两处的代码:switch、添加方法

    2.url中使用method=xxx暴漏了要调用的方法,不私密,有安全隐患。

    方法二:

     CustomerServlet

    package com.mvcapp.servlet;
    
    import com.sun.net.httpserver.HttpServer;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.lang.reflect.Method;
    
    
    public class CustomerServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req,resp);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String servletPath = req.getServletPath();
            String methodName = servletPath.substring(1,servletPath.length()-3);
            Method method = null;
            try {
                method = getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
                 method.invoke(this,req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private void add(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("add");
        }
        private void delete(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("delete");
        }
        private void update(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("update");
        }
    }
    

      

    web.xml

      <servlet>
        <description></description>
        <display-name>CustomerServlet</display-name>
        <servlet-name>CustomerServlet</servlet-name>
        <servlet-class>com.mvcapp.servlet.CustomerServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>CustomerServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    

      

    jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <a href="add.do">ADD</a>
    <br>
    <a href="update.do">UPDATE</a>
    <br>
    <a href="delete.do">DELETE</a>
    
    </body>
    </html>
  • 相关阅读:
    常见英语缩写Abbreviations
    Outlook2016邮件如何设置靠右的预览窗格/Reading Pane?
    Power BI Server的SharePoint Credentials设置,OAuth2 + None
    Power BI 怎么定时同步自己电脑上的数据,并在Power BI server上定时刷新报表?
    Black Unique 全球购骑士卡 吃喝玩乐全有折扣
    怎么导出、同步OneNote上面的笔记到另一台电脑的解决方案
    Spring Cloud Gateway 网关内置API
    Spring Cloud Gateway 过滤器
    Spring Cloud Gateway 路由谓词工厂
    Spring Cloud Gateway 路由定位器
  • 原文地址:https://www.cnblogs.com/yangHS/p/11134443.html
Copyright © 2011-2022 走看看