BaseServlet方法的抽取

在刚学习Servlet时,是一个功能对应这一个Servlet,一个对象有(addServlet,UpdateServlet,SelectServlet…)如果一个对象有哦一百个功能,我们就需要创建100个servlet这在MVC中有着很高的冗余,因此,将这些Servlet整合成一个对象的Servlet,将User对应的方法封装到该对象的Servlet中,在BaseServlet中重写service()方法,通过Method传递参数的方式,判断使用Servlet中的哪个方法。

此外,BaseServlet中还简化了转发和重定向。

在这里插入图片描述

BaseServlet.java

package Base;

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 abstract class BaseServlet extends HttpServlet {

    @Override
    public  void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 获取参数,用来识别用户想使用的方法。
         * 这个参数用method表示。
         * Servlet通过request获取到method 然后判断调用哪一个方法 。
         */

        String methodName=req.getParameter( "method" );
        if(methodName ==null||methodName.isEmpty()){
            throw  new RuntimeException("没有参数!!!");
        }

        Class c=this.getClass();
        Method method=null;
        try {
            method=c.getMethod( methodName,HttpServletRequest.class,HttpServletResponse.class );
        } catch (Exception e) {
            throw  new RuntimeException("您调用的"+methodName+"方法不存在!");
        }
        try {
            method.invoke( this,req,resp );

        } catch (Exception e) {
            System.out.println("您调用的方法内部异常!");
            throw new RuntimeException( e );
        }
        try {
            /**
             * 获取返回的字符串,判断是转发还是重定向
             * 确定后帮助他完成转发或重定向
             */
            String result =(String)method.invoke( this,req,resp );
            if(result==null||result.trim().isEmpty()){
                return;
            }
            if(result.contains( ":" )){
                int index=result.indexOf( ":" );
                String s=result.substring( 0,index );
                String path=result.substring( index+1);
                if(s.equalsIgnoreCase( "r" )){
                    resp.sendRedirect(req.getContextPath()+ path );
                }else if(s.equalsIgnoreCase( "f" )){
                    req.getRequestDispatcher( path).forward( req,resp );
                }else{
                    throw new RuntimeException( "指定的操作不正常!" );
                }

            }else{
                req.getRequestDispatcher( result ).forward( req,resp );

            }
            /**
             * 判断是
             */
        }catch (Exception e){

        }
    }
}

UserServlet.java

package Servlet;

import Base.BaseServlet;

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.InvocationTargetException;
import java.lang.reflect.Method;

public class UserServlet extends BaseServlet {
    /**
     * 在这里给出多个请求的处理方法。
     * 请求处理方法:除了名字之外,都与Service方法相同。
     */

    public  String addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("这是添加的方法!");
        return "r:/login.jsp";

    }

    public  String  deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("这是删除的方法!");
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王子健121

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值