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>
原文地址:https://www.cnblogs.com/yangHS/p/11134443.html