2012-08-29 155 views
3

通過引入Servlet 3.0,我們可以使用annotations將servlet映射到URL模式,並在web.xml中省略映射。JSP servlet映射

我不知道是否有一些intstructions或特殊標籤允許JSP映射在頁面代碼URL,而不在web.xml

回答

5

聲明的servlet有這樣的無工廠。

最好的你可以做的是隱藏在/WEB-INF的JSP(以便它不能直接通過URL請求)和剛剛創建,其轉發給JSP servlet並最終繪製它所需的URL模式。這也很容易:

@WebServlet("/foo") 
public class FooServlet extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     request.getRequestDispatcher("/WEB-INF/foo.jsp").forward(request, response); 
    } 

} 

這樣的JSP中/WEB-INF/foo.jsp可通過http://localhost:8080/context/foo。您可以使用front controller pattern將它進一步抽象爲一個JSP的一個servlet。