简体   繁体   English

如何设置JSP页面的URL?

[英]How can I set the URL for a JSP page?

In a servlet I would just do 在servlet中我会这样做

@WebServlet("/myURL")

But how would I do that with a JSP page? 但是我如何使用JSP页面呢?

Just like any servlet, you can map a particular URL-pattern to a JSP. 就像任何servlet一样,您可以将特定的URL模式映射到JSP。

Simply add this snippet in your deployment descriptor 只需在部署描述符中添加此代码段即可

<servlet>
    <servlet-name>fooBar</servlet-name>
    <jsp-file>/foo.jsp</jsp-file> <!-- Your JSP. Must begin with '/' -->
</servlet>

<servlet-mapping>
    <servlet-name>fooBar</servlet-name>
    <url-pattern>/bar</url-pattern> <!-- Any URL you want here -->
</servlet-mapping>

There is no facility to have annotations inside the JSP so if you don't want to make an entry inside the web.xml and work purely with annotations, you have a work around to make a sevlet that simply forwards the RequestDispatcher to the JSP and you can annotate this servlet with the URL that you want. 在JSP中没有设备可以注释,所以如果你不想在web.xml中创建一个条目并且纯粹使用注释工作,那么你就可以创建一个简单地将RequestDispatcher转发到JSP的sevlet。您可以使用所需的URL来注释此servlet。

@WebServlet("/bar") //your URL pattern
public class DummyServlet extends HttpServlet {

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM