wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in servlet" Watch "Doubt in servlet" New topic
Author

Doubt in servlet

preetiarvind sharma
Greenhorn

Joined: Nov 17, 2005
Posts: 22
Hi All,
I m trying to develop a really basic servlet. My application structure goes like this.

test - root
webapps - test - WEB-INF - web.xml
webapps- test - WEB-INF - test.jsp
webapps - test - classes - servlets - TestServlet.class
webapps - test - classes - beans - TestBean.class

Here I cant call my servlet directly from the Address basr using this URL
http://localhost:8080/test/TestMe

What wrong am i doing here??
Pls help.

here is the copy of my web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>servlets.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>test/Testme</url-pattern>
</servlet-mapping>
</web-app>


Regards,

Preeti
Raghunath Nandyala
Ranch Hand

Joined: Jan 24, 2006
Posts: 64
<url-pattern>test/Testme</url-pattern>

change to

<url-pattern>/test/Testme</url-pattern>

and try this...


and one more

webapps- test - WEB-INF - test.jsp
to
webapps- test - test.jsp

place all jsps file under test


SCBCD,SCWCD,SCJP
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

webapps - test - WEB-INF - web.xml


Correct


webapps- test - WEB-INF - test.jsp


Wrong.(This is correct but can't be accessed directly.) Correct it to
webapps- test - test.jsp.


webapps - test - classes - servlets - TestServlet.class


Wrong. Correct it
webapps - test - WEB-INF - classes - servlets - TestServlet.class


webapps - test - classes - beans - TestBean.class


Wrong. Correct it(put all classes in one package)
webapps - test - WEB-INF - classes - servlets - TestBean.class


Here I cant call my servlet directly from the Address basr using this URL
http://localhost:8080/test/TestMe


Correct the URL to
http://localhost:8080/MyWebApplication/test/TestMe


<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>test/Testme</url-pattern>
</servlet-mapping>


url-pattern element is Wrong. Correct it to

<url-pattern>/test/Testme</url-pattern>

Note: I had a slash in front of test.
[ February 08, 2006: Message edited by: Vishnu Prakash ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
preetiarvind sharma
Greenhorn

Joined: Nov 17, 2005
Posts: 22
Thanks Vishnu and RaghuNath for ur replies.
First of all aplogies for the little mistake in the file structure i have provided initially

my jsp is actually in 'test' and not WEB-INF .. so its test/test.jsp
and classes are in the WEB-INF directory as separate folders for beans and servlets.

Vishnu - i have tried /test/TestMe . It didnt help.
do i have to give the package name (servlets) since servlet in inside a pkg. Btw i have tried that, i dont think I am using the right URL?

RaghuNath - I wanted to keeps my beans separate from the servlets, thats why I have created two packages inside the classes. I dont think thats wrong?? Your suggestions please.


Regards
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi preetiarvind -

if you look closely at what Vishnu wrote, you'll see that you need to put your class files inside a directory called classes within WEB-INF. This classes directory is part of the classpath, so what packages you put inside there will be picked up by the servlet container. If you wish to have your own package structure inside this, go ahead.


Vishnu - i have tried /test/TestMe . It didnt help.
do i have to give the package name (servlets) since servlet in inside a pkg. Btw i have tried that, i dont think I am using the right URL?


your url-pattern needs to start with a slash as pointed out. However, it need not reflect your physical package structure - the whole point is that it's declarative, so that you could change the back-end package structure without affecting the front-end.

I Assuming you have tomcat running on port 8080, think you should be able to get to your servlet with http://localhost:8080/test/test/Testme

http://localhost:8080/test

should get you to the root of your webapp... then you specified that you wanted the url /test/Testme to map to the servlet, so...

Hope that helps,
preetiarvind sharma
Greenhorn

Joined: Nov 17, 2005
Posts: 22
Thanks Daniel,

I ll try that now

Preeti
preetiarvind sharma
Greenhorn

Joined: Nov 17, 2005
Posts: 22
it worked.
Thank you all for the help


preeti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in servlet