2017-10-16 73 views
0

我在Java Web應用程序的根目錄下有兩個servlet和一個index.html頁面。 index.html頁面收集一些數據,使用插入servlet插入它,然後爲用戶提供一個URL來檢索數據(即http://localhost:8080/12345)。我希望用戶能夠將http://localhost:8080/12345放在瀏覽器中,並讓Retrieve servlet被調用。如何同時支持index.html和「/」Servlet映射

現在發生的情況是,當我輸入http://localhost:8080http://localhost:8080/時,Retrieve servlet被調用(它被映射到web.xml中的「/」)。我只想在請求http://localhost:8080/some_data_here時調用Retrieve servlet。任何想法如何修改servlet映射以支持這些要求?

的index.html

<html> 
    <body> 
     <form action = "insert" method = "POST"> 
     Enter Data: <input type = "text" name = "data"> 
     <br /> 
     <input type = "submit" value = "Submit" /> 
     </form> 
    </body> 
</html> 

WEB.XML

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
    version="2.4"> 
    <servlet> 
     <servlet-name>Insert</servlet-name> 
     <servlet-class>com.servlets.Insert</servlet-class> 
     <load-on-startup>-1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Insert</servlet-name> 
     <url-pattern>/insert</url-pattern> 
    </servlet-mapping> 
    <servlet> 
     <servlet-name>Retrieve</servlet-name> 
     <servlet-class>com.servlets.Retrieve</servlet-class> 
     <load-on-startup>-1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Retrieve</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
</web-app> 

回答

1

怎麼樣的Retrieve servlet作爲你說的不映射到//12345,然後重定向後的請求Insert servlet到/12345

+0

您將如何映射到/ USER-ENTERED-DATA,因爲12345是基於用戶輸入的數據庫ID。我需要留在index.html頁面上並顯示http:// localhost:8080/USER-ENTERED-DATA(即/ 12345) – c12

+0

如果您使用相同的路徑('/')執行您想要的不同操作然後你必須建立一個額外的servlet /過濾器,分析URL,然後決定調用哪個servlet(例如,通過將請求轉發到另一個路徑('Retrieve')servlet正在監聽)。 –