博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaWeb - Servlet之全局转发器(反射)
阅读量:1996 次
发布时间:2019-04-27

本文共 2058 字,大约阅读时间需要 6 分钟。

package com.imooc.sm.global;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.GenericServlet;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;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 DispatcherServlet extends GenericServlet {    private ApplicationContext context;    public void init() throws ServletException {        super.init();        context = new ClassPathXmlApplicationContext("spring.xml");    }    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {        HttpServletRequest request = (HttpServletRequest) servletRequest;        HttpServletResponse response = (HttpServletResponse) servletResponse;        String path = request.getServletPath().substring(1);        String beanName = null;        String methodName = null;        int index = path.indexOf('/');        if (index != -1) {            beanName = path.substring(0, index) + "Controller";            methodName = path.substring(index + 1, path.indexOf(".do"));        } else {            beanName = "selfController";            methodName = path.substring(0, path.indexOf(".do"));        }        Object obj = context.getBean(beanName);        try {            Method method = obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);            method.invoke(obj,request,response);        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }    }}
Global
com.imooc.sm.global.DispatcherServlet
Global
*.do

 

转载地址:http://ooktf.baihongyu.com/

你可能感兴趣的文章
python 函数式编程
查看>>
python编码
查看>>
redis cli
查看>>
java http请求
查看>>
tensorflow 数据格式
查看>>
tf dense layer两种创建方式的对比和numpy实现
查看>>
tf initializer
查看>>
tf keras SimpleRNN源码解析
查看>>
tf keras Dense源码解析
查看>>
tf rnn输入输出的维度和权重的维度
查看>>
检验是否服从同一分布
查看>>
tf callbacks
查看>>
keras、tf、numpy实现logloss对比
查看>>
MyBatisPlus简单入门(SpringBoot)
查看>>
攻防世界web进阶PHP2详解
查看>>
攻防世界web进阶区web2详解
查看>>
xss-labs详解(上)1-10
查看>>
xss-labs详解(下)11-20
查看>>
攻防世界web进阶区ics-05详解
查看>>
攻防世界web进阶区FlatScience详解
查看>>