Servlet 进阶 getMethod 反射 及 request response 转发与重定向 完整综合版

版权声明:转载请注明出处 交流请加QQ 156356969 https://blog.csdn.net/weixin_41957098/article/details/88816010

被继承的抽象类 BaseServlet.java

package cn.day1901.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 {
    private static final long serialVersionUID = 1L;
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 客户端必须传method方法
         * 获取参数,用来识别处理方法,判断是哪一个方法,是哪一个就调用哪一个
         * 得到方法名,通过反射来调用方法, 得到当前类的Class对象,然后调用安的方法进行查询,得到Method
         * 我们要得到当前类的方法,所以需要得到当前类的Class
         * getDeclaredMethod 可以获取任意方法 像,protected修饰的
         * getMethod 只可以获取 修饰符为 public 的方法
         */
        
        String methodName = request.getParameter("method");
        if(methodName == null || methodName.trim().isEmpty()) {
            throw new RuntimeException("您没有传递method参数,无法确定您想要调用的方法!");
        }
        Method me =null;
        try {
            me = this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);        
        } catch (Exception e) {            
            throw new RuntimeException(e+"你要调用的方法:"+methodName+"不存在");
        }         
        Object req = null;
        Object resp = null;
        try {
            String result = (String) me.invoke(this, request,response);
            /*
             * 获取请求处理方法执行返回的字符串,它表示转发或重定向的路径,帮它完成转发或重定向
             * 查看返回的字符串是否包含冒号,如果没有,就表示转发,如果有使用冒号分割字符串,得到前缀和后前缀,如果是f则转,r重定向
             */
            System.out.println(">"+result);
            
            if(result == null || result.trim().isEmpty()) {return;}
            if(result.contains(":")) {
                int index=result.indexOf(":");//获取冒号的位置
                String s = result.substring(0, 1);//截取前缀
                String path = result.substring(index+1);//截取后缀
                if(s.equalsIgnoreCase("r")) {
                    response.sendRedirect(request.getContextPath()+path);
                }else if(s.equalsIgnoreCase("f")) {
                    request.getRequestDispatcher(path).forward(request, response);
                }else {
                    throw new RuntimeException("你的指定操作"+s+",当前版本不支持!");
                }
            }else {
                System.out.println("没有冒号");
                request.getRequestDispatcher(result).forward(request, response);
            }
            
        } catch (Exception e) {            
            throw new RuntimeException(e+"你调用方法内部"+methodName+"冒出的异常");
        }
    }
}
 

继承它,使用安!

package cn.day1901.servlet;

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

public class Bservlet extends BaseServlet {
    private static final long serialVersionUID = 1L;
    
    public String defaultForword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        System.out.println("addUser");
        return "/index.jsp";
    }
    
    public String addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        System.out.println("addUser");
        return "f:/index.jsp";
    }
    
    public String editUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        System.out.println("editUser");
        return "r:/index.jsp";
    }
    
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {            
        System.out.println("deleteUser");
        return "f:/index.jsp";
    }
    public String findAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        System.out.println("findAllUser");
        return "r:/index.jsp";
    }
    public String nullAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        System.out.println("nullAllUser");
        return null;
    }

}
 

不会用的,可以加我博客上面的QQ群 为你解答 我也是新手,学习当中把东西分享出来,结交朋友,共同进步,高手不要喷我哦!

猜你喜欢

转载自blog.csdn.net/weixin_41957098/article/details/88816010
今日推荐