在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
操作了。

发布了109 篇原创文章 · 获赞 56 · 访问量 5710

猜你喜欢

转载自blog.csdn.net/zhang6132326/article/details/104254911