change directory
This commit is contained in:
50
blog/dispatcher-servlet/pom.xml
Normal file
50
blog/dispatcher-servlet/pom.xml
Normal file
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>dispatcher-servlet</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>dispatcher-servlet</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<junit.version>5.7.1</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.dispatcherservlet;
|
||||
|
||||
import com.example.dispatcherservlet.annotation.Controller;
|
||||
import com.example.dispatcherservlet.annotation.RequestMapping;
|
||||
import com.example.dispatcherservlet.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class HelloController {
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/hello")
|
||||
public String hello() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.dispatcherservlet;
|
||||
|
||||
import com.example.dispatcherservlet.annotation.Controller;
|
||||
import com.example.dispatcherservlet.annotation.RequestMapping;
|
||||
import com.example.dispatcherservlet.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class TestController {
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/hi")
|
||||
public String hi() {
|
||||
return "hi";
|
||||
}
|
||||
|
||||
@RequestMapping("/main")
|
||||
public String main() {
|
||||
return "main.jsp";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.dispatcherservlet.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Controller {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.dispatcherservlet.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequestMapping {
|
||||
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.dispatcherservlet.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ResponseBody {
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
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.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@WebServlet(name = "dispatcherServlet", urlPatterns = "/")
|
||||
public class DispatcherServlet extends HttpServlet {
|
||||
|
||||
private List<ViewResolver> viewResolvers;
|
||||
private List<HandlerAdapter> handlerAdapters;
|
||||
private Map<String, MappingRegistry> handlerMapping;
|
||||
|
||||
public DispatcherServlet() {
|
||||
WebApplicationContext resources = WebApplicationContext.getInstance();
|
||||
this.viewResolvers = resources.getViewResolvers();
|
||||
this.handlerMapping = resources.getHandlerMapping();
|
||||
this.handlerAdapters = resources.getHandlerAdapters();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html");
|
||||
|
||||
try {
|
||||
MappingRegistry handler = getHandler(request);
|
||||
if (handler != null) {
|
||||
HandlerAdapter handlerAdapter = getHandlerAdapter(handler);
|
||||
if (handlerAdapter != null) {
|
||||
String result = handlerAdapter.handle(request, response, handler);
|
||||
if (result != null) {
|
||||
render(request, response, result);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
request.getRequestDispatcher("/404.jsp").forward(request, response);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private MappingRegistry getHandler(HttpServletRequest request) {
|
||||
return handlerMapping.get(request.getRequestURI());
|
||||
}
|
||||
|
||||
private HandlerAdapter getHandlerAdapter(MappingRegistry handler) {
|
||||
for (HandlerAdapter handlerAdapter : handlerAdapters) {
|
||||
if (handlerAdapter.supports(handler.getHandler())) {
|
||||
return handlerAdapter;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void render(HttpServletRequest request, HttpServletResponse response, String result) throws Exception {
|
||||
View view = resolveViewName(result);
|
||||
if (view != null) {
|
||||
view.render(request, response, result);
|
||||
}
|
||||
}
|
||||
|
||||
private View resolveViewName(String path) throws Exception {
|
||||
|
||||
for (ViewResolver viewResolver : viewResolvers) {
|
||||
View view = viewResolver.resolveViewName(path);
|
||||
if (view != null) {
|
||||
return view;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface HandlerAdapter {
|
||||
|
||||
boolean supports(Object handler);
|
||||
|
||||
String handle(HttpServletRequest request, HttpServletResponse response, MappingRegistry handler);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class InternalResourceView implements View {
|
||||
@Override
|
||||
public void render(HttpServletRequest request, HttpServletResponse response, String path) {
|
||||
try {
|
||||
request.getRequestDispatcher(path).forward(request, response);
|
||||
} catch (ServletException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
public class InternalResourceViewResolver implements ViewResolver{
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String viewName) {
|
||||
return viewName.endsWith(".jsp") ? new InternalResourceView() : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class MappingRegistry {
|
||||
|
||||
private Object handler;
|
||||
private Method method;
|
||||
|
||||
public MappingRegistry() {
|
||||
}
|
||||
|
||||
public MappingRegistry(Object handler, Method method) {
|
||||
this.handler = handler;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public Object getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import com.example.dispatcherservlet.annotation.Controller;
|
||||
import com.example.dispatcherservlet.annotation.ResponseBody;
|
||||
|
||||
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 RequestMappingHandlerAdapter implements HandlerAdapter {
|
||||
|
||||
@Override
|
||||
public boolean supports(Object handler) {
|
||||
return handler.getClass().isAnnotationPresent(Controller.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String handle(HttpServletRequest request, HttpServletResponse response, MappingRegistry handler) {
|
||||
Object result = null;
|
||||
try {
|
||||
Method method = handler.getMethod();
|
||||
result = method.invoke(handler.getHandler());
|
||||
if (method.isAnnotationPresent(ResponseBody.class)) {
|
||||
if (result instanceof String) {
|
||||
response.setContentType("text/plain");
|
||||
response.getWriter().println(result);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
} catch (InvocationTargetException | IllegalAccessException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return (String) result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface View {
|
||||
|
||||
void render(HttpServletRequest request, HttpServletResponse response, String path);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
public interface ViewResolver {
|
||||
|
||||
View resolveViewName(String viewName) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.example.dispatcherservlet.springmvc;
|
||||
|
||||
import com.example.dispatcherservlet.annotation.Controller;
|
||||
import com.example.dispatcherservlet.annotation.RequestMapping;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
public class WebApplicationContext {
|
||||
|
||||
public static WebApplicationContext instance;
|
||||
|
||||
private final Map<String, MappingRegistry> handlerMapping = new HashMap<>();
|
||||
private final List<HandlerAdapter> handlerAdapters = new ArrayList<>();
|
||||
private final List<ViewResolver> viewResolvers = new ArrayList<>();
|
||||
|
||||
public static WebApplicationContext getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new WebApplicationContext();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private WebApplicationContext() {
|
||||
initResources();
|
||||
}
|
||||
|
||||
public Map<String, MappingRegistry> getHandlerMapping() {
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
public List<HandlerAdapter> getHandlerAdapters() {
|
||||
return handlerAdapters;
|
||||
}
|
||||
|
||||
public List<ViewResolver> getViewResolvers() {
|
||||
return viewResolvers;
|
||||
}
|
||||
|
||||
private void initResources() {
|
||||
String packageName = "com.example.dispatcherservlet";
|
||||
Set<Class<?>> classes = new HashSet<>();
|
||||
setClasses(classes, packageName);
|
||||
|
||||
for (Class<?> aClass : classes) {
|
||||
try {
|
||||
Object instance = aClass.getConstructor().newInstance();
|
||||
if (instance instanceof HandlerAdapter) {
|
||||
handlerAdapters.add((HandlerAdapter) instance);
|
||||
}
|
||||
if (instance instanceof ViewResolver) {
|
||||
viewResolvers.add((ViewResolver) instance);
|
||||
}
|
||||
if (aClass.isAnnotationPresent(Controller.class)) {
|
||||
Method[] declaredMethods = aClass.getDeclaredMethods();
|
||||
for (Method declaredMethod : declaredMethods) {
|
||||
if (declaredMethod.isAnnotationPresent(RequestMapping.class)) {
|
||||
RequestMapping annotation = declaredMethod.getAnnotation(RequestMapping.class);
|
||||
handlerMapping.put(annotation.value(), new MappingRegistry(instance, declaredMethod));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setClasses(Set<Class<?>> classes, String packageName) {
|
||||
String directoryString = getDirectoryString(packageName);
|
||||
File directory = new File(directoryString);
|
||||
if(directory.exists()){
|
||||
String[] files = directory.list();
|
||||
for(String fileName : files){
|
||||
if (isExcluded(fileName)) continue;
|
||||
if (fileName.endsWith(".class")) {
|
||||
fileName = fileName.substring(0, fileName.length() - 6);
|
||||
try{
|
||||
Class<?> c = Class.forName(packageName + "." + fileName);
|
||||
if (!c.isInterface()) {
|
||||
classes.add(c);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
setClasses(classes, packageName + "." + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isExcluded(String fileName) {
|
||||
String[] exclude = {"WebApplicationContext", "MyDispatcherServlet"};
|
||||
for (String s : exclude) {
|
||||
if (fileName.contains(s)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getDirectoryString(String packageName) {
|
||||
String packageNameSlashed =
|
||||
"./" + packageName.replace(".", "/");
|
||||
URL packageDirURL =
|
||||
Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
|
||||
return packageDirURL.getFile();
|
||||
}
|
||||
}
|
||||
10
blog/dispatcher-servlet/src/main/webapp/404.jsp
Normal file
10
blog/dispatcher-servlet/src/main/webapp/404.jsp
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>404 Page</h1>
|
||||
</body>
|
||||
</html>
|
||||
6
blog/dispatcher-servlet/src/main/webapp/WEB-INF/web.xml
Normal file
6
blog/dispatcher-servlet/src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?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">
|
||||
</web-app>
|
||||
13
blog/dispatcher-servlet/src/main/webapp/index.jsp
Normal file
13
blog/dispatcher-servlet/src/main/webapp/index.jsp
Normal file
@@ -0,0 +1,13 @@
|
||||
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>JSP - Hello World</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= "Hello World!" %>
|
||||
</h1>
|
||||
<br/>
|
||||
<a href="hello-servlet">Hello Servlet</a>
|
||||
</body>
|
||||
</html>
|
||||
10
blog/dispatcher-servlet/src/main/webapp/main.jsp
Normal file
10
blog/dispatcher-servlet/src/main/webapp/main.jsp
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Main!!</h1>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user