<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.Express.mvc.DispatcherServlet</servlet-class>
<init-param>
<param-name>contentConfigLocation</param-name>
<param-value>application.properties</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<%--
Created by IntelliJ IDEA.
User: ly
Date: 2021/2/3
Time: 13:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: ly
Date: 2021/2/3
Time: 11:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>注册成功<></h1>
</body>
</html>
package com.Expres.test;
import com.Express.mvc.ResponseBody;
import com.Express.mvc.ResponseView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserController {
@ResponseBody("/login.do")
public String login(HttpServletRequest request, HttpServletResponse response){
return "success login";
}
@ResponseView("/reg.do")
public String reg(HttpServletRequest request, HttpServletResponse response){
return "success.jsp";
}
}
package com.Express.mvc;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@WebServlet(name = "DispatcherServlet")
public class DispatcherServlet extends javax.servlet.http.HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
String path= config.getInitParameter("contentConfigLocation");
InputStream is=DispatcherServlet.class.getClassLoader().getResourceAsStream(path);
HandlerMapping.load(is);
//通过反射获取注解
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1 获取用户请求的URI
String uri = req.getRequestURI();
HandlerMapping.MVCMapping mapping = HandlerMapping.get(uri);
if (mapping==null)
{
resp.sendError(404,"您的MVC请求地址有误"+uri);
return;
}
Object obj = mapping.getObj();
Method method = mapping.getMethod();
Object result=null;
try {
result = method.invoke(obj, req, resp);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
switch (mapping.getType()){
case TEXT:
resp.getWriter().write((String)result);
break;
case VIEW:
resp.sendRedirect((String)result);
break;
}
}
}
package com.Express.mvc;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 映射器(包含大量网址与方法的对应关系)
*/
public class HandlerMapping {
private static Map<String,MVCMapping> data= new HashMap<>();
//从装有一大堆集合的方法里面获取网址所对应的方法
public static MVCMapping get(String uri){
return data.get(uri);
}
//加载配置文件的方法
public static void load(InputStream is){
Properties ppt =new Properties();
try {
ppt.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取配置文件中的一个个类
Collection<Object> values = ppt.values();
for (Object cla:values) {
String className=(String) cla;
Class c= null;
try {
//加载配置文件中描述的每一个类
c = Class.forName(className);
//创建这个类的对象
Object obj =c.getConstructor().newInstance();
//获取这个类的所有方法
Method [] methods=c.getMethods();
for (Method m:methods
) {
Annotation [] as=m.getAnnotations();
if(as!=null)
{
for (Annotation anno:as
) {
if (anno instanceof ResponseBody){
//此方法是用于返回字符串给客户端
MVCMapping map=new MVCMapping(obj,m,ResponseType.TEXT);
//没有处理重复的情况
//data.put(((ResponseBody) anno).value(), map);
//处理重复的情况
Object o=data.put(((ResponseBody) anno).value(), map);
if (o!=null){
//存在了重复的 请求地址
throw new RuntimeException("请求地址重复"+((ResponseBody) anno).value());
}
}
else if (anno instanceof ResponseView){
//此方法适用于返回界面给客户端
MVCMapping map=new MVCMapping(obj,m,ResponseType.VIEW);
Object o= data.put(((ResponseView) anno).value(), map);
if (o!=null){
//存在了重复的 请求地址
throw new RuntimeException("请求地址重复"+((ResponseView) anno).value());
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 映射对象,每一个对象封装了一个方法用于处理请求
*/
public static class MVCMapping{
private Object obj;
private Method method;
private ResponseType type;
public MVCMapping(Object obj, Method method, ResponseType type) {
this.obj = obj;
this.method = method;
this.type = type;
}
public MVCMapping() {
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public ResponseType getType() {
return type;
}
public void setType(ResponseType type) {
this.type = type;
}
}
}
package com.Express.mvc;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
* 被此注解添加的方法会被用于处理请求
* 方法返回的内容会以文字形式返回到客户端
*/
public @interface ResponseBody {
String value();
}
package com.Express.mvc;
public enum ResponseType {
TEXT,VIEW;
}
package com.Express.mvc;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
* 被此注解添加的方法会被用于处理请求
* 方法返回的内容会直接重定向
*/
public @interface ResponseView {
String value();
}
配置文件
#配置每一个用于处理请求的类每一个类中包含可能用到的n个方法
a=com.Expres.test.UserController