如何同时支持 index.html 和“/”Servlet 映射
How to Support both index.html and "/" Servlet Mapping
我的 Java 网络应用程序的根目录中有以下两个 servlet 和一个 index.html 页面。 index.html 页面收集一些数据,使用 Insert servlet 将其插入,然后为用户提供 URL 以检索数据(即浏览器中的 http://localhost:8080/12345). I would like the user to be able to put http://localhost:8080/12345 并调用 Retrieve servlet。
现在发生的事情是当我输入 http://localhost:8080 or http://localhost:8080/ the Retrieve servlet gets called (it's mapped to "/" in the web.xml). I would only like to call the Retrieve servlet when http://localhost:8080/some_data_here 被请求时。对如何修改 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>
如你所说,不将 Retrieve
servlet 映射到 /
,而是映射到 /12345
,然后将 Insert
servlet 之后的请求重定向到 /12345
.
我的 Java 网络应用程序的根目录中有以下两个 servlet 和一个 index.html 页面。 index.html 页面收集一些数据,使用 Insert servlet 将其插入,然后为用户提供 URL 以检索数据(即浏览器中的 http://localhost:8080/12345). I would like the user to be able to put http://localhost:8080/12345 并调用 Retrieve servlet。
现在发生的事情是当我输入 http://localhost:8080 or http://localhost:8080/ the Retrieve servlet gets called (it's mapped to "/" in the web.xml). I would only like to call the Retrieve servlet when http://localhost:8080/some_data_here 被请求时。对如何修改 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>
如你所说,不将 Retrieve
servlet 映射到 /
,而是映射到 /12345
,然后将 Insert
servlet 之后的请求重定向到 /12345
.