Java Servlet中是否有任何注释?

时间:2016-09-30 06:12:15

标签: java servlets annotations request-mapping

民间,

我正在使用基于Java Servlet 的Web应用程序,并使用Request URI 多个客户端请求处理到一个servlet中使用条件语句来应用逻辑

ControlServlet.java

public class ControlServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
            HttpSession session = request.getSession(true);
            String strRequestURI = request.getRequestURI();
            try{
            if(strRequestURI.contains("/one")){
                //Logic
            }
            if(strRequestURI.contains("/two")){
                //Logic
            }
            if(strRequestURI.contains("/three")){
                //Logic
            }
            if(strRequestURI.contains("/four")){
                //Logic
            }

        }
        catch(ServletException | IOException ex){
            logs.errorLog(ex);
            }
        catch(Exception ex){
            logs.errorLog(ex);
        }
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
    <servlet>
        <servlet-name>ControlServlet</servlet-name>
        <servlet-class>com.main.ControlServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ControlServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

F_one.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="POST" action="one.do" >
            <!--First Form-->
        </form>
    </body>
</html>

F_two.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="POST" action="two.do" >
            <!--Second Form-->
        </form>
    </body>
</html>

我的问题是,我想避免使用条件语句检查每个请求。我可以使用case(switch)但我正在寻找基于 Annotation 的请求映射到Java Servlet,以便我可以同时处理 doGet() doPost() 方法,无需再次覆盖它。 如果有人知道,请让我知道模板,以便我可以更新我的项目。

提前致谢!!

0 个答案:

没有答案
相关问题