JavaWeb frameless, by using sophisticated design pattern reflecting the discharge micro-channel chat page PC

Imitating a week beginning of the write side of the micro channel PC chat page, no use ssh, ssm like framework, the JavaWeb, reflection, MySQL, C3P0 technology. Here the core technology and which listed Please advise.

The project differs from traditional JavaWeb

Traditional JavaWeb project every request to write a corresponding Servlet, this will result in a need to write down the completion dozens Servlet, and using the principle of reflection (based on the acquired url-pattern, taken out of the corresponding method name, and further call the appropriate method) can be a good solution to this problem.

1, the configuration file web.xml

Use Value set to the packet, user requests will call UserServlet


  <servlet>
    <servlet-name>UserServlet</servlet-name>
    <servlet-class>com.hsy.action.UserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserServlet</servlet-name>
    <url-pattern>/user</url-pattern>
  </servlet-mapping>

2, create tools BaseServlet

This class is the technical core of the entire project by getParameter ( "method") and getMethod (methodName, HttpServletRequest.class, HttpServletResponse.class) to determine the method to be accessed. By Class c = this.getClass (); to get the class invoked the current method, and finally performed by the corresponding invoke (this, req, res) method


public class BaseServlet extends HttpServlet {
    /*
     * 它会根据请求中的m,来决定调用本类的哪个方法
     */
    protected void service(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=utf-8");

        // 例如:http://localhost:8080/demo1/xxx?method=login
        String methodName = req.getParameter("method");// 它是一个方法名称

        // 当没用指定要调用的方法时,那么默认请求的是execute()方法。
        if(methodName == null || methodName.isEmpty()) {
            methodName = "execute";
        }
        Class c = this.getClass();
        try {
            // 通过方法名称获取方法的反射对象
            Method m = c.getMethod(methodName, HttpServletRequest.class,
                    HttpServletResponse.class);
            // 反射方法目标方法,也就是说,如果methodName为add,那么就调用add方法。
            String result = (String) m.invoke(this, req, res);
            // 通过返回值完成请求转发
            if(result != null && !result.isEmpty()) {
                req.getRequestDispatcher(result).forward(req, res);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
}

3. Create UserServlet and inherited tools BaseServlet

Here it is just a simple example to illustrate this with one of his works. Suppose a request http: // localhost:? After 8080 / demo1 / user method = login come, first find the following UserServlet according to web.xml. A fierce look might be a little ignorant force (which went TM can execute login here ??? This is not the way to do it nonsense). Do not worry, listen to me slowly come. This UserServlet inherited tools BaseServlet, step by reflection on technology we can not perform the login method in this class yet? I feel a bit wrong? now it's right! ! ! I visited this class even if it is inherited BaseServlet method will automatically perform the method in the parent class do? Under normal circumstances it is not enough to call the constructor of the parent class constructor even when ah also instantiable subclass. But here it is possible, because it will automatically call the server method in Servlet life cycle approach, which means that this class will first call the server method, then it will automatically call the server method of the parent class. This tool implements a subclass of class BaseServlet a total of more use to call different methods (many now realize the underlying framework is one such).


public class UserServlet extends BaseServlet {
    public String login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("UserServlet中的login方法调用了");
        return null;
    }
}

Guess you like

Origin www.cnblogs.com/shaoyu/p/11261235.html