Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in servlet

 
preetiarvind sharma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
preetiarvind sharma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel,

I ll try that now

Preeti
 
preetiarvind sharma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it worked.
Thank you all for the help


preeti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic