File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes not able to set the url pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "not able to set the url pattern" Watch "not able to set the url pattern" New topic
Author

not able to set the url pattern

Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi
As in HFJS book chapter 3, im trying set the <url-pattern> to something like 'SelectBeer.do" . But i am not able to set the url-pattern to anything except the actual servlet class name. When i run the web app(the same web app described in chapter 3, the beer advisor web app) after i click the submit button it goes to the url which ends with the servletclassname. i.e. http://localhost:8080/BeerV1/BeerSelect. i changed the action atttribute value in the form.html to 'selectbeer.do'.see my web.xml also.

please suggest me what is wrong here.

Thanks,
Teena
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

Hi,

Can you post the form.html content too

Regards,
Ranil


SCJP 1.4 (80%) , SCWCD5.0 (91%) , SCEA / OCMEA
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Thanks for your response. I think you can help me out since you are SCWCD! so you might have tried the same example in chapter3.
Hmm..
right now i can't as im from a different place now. but i can assure you that i copied the same form.html code from the book.(Chapter 3 HFSJ)
is it dependant on the SDK version? i have netbeans 5.5.1 which takes jdk1.4, not 1.5. is this a reason?
Ok, i remember it..
the html starting from <form>...

<form method="POST" action="selectbeer.do">
Select Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
</select>
</form>
</body>
</html>

Hope i provided you some details
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Teena George:
i have netbeans 5.5.1 which takes jdk1.4, not 1.5. is this a reason?
Ok, i remember it..
the html starting from <form>...

<form method="POST" action="selectbeer.do">
Select Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
</select>
</form>
</body>
</html>



1.netbeans5.5 support jdk1.5

2.where is the submit button for your form?

3.if you want to try HeadFirst Concept try with simple program.
whether headfirst explain in struts?
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

Hi Teena,

It looks like to me that the code written for the form.html is not complete. As it is pointed out you got to place the submit button too. If you have not placed that in the actual code that you used in testing please do so first and retry again.Assuming that you have a submit button at the right place in the code of form.html the request which would be generated by the browser has to point to the following url, http://yourserverip ort/approot/beerselect.do.

And the web container first look for an exact match , folder match and then extension match in the web.xml file. In this case it seems to me that there is an exact match and extension match too. exact match takes the precedence over extension match then. Hence your application must be mapped to /beerselect.do , which in turn maps it self to BeerSelect servlet.

Please do try the example again and keep posted the updates.


Hope this helps

Regards,
Ranil
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Oh, i included the submit button in form.html, but i forgot to add that while posting. The submit button is placed correctly. the problem is when i click on the submit button it goes to the /BeerSelect servlet not the beerselect.do(which is again the servlet). i dont know why it checks for the servlet name instead of the url-pattern i gave. apart from web.xml and the form.html, are there any places where i can change this? i dont understand the exact mach and extension match concept. is it because i used the same name for the servlet and the url-pattern? is it case sensitive?
Rufus Addis
Ranch Hand

Joined: Aug 21, 2008
Posts: 83
I'm a little confused myself - can you clarify what you are expecting to happen, and what is actually happening!

In your web.xml (I presume this is the one in the <tomcat-path>\webapps\Beer-v1\WEB-INF\) you have the following:


So anything posted to /selectbeer.do will be matched against the servlet-name BeerSelect which in turn is mapped to the servlet com.example.web.BeerSelect (i.e. <tomcat-path>\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.class).

So the other BeerSelect servlet you mention in your posting - how is this defined and where is it? Why have two servlets with the same name??

The other thing worth checking is caching issues - both the browser and the container may be caching previous versions of "faulty" code.

View the source of your form in the browser and make sure it's as you expect - if not try a Ctrl F5 (assuming IE). Try restarting your container to make sure it's picking up any changes you make to web.xml etc.

HTH - Rufus.
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

Hi Teena ,

Yes , I would also suggest you to clear the browser cache and also the stuff in the tomcat's work folder.

Regards,
Ranil
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi,
Thank you all for the responses. the problem was in using the same name for the servlet and for the url pattern. when i changed the url-pattern to another unrelted name, it worked.

Thank you again,
Teena
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

Hi Teena,

I would like to point that using the same name for both url-pattern , servlet class would not make any issue. For example you could have

<servlet>
<servlet-name>BeerSelect</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>BeerSelect</servlet-name>
<url-pattern>/selectbeer.do</url-pattern>
</servlet-mapping>

in web.xml file and it works. (I guess you are talking about servlet-name being the same as the servlet class name )

also following mappings are working as well

<servlet>
<servlet-name>BeerSelect</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>BeerSelect</servlet-name>
<url-pattern>/BeerSelect.do</url-pattern>
</servlet-mapping>

Hope this is helpful!

Regards,
Ranil
[ October 08, 2008: Message edited by: Ranil Liyana Arachchige ]
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi Ranil,
Actually my problem was with the second way. when i used



the url in the address bar was ../BeerSelect not the expected ../beerselect.do
i doubt this is the expected result as you suggested(exact match and extension match)
see that i used full lower case letters for the urlpattern.
will that effect the result?

Thanks,
Teena
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

Hi Teena,

Yes , it effects. The url that you are trying to access must resolve to an available url mapping in web.xml. so that if you want to access /selectbeer.do , then the form must be submitted to "selectbeer.do" not "SelectBeer.do". You may be able to get a good understand on URL mapping rules by referring to HFSJ 2nd Edition , page 616 onwards.

hope that helps you

Regards,
Ranil
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi Ranil,
Now everything works fine.
these are the scenarios i tried:

Everything works fine. as far as i know, the url-pattern in web.xml and the value of the action attribute in the form.html should be the same including letter casing.

Thanks again,
Teena
Ranil Liyana Arachchige
Ranch Hand

Joined: Sep 20, 2008
Posts: 71

great !!! good luck with the work
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not able to set the url pattern