BaseServlet

BaseServlet简介

  1.我们希望在一个Servlet中可以有多个请求处理的方法!

  2.客户端发送请求时,必须多给一个参数,用来说明要调用的方法

     请求处理方法的签名必须与service相同,即返回值和参数以及申明的异常都相同!

     客户端必须传递名为method的参数!

    domain:User

    dao:UserDao

    service:UserService

    servlet:UserServlet

 

    void init(ServletConfig config)

    void destory()

    void service(ServletRequest, ServletResponse) throws IOException,ServletException{

      在这里让他去调用其他方法!

      要求:用户发送请求时,必须给出一个参数,来说明要调用哪一个方法

      //获取参数,通过参数名称来确定要调用的方法

    }

    public void addUser(){

    }

 运行:http://localhost:8080/xxx/Aservlet?method=addUser

BaseServlet.java

package cn.cmlx.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseServlet extends HttpServlet {
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
         * 1.获取参数,用来识别用户想请求哪一个方法 2.判断是哪一个方法,就调用哪一个
         */
        String methodName = req.getParameter("method");
        if (methodName == null || methodName.trim().isEmpty()) {
            throw new RuntimeException("您没有传递method参数!无法确定您要调用的方法!");
        }
        /*
         * 得到方法名称,是否可通过反射来调用方法? 1.得到方法名,通过方法名再得到 * 需要得到Class,然后调用它的方法进行查询!得到Method *
         * 我们要查询得是当前类的方法,所以我们需要得到当前类的Class
         */
        Class c = this.getClass();// 得到当前类的class对象
        Method method = null;
        try {
            method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("您要调用的方法" + methodName + "不存在!");
        }
        /*
         * 调用method表示的方法
         */
        try {
            String result = (String) method.invoke(this, req, resp);
            /*
             * 获取请求方执行后返回的字符串,它表示转发或重定向的路径! 帮他完成转发或重定向!
             */
            /*
             * 如果返回的字符串为null,或是"",那么我们什么也不做!
             */
            if (result == null || result.trim().isEmpty()) {
                return;
            }
            /*
             * 查看返回字符串是否包含冒号,如果没有表示转发。 如果有,使用冒号分割字符串,得到前缀和后缀!
             * 其中前缀是f表示转发,如果是r表示重定向,后缀就是要转发的或重定向的路径!
             */
            if (result.contains(":")) {
                // 使用冒号分割字符串,得到前缀、后缀
                int index = result.indexOf(":");// 获取冒号的位置
                String s = result.substring(0, index);
                String path = result.substring(index + 1);
                if (s.equalsIgnoreCase("r")) {// 如果前缀是r,那么重定向!
                    resp.sendRedirect(req.getContextPath() + path);
                }else if(s.equalsIgnoreCase("f")) {
                    req.getRequestDispatcher(path).forward(req, resp);
                }else {
                    throw new RuntimeException("你指定的操作:"+s+"当前版本还不支持!");
                }
            }else {//没有冒号,默认为转发!
                req.getRequestDispatcher(result).forward(req, resp);
            }

        } catch (Exception e) {
            System.out.println("您调用的方法" + methodName + ",它内部出了异常!");
            throw new RuntimeException(e);
        }
    }
}

测试:

TestBaseServlet.java
package cn.cmlx.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 在这里给出多个请求方法 请求处理方法,除了名称以外,都与service方法相同
 * 
 * @author 赤名莉香
 *
 */
@WebServlet("/TestBaseServlet")
public class TestBaseServlet extends BaseServlet {

    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("addUser()...");
        throw new RuntimeException("测试一下");
    }

    public void editUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("editUser()...");
    }

    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("deleteUser()...");
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("findAll()...");
    }
}
TestBaseServlet1.java
package cn.cmlx.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/TestBaseServlet1")
public class TestBaseServlet1 extends BaseServlet {
    public String fun1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("fun1()...");
        return "f:/index.jsp";
    }
    public String fun2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("fun2()...");
        return "r:/index.jsp";
    }
    public String fun3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("fun3()...");
        return null;
    }
}

 

posted @ 2018-08-23 09:09  cmlx  阅读(105)  评论(0)    收藏  举报