|
14 | 14 |
|
15 | 15 | @WebServlet(name = "dispatcher", urlPatterns = "/", loadOnStartup = 1) |
16 | 16 | public class DispatcherServlet extends HttpServlet { |
17 | | - private static final long serialVersionUID = 1L; |
18 | | - private static final Logger logger = LoggerFactory.getLogger(DispatcherServlet.class); |
19 | | - private static final String DEFAULT_REDIRECT_PREFIX = "redirect:"; |
20 | | - |
21 | | - private RequestMapping rm; |
22 | | - |
23 | | - @Override |
24 | | - public void init() throws ServletException { |
25 | | - rm = new RequestMapping(); |
26 | | - rm.initMapping(); |
27 | | - } |
28 | | - |
29 | | - @Override |
30 | | - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
31 | | - String requestUri = req.getRequestURI(); |
32 | | - logger.debug("Method : {}, Request URI : {}", req.getMethod(), requestUri); |
33 | | - |
34 | | - Controller controller = rm.findController(requestUri); |
35 | | - try { |
36 | | - String viewName = controller.execute(req, resp); |
37 | | - move(viewName, req, resp); |
38 | | - } catch (Throwable e) { |
39 | | - logger.error("Exception : {}", e); |
40 | | - throw new ServletException(e.getMessage()); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - private void move(String viewName, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
45 | | - if (viewName.startsWith(DEFAULT_REDIRECT_PREFIX)) { |
46 | | - resp.sendRedirect(viewName.substring(DEFAULT_REDIRECT_PREFIX.length())); |
47 | | - return; |
48 | | - } |
49 | | - |
50 | | - RequestDispatcher rd = req.getRequestDispatcher(viewName); |
51 | | - rd.forward(req, resp); |
52 | | - } |
| 17 | + private static final long serialVersionUID = 1L; |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(DispatcherServlet.class); |
| 19 | + private static final String DEFAULT_REDIRECT_PREFIX = "redirect:"; |
| 20 | + |
| 21 | + private RequestMapping rm; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void init() throws ServletException { |
| 25 | + rm = new RequestMapping(); |
| 26 | + rm.initMapping(); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 31 | + String requestUri = req.getRequestURI(); |
| 32 | + logger.debug("Method : {}, Request URI : {}", req.getMethod(), requestUri); |
| 33 | + |
| 34 | + Controller controller = rm.findController(requestUri); |
| 35 | + try { |
| 36 | + String viewName = controller.execute(req, resp); |
| 37 | + move(viewName, req, resp); |
| 38 | + } catch (Throwable e) { |
| 39 | + logger.error("Exception : {}", e); |
| 40 | + throw new ServletException(e.getMessage()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private void move(String viewName, HttpServletRequest req, HttpServletResponse resp) |
| 45 | + throws ServletException, IOException { |
| 46 | + if (viewName.startsWith(DEFAULT_REDIRECT_PREFIX)) { |
| 47 | + resp.sendRedirect(viewName.substring(DEFAULT_REDIRECT_PREFIX.length())); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + RequestDispatcher rd = req.getRequestDispatcher(viewName); |
| 52 | + rd.forward(req, resp); |
| 53 | + } |
53 | 54 | } |
0 commit comments