简体   繁体   English

转发到JSP的Servlet

[英]Servlets that forward to JSPs

I'm coding a Java EE WebApp which uses several JSP files. 我正在编写一个使用多个JSP文件的Java EE WebApp。 Until now, I use to write the addresses like this: 到现在为止,我用来写这样的地址:

http://www.example.com/login.jsp

But I prefer they would be like: 但我更喜欢他们会像:

http://www.example.com/login

So I made a Servlet for each JSP file, and they that look like this: 所以我为每个JSP文件创建了一个Servlet,它们看起来像这样:

public class ForwardLoginServlet extends HttpServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
                     throws ServletException, IOException {

        String url = context.getInitParameter("loginURL"); // this will return the login.jsp filename
        forwardToURL(url, request, response);
    }
    // More stuff here (doget, do post,...)
}

It works perfecty, but my question is: is there any another way to do this without creating a new Servlet for every JSP? 它工作得很完美,但我的问题是:有没有其他方法可以做到这一点,而无需为每个JSP创建一个新的Servlet? It's not very fast to write a JSP an then write the Servlet for it... 编写JSP然后为它编写Servlet并不是很快......

Thanks! 谢谢!

If you only need to map url to jsp then you can specify it in web.xml 如果您只需要将url映射到jsp,则可以在web.xml中指定它

  <servlet>
    <servlet-name>login</servlet-name>
    <jsp-file>/WEB-INF/views/login.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

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

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