之前第一节我们讲了DispatchServlet中会对请求做一些处理,这些处理即是执行请求路径所对应的方法。
获取此请求路径所对应的方法:
通过这两个方法获取了请求路径以及请求方式
String url = servletRequest.getRequestURI();
String method = servletRequest.getMethod();
然后获取之前在AnnotionUtil方法中获取到的所有的路径和方法,并且找到当前请求路径所对应的字符串集
List list = (List) getServletContext().getAttribute("annotationList");
String urlAndMethod = url + " " + method;
String thisRequest = "";
for (String s : list) {
if (s.contains(urlAndMethod)) {
thisRequest = s;
break;
}
}
获取到请求所对应的字符串集后,之后我们需要做的事情就是执行此路径所对应的方法。首先需要判断此路径是否对应有方法,没有的话返回错误页面,之后在路径所对应的字符串集中取出其类名和方法名,并且获取到方法中的参数的类型,最后执行路径所对应方法
if (!thisRequest.equals("")) {
String[] stringArray = thisRequest.split(" ");
try {
Class> cla = Class.forName(stringArray[3]);
Method[] methods = cla.getMethods();
Class>[] classes = null;
for (Method method1 : methods) {
if (method1.getName().equals(stringArray[4])) { //获取参数类型
Class>[] parameterTypes = method1.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
classes = parameterTypes;
}
}
}
Object o = cla.newInstance(); //实例化当前类
Object[] objects = getParam(classes);赋予参数值
String result = (String) cla.getMethod(stringArray[4], classes).invoke(o,objects);//反射执行方法并接受返回结果
if (stringArray[2].equals(SystemConfig.ResponsePage)) {//处理返回结果
servletRequest.getRequestDispatcher("/" + result).forward(servletRequest, servletResponse);
} else {
servletResponse.getWriter().write(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
servletResponse.getWriter().write("error");
}
public Object[] getParam(Class>[] classes) {
Object[] objects = null;
if (classes!=null){
objects= new Object[classes.length];
//因为目前参数只支持httpServletRequest和httpServletResponse,后续会完善
for (int i = 0;i
if (classes[i].getName().contains("HttpServletRequest")){
objects[i] = httpServletRequest;
}else if (classes[i].getName().contains("HttpServletRequest")) {
objects[i] = httpServletResponse;
}else {
objects[i] = null;
}
}
}
return objects;
}
DispatchServlet:
public class DispatcherServlet extends GenericServlet implements Serializable{
private HttpServletRequest httpServletRequest = null;
private HttpServletResponse httpServletResponse = null;
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest)servletRequest;
response = (HttpServletResponse)servletResponse;
httpServletRequest = request;
httpServletResponse = response;
}catch (Exception e){
throw new ServletException("non-HTTP request or response");
}
this.service(request,response);
}
public void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
String url = servletRequest.getRequestURI();
String method = servletRequest.getMethod();
List list = (List) getServletContext().getAttribute("annotationList");
String urlAndMethod = url + " " + method;
String thisRequest = "";
for (String s : list) {
if (s.contains(urlAndMethod)) {
thisRequest = s;
break;
}
}
if (!thisRequest.equals("")) {
String[] stringArray = thisRequest.split(" ");
try {
Class> cla = Class.forName(stringArray[3]);
Method[] methods = cla.getMethods();
Class>[] classes = null;
for (Method method1 : methods) {
if (method1.getName().equals(stringArray[4])) {
Class>[] parameterTypes = method1.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
classes = parameterTypes;
}
}
}
Object o = cla.newInstance();
Object[] objects = getParam(classes);
String result = (String) cla.getMethod(stringArray[4], classes).invoke(o,objects);
if (stringArray[2].equals(SystemConfig.ResponsePage)) {
servletRequest.getRequestDispatcher("/" + result).forward(servletRequest, servletResponse);
} else {
servletResponse.getWriter().write(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
servletResponse.getWriter().write("error");
}
}
public Object[] getParam(Class>[] classes) {
Object[] objects = null;
if (classes!=null){
objects= new Object[classes.length];
for (int i = 0;i
if (classes[i].getName().contains("HttpServletRequest")){
objects[i] = httpServletRequest;
}else if (classes[i].getName().contains("HttpServletRequest")) {
objects[i] = httpServletResponse;
}else {
objects[i] = null;
}
}
}
return objects;
}
}
测试:
@MyRequestMapping(path = "/zhu/html_test",method = RequestMethod.GET)
public String testOneaa(){
return "xxx.html";
}
@MyResponseString
@MyRequestMapping(path = "/zhu/test1",method = RequestMethod.GET)
public String testA(HttpServletRequest httpServletRequest){
return "xx";
}
@MyRequestMapping(path = "/zhu/test",method = RequestMethod.GET)
public String testOne(){
return "index.jsp";
}
总结:
最初的框架较简便,后期会完善它,讲到这大概就讲完了,很简单,只是实现了一些基本的功能还有很多细节没有处理,完整看看整个架构就运用了java的反射和注解,枚举,以及一些java web编程的基本知识,没有涉及到高深的东西,所以还是很好理解的。
项目源码地址:
http://pan.baidu.com/s/1i5sFPxr