我是Java servlet的新手。
如果这个问题看起来很愚蠢,请原谅我。
我在web.xml servlet选项卡中使用netbeans添加了servlet映射,例如servlet类和url模式,但是当应用程序运行时,它不会直接进入目标URL,即http:// localhost:8080 / HelloDuke2 / greeting但 http :// localhost:8080 / HelloDuke2 /
我在配置中缺少什么?
如何将启动URL设置为http:// localhost:8080 / HelloDuke2 / greeting?
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>GreetingDukeServlet</servlet-name> <servlet-class>HelloDuke.GreetingDukeServlet</servlet-class> </servlet> <servlet> <servlet-name>ResponseDukeServlet</servlet-name> <servlet-class>HelloDuke.ResponseDukeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ResponseDukeServlet</servlet-name> <url-pattern>/ResponseDukeServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GreetingDukeServlet</servlet-name> <url-pattern>/GreetingDukeServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>
请帮忙。
谢谢。
编辑:
我尝试了Alexey Sviridov提供的解决方案,但是在浏览器报告HTTP状态404资源不可用的情况下,它不起作用。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>GreetingDukeServlet</servlet-name> <servlet-class>HelloDuke.GreetingDukeServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>ResponseDukeServlet</servlet-name> <servlet-class>HelloDuke.ResponseDukeServlet</servlet-class> <init-param> <param-name>Message</param-name> <param-value>Hello, PeterWkc</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>GreetingDukeServlet</servlet-name> <url-pattern>/GreetingDukeServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ResponseDukeServlet</servlet-name> <url-pattern>/ResponseDukeServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> </web-app>
index.xhtml
> <?xml version='1.0' encoding='UTF-8' > ?> <!DOCTYPE html PUBLIC "-//W3C//DTD > XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > <html > xmlns="http://www.w3.org/1999/xhtml" > xmlns:h="http://java.sun.com/jsf/html"> > <h:head> > <title>Hello Duke</title> > <meta http-equiv="REFRESH" content="0; url=GreetingDukeServlet/"> > > </meta> > > > </h:head> > <h:body> > Hello from Facelets > > > > </h:body> </html>
我看到了Facelets页面,并且看到了特定于JSF的页面<context-param>,但是我没有看到JSF FacesServlet的定义web.xml,但是您在摆弄其他servlet。
<context-param>
FacesServlet
您不混合基本概念/技术吗?诚然,Java EE6教程中缺少 JSP教程,但是要使用纯香草servlet,通常应使用纯HTML或JSP而不是Facelets。
无论如何,要通过URL调用servlet,您需要确保URL与<url-pattern>servlet的匹配(如中所定义)web.xml。您已定义GreetingDukeServlet要侦听匹配的URL /GreetingDukeServlet。因此,URL必须改为http:// localhost:8080 / HelloDuke2 / GreetingDukeServlet。
<url-pattern>
GreetingDukeServlet
/GreetingDukeServlet
如果您实际上希望将该URL改为http:// localhost:8080 / HelloDuke2 / greeting,则应<url- pattern>将/greeting改为。
<url- pattern>
/greeting