Refused to display '' in a frame because it set 'X-Frame-Options' to 'deny'

前言

当浏览器发送⼀次请求到服务器时,Servlet容器会根据请求的url-pattern找到对应的Servlet类,执⾏对应的doPost或doGet⽅法,最后将响
应信息返回给浏览器。
这种情况下,⼀个具体的Servlet类只能处理对应的web.xml中配置的url-pattern请求,⼀个Servlet类,⼀对配置信息。如果业务扩展,需要三个Servlet来处理请求,就需要再加上两个具体的Servlet类,两对配置信息,一方面每新增一个接口都需要增加大量的代码,另一方面同一个Model的不同操作方法非常分散;
针对上面提到的问题,我们能不能实现⼀个Servlet处理多个请求呢?如SpringMVC的Controller。

Servlet 的抽取

使一个 servlet 能接收并处理多个请求

  1. 前端访问的时候, 在请求参数中添加 method 属性, 其中值是将要访问的方法
  2. 在 BaseServlet 中利用反射机制, 根据 method 的值, 调用对应的方法
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        req.setCharacterEncoding("UTF-8");
        
        try {
            //1、获得请求的method的名称
            String methodName = req.getParameter("method");
            //2、获得当前被访问的对象的字节码对象
            Class clazz = this.getClass();//ProductServlet.class ---- UserServlet.class
            //3、获得当前字节码对象的中的指定方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
            //4、执行相应功能方法
            method.invoke(this, req,resp);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}
  1. 抽取完成后, 业务 Servlet 的书写方式变成:

    1. 继承 BaseServlet
    2. 实现业务方法
    public class PrdocutServlet extends BaseServlet {
    
        //删除一个商品
        public void delProFromCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 删除一个商品的逻辑
        }
    
        //根据商品的类别获得商品的列表
        public void productList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 逻辑代码
        }
    
    }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容