2017-09-03 59 views
0

我正在編寫一個使用多個JSP文件的Java EE WebApp。到現在爲止,我用它來寫這樣的地址:轉發到JSP的Servlet

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

但我更喜歡他們會是這樣的:

http://www.example.com/login 

所以我做了每個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,...) 
} 

它的工作原理perfecty,但我的問題是:是否有任何其他方式做到這一點,而無需創建爲每個JSP一個新的Servlet?編寫一個JSP並不是很快,然後爲它編寫Servlet ...

謝謝!

回答

2

如果你只需要映射的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> 
+0

謝謝!我認爲這很容易! :) – Ommadawn