This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Deployment

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Everyone,

I am having a problem with deploying my servlet.This is my first servlet example.Can anyone help me out.

my java source code is

package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloWorldServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
PrintWriter out = res.getWriter();

System.out.println("Hello, world!");
//out.close();
}
}



My web.xml is as follows

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Tomcat Documentation</display-name>
<description>
Tomcat Documentation.
</description>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/one</url-pattern>
</servlet-mapping>

</web-app>


My directory stucture is

CATALINA_HOME/webapps/MyFirstWebApp/WEB-INF/classes/test/HelloWorldServlet.class

and of course i have placed web.xml under the WEB-INF directory

Can anyone tell me where i have gone wrong

Thanks in advance
Deepthi
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepthi,
I tried the code on my system and I am using tomcat ver 5.0 and it worked just fine for me.Which server are you using?Could you elaborate on what exactly is happening. Are you getting some error message like 404(resource not found)?I will tell you what I have done, probably you can try this as well. I made a html page and I put a submit button there clicking on which it will call my servlet(one that you have coded) and will display a message. Only change that I have done in your servlet code is: instead of System.out.println("message") I have put out.println("message") so that I get the output on my page. Your directly structure looks fine and your deployment descriptor is also fine. If you elaborate on the exact issue you are facing while deployment we will be able to resolve it.
Thanks,
Rancy
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rancy Chadha's advice is good.

If his fix doesn't work for you, when posting back be sure to also include the URL that you are using when trying to access your servlet.

As a side note, overriding the service method is almost never a good idea.
You should, instead, override the doGet method or, if handling form submissions, override the doPost method. The service method, if left alone, will route the request to the proper one for you. This isn't going to fix this problem but it's best to start out on the right foot and avoid bad practices.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepthi Tirunahari:

I am having a problem with deploying my servlet.


check your server log and find out what error/Exception you got after calling this servlet.. paste it here the stake trace it would help us to find if there got any other problem..
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Thanks a lot for your valuable advice....

The version of tomcat i using is 5.5.20

In the browser when i give

http://localhost:8080/manager/html

The page is getting displayed fine.....But when i try to click on the MyFirstWebApp on that page i get the following error report

HTTP Status 404 - /MyFirstWebApp/

---------------------------------------------------------------------------
type : Status report

message : /MyFirstWebApp/

description : The requested resource (/MyFirstWebApp/) is not available.


---------------------------------------------------------------------------

Apache Tomcat/5.5.20




Can anyone tell me what might be the reason for this error
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepthi,

The reason I think you are unable to deploy the servlet would be because you have put your servlet directory under webapps directory and you are using tomcat ver 5.5 so I think instead of putting your servlet directory(MyFirstWebApp) under webapps, try putting it under webapps/ROOT directory. Again I am not sure if this will work for you or not but give it a try and also I would like to tell you where I found this, refer to this link Click Here. This is a Tutorial on Installing and Using
Jakarta Tomcat 5.5 for Servlet and JSP Development. Since I have 5.0 installed I cannot test, you can try and let me know about it.

Thanks,
Rancy
 
Raghavendra Nittur
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Deepthi,
try
http://localhost:8080/MyFirstWebApp/one

as i noticed, your web.xml has url-pattern as "/one".

Regards, Raghav
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rancy,

I am just not able to deploy my web applications on tomcat 5.5.....I have read through the link that you have given me....but nothing just seems to work out....

But on my tomcat 5.0 i am able to deploy my applications very easily....It is just that i am having a problem with tomcat 5.5...

can anybody please help me out...

I keep getting this message

HTTP Status 404 - /MyFirstWebApp/HelloWorldServlet

--------------------------------------------------------------------------------

type Status report

message /MyFistWebApp/one
description The requested resource (/MyFistWebApp/one
) is not available.


--------------------------------------------------------------------------------

Thanks inAdvance
Deepthi

Apache Tomcat/5.5.20
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright Deepthi, don't worry, what I'll do is I'll install ver 5.5 on my system and lets see if it works or not, okay.
I will reply to you as soon as I work on it.

Thanks,
Rancy
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rancy,

Thanks a lot for your help and support........

Regards
Deepthi
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepthi,

I did install ver 5.5 on my system and tried the same thing on it and it works fine for me. Here is step wise procedure for what I have done:

1)The directory which is the source directory "MyFirstWebApp" is under "WebApps" folder
2)Under MyFirstWebApp I have one html file called "test.html" which is the interface that uses my servlet to display a message and a folder WEB-INF folder
3)Under Web-Inf folder I have a "classes" folder which contains the class file of my servlet that is "HelloWorldServlet.class"
4)This folder also contains "web.xml" file which is same as yours.

I am using port 8383 for this version so when I type "http://localhost:8383/MyFirstWebApp/test.html" I get my page and I also get the correct output.
Also when I want to directly communicate with the servlet and I type "http://localhost:8383/MyFirstWebApp/one" I get the output.

Probably like Raghav said, instead of using path "http://localhost:8080/MyFirstWebApp/HelloWorldServlet" try using the link "http://locatlhost:8080/MyFirstWebApp/one" because you have mapped your servlet with this name "/one" in your deployment descriptor and it should give you the output. It works absolutely fine for me. Do let me know the result

Thanks,
Rancy
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rancy,

It Worked!!!

I have followed the step by step procedure you have given me......i started everything from scratch.....i did the same steps i did before and it worked fine for me..........And i feel very guilty for wasting your time with such a simple installation......sorry for that

And Thnks a lot

Regards
Deepthi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic