开发者

Java Servlet URL Mapping

i fairly new to java servlet.

Please forgive me if this question seems stupid.

I have add servlet mapping such as servlet class and url pattern using netbeans in web.xml servlet tab but when the application was running it does not direct go into the target url which is http://localhost:8080/HelloDuke2/greeting but http://localhost:8080/HelloDuke2/

What am i missing in the configuration ?

How to set the start up url to 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>

Please help.

Thanks.

EDIT:

I have tried the solution provided by Alexey Sviridov but it doesn't works where the browser report http status 404 resource is not available.

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>ResponseDukeServ开发者_如何转开发let</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>

Please help.

Thanks.


I see a Facelets page and I see a JSF specific <context-param>, but I don't see the JSF FacesServlet being definied in web.xml, yet you're fiddling with other servlets.

Aren't you mixing the basic concepts/technologies? Admittedly, the JSP tutorial is missing in Java EE 6 tutorial, but to work with plain vanilla servlets, you'd usually use plain HTML or JSP instead of Facelets.

Anyway, to invoke a servlet by URL, you need to ensure that the URL matches the <url-pattern> of the servlet as is been definied in the web.xml. You've definied your GreetingDukeServlet to listen on URLs matching /GreetingDukeServlet. So the URL has to be http://localhost:8080/HelloDuke2/GreetingDukeServlet instead.

If you actually want the URL to be http://localhost:8080/HelloDuke2/greeting instead, then you should change the <url-pattern> to /greeting instead.

See also:

  • Servlets info page - Contains a little hello world


you need to define welcome file list. see http://download.oracle.com/javaee/1.4/tutorial/doc/WebApp4.html

http://wiki.apache.org/tomcat/HowTo#How_do_I_override_the_default_home_page_loaded_by_Tomcat.3F


Yes, you need define index page for your web app and you can make redirect inside f.e. create page index.html with this contect

<html><head><meta http-equiv="REFRESH" content="0; url=greeting/"></head></html>

and then in web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜