JSP servlet映射

Odo*_*ois 3 jsp url-mapping servlet-3.0 java-ee-6

通过引入Servlet 3.0,我们可以使用注释将servlet映射到URL模式,并在web.xml中省略映射.

我想知道是否有一些intstructions或特殊标签允许将jsp映射到页面代码中的URL而不在web.xml中声明servlet

Bal*_*usC 5

没有这样的设施.

最好的做法是隐藏JSP /WEB-INF(以便它永远不会被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);
    }

}
Run Code Online (Sandbox Code Playgroud)

这样JSP /WEB-INF/foo.jsp就可以使用了http://localhost:8080/context/foo.您可以使用前端控制器模式将其进一步抽象为一组使用前端控制器模式的 servlet .