在Servlet中封装转发(服务器端跳转)和重定向(客户端跳转)

本文介绍了一种基于Servlet的动态调度机制,通过反射调用不同方法实现请求处理的灵活性。核心内容包括基础Servlet类的设计,以及如何通过参数选择具体业务方法进行调用,支持转发和重定向。

1 基础的Servlet类

package com.servlet;

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 {
        System.out.println("******");
        String mehtodName=req.getParameter("method");
        if(mehtodName==null||mehtodName.trim().isEmpty()){
            throw new RuntimeException("没有获得到method参数");
        }
        Class c=this.getClass();
        Method method=null;
        try {
            method= c.getMethod(mehtodName, HttpServletRequest.class, HttpServletResponse.class);

        } catch (NoSuchMethodException e) {
            throw new RuntimeException("调用的方法不存在-->"+mehtodName);
        }

        try {
            //method.invoke(this,req,resp);
            String result=(String) method.invoke(this,req,resp);
            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) {
            throw new RuntimeException("调用的方法-->"+mehtodName+",在内部发生了异常。。。"+e.getMessage());
        }
    }
}

2 继承类

package com.servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



public class AServlet extends BaseServlet {


    public String addUser(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("addUser()....");
        return "f:/index.jsp"; //跳转
    }
    public String editUser(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("editUser()....");
        return "r:/index.jsp";//重定向
    }
    public String deleteUser(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("deleteUser()....");
        return null;

    }
}

以后我们可以这样封装经常重复代码
sendRedirect
getRequestDispatcher
操作了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值