0

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>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>

Please help.

Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
nicholas
  • 2,581
  • 14
  • 66
  • 104

3 Answers3

2

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I using netbeans 6.9.1 to create the Java EE project where when choosing the framework, the default is Facelets page. I might require to change to jsp page. What is the different between facelet and plain jsp ? Actually, the correct url pattern is /GreetingDukeServlet/. I have change that. – nicholas Mar 07 '11 at 05:42
  • How to change from facelet to jsp ? – nicholas Mar 07 '11 at 06:06
  • How you can identify i using facelet rather than jsp page ? – nicholas Mar 07 '11 at 06:17
  • How to define JSF FaceServlet in web.xml ? – nicholas Mar 07 '11 at 07:18
  • What do you want to learn? JSP/Servlets or Facelets/JSF? You might want to get yourself through [this answer](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp/2097732#2097732) first. – BalusC Mar 07 '11 at 11:25
  • Currently, i would like to know the difference between the two ? – nicholas Mar 07 '11 at 11:45
  • Facelets is successor of JSP. Facelets is XML based whereas JSP is "proprietary". Facelets has much better templating capabilities. If you want to develop with JSF, definitely use Facelets. It's so much better than JSP. If you want to develop with "plain" HTML/servlets, then JSP is easier to use. Right now it's unclear what you want because you're mixing the technologies/concepts. If you want to learn JSF 2.0, check [this tutorial](http://balusc.blogspot.com/2011/01/jsf-20-tutorial-with-eclipse-and.html). It only uses Eclipse instead of Netbeans (just because I don't use Netbeans). – BalusC Mar 07 '11 at 11:48
1

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>
Alexey Sviridov
  • 3,360
  • 28
  • 33
  • Thanks for your information. I using xhtml file and whether the tag is the same. – nicholas Mar 05 '11 at 08:39
  • @peterwkc - yes, xhtml has a same tags covered by xml header. BTW if my answer is useful you may vote for it :) – Alexey Sviridov Mar 05 '11 at 10:01
  • I have vote it. I have a question where why the hello2 example in the Java EE 5 does not require this meta tag but i do need it. – nicholas Mar 07 '11 at 06:15
  • It's not work because use use JSP tags but may be it's not rendered by Jasper. Checkout source code of your start page in browser. If you'll see tags then i'm right. Just try to use plain html to redirect - completely how i'm wrote in answer. Or yor browser redirect, but unknown page? I'm sorry for i'm ask you to vote :). – Alexey Sviridov Mar 07 '11 at 11:26
  • 404 may appear because you must point redirect (content="0; url=greeting/") to actual your url that already works. JEE server itself doesn't actual redirect at all - you must do it by yourself. – Alexey Sviridov Mar 07 '11 at 11:38
  • I not really understand what you saying in the last two comments. Can you explain one more time ? – nicholas Mar 08 '11 at 03:37
  • Sorry, let's again. There is two major url-rleted things. First - is your web-app context name (by default same as your application name) and then mappings (servlet or filter). So if your web app called 'greet.war' then it (by default) sitting on http://localhost:8080/greet/ – Alexey Sviridov Mar 09 '11 at 03:15
  • So if you define your welcome-file, index.xhtml for example, then when you point your browser to http://localhost:8080/greet/ server actually return content of http://localhost:8080/greet/index.html. Now let's take a look on your servlet mappings. If you define GreetingDukeServlet /GreetingDukeServlet then acually your servlet sitting on localhost:8080/greet/GreetingDukeServlet – Alexey Sviridov Mar 09 '11 at 03:29
  • and now clue it together. In your index.xhtml you define redirect () then if you point browser to localhost:8080/greet/ then loaded your index.xhtml and redirect you to localhost:8080/greet/GreetingDukeServlet/ where your servlet sitting. – Alexey Sviridov Mar 09 '11 at 03:35
  • if you need more explain, write to me sviridov.alexey(at)gmail.com – Alexey Sviridov Mar 09 '11 at 03:37