wood burning stoves 2.0*
The moose likes Servlets and the fly likes unable to run servlet in tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "unable to run servlet in tomcat" Watch "unable to run servlet in tomcat" New topic
Author

unable to run servlet in tomcat

sumedha rao
Ranch Hand

Joined: Nov 26, 2010
Posts: 115

Hello Ranchers,
I am new to using tomcat so kindly bear with me.I have just started preparing for scwcd and i am using head first as my guide.
I have followed the instructions exactly as they directed on page 30-31 of the book,but i am unable to view the output in the browser.

All i can see is the error Http status-404 on the browser.
Please help me,i am fed up of checking the code and trying different methods again and again.Please help


scjp1.6,scwcd1.5!!!
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

What is the directory structure of your web application? Have you configured url mappings in web.xml properly?


Swastik
sumedha rao
Ranch Hand

Joined: Nov 26, 2010
Posts: 115

yes the directory structure is correct.i have followed the same directory structure as given in the book-tomcat->webapps->ch1->WEB-INF(inside this is web.xml)->classes(inside this is Ch1Servlet.class).The compilation was successful but maybe there is some problem at runtime...please help i have been trying since a few hrs but no success
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Personally I haven't gone through the book, so tough to say what is there on page 30-31. Could you please show your web.xml and how you are trying to invoke the servlet i.e the url to invoke the servlet?
sumedha rao
Ranch Hand

Joined: Nov 26, 2010
Posts: 115

web.xml:-


Ch1Servlet.java:-


steps after compiling:
Start->all programs->Tomcat->Configure Tomcat

Launch the browser
http://localhost:8080/ch1/Serv1
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Something looks wrong with your web.xml, when I used it tomcat says some unsupported encoding exception. The following worked for me, could you please try with the following once

sumedha rao
Ranch Hand

Joined: Nov 26, 2010
Posts: 115

i am still getting the same error..i really cant understand whats the problem..is the web.xml code different for different versions of tomcat? i am using version 6.0.29
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Is that tomcat console or log displaying any error when you start tomcat?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5



This mistake happens frequently - if fact if you browse this forum often you will see numerous examples.

ALL classes used in servlet should be explicitly placed in a package because when the JVM sees a class without a package it looks in the "current" directory. You have no control over the current directory in the servlet environment.

Bill
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

For me it has worked without package as well. I may be wrong, but as far as I know not the current directory rather it looks into WEB-INF\classes.
sumedha rao
Ranch Hand

Joined: Nov 26, 2010
Posts: 115

hey the output is displayed now!!! I stopped the tomcat service and restarted it from the control panel and i can see the output now!!
Thanks a lot for spending your precious time in solving my problem!!!Thankyou so much
monika se
Greenhorn

Joined: Mar 23, 2011
Posts: 22
Similar Problem i have::

My servlet_lifecycle.java file is:

//servlet programs demonstrating its life cycle

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class servlet_lifecycle extends HttpServlet{
int i;

public void init() throws ServletException
{
i=0; //initializing i value-- parameters
}

//incrementing i value in doGet

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

if(i==0)
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet's Life Cycle</title>");
out.println("</head>");
out.println("</body>");
out.println("</h1>i value initialized in init method</h1>" + "<h1>"+ i + "</h1>");
out.println("</body>");
out.println("</html>");
}
i=i+1;
if(i==10)
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet's Life Cycle</title>");
out.println("</head>");
out.println("</body>");
out.println("</h1>i value reaches 10, hence calling destroy method to reset it</h1>" + "<h1>"+ i + "</h1>");
out.println("</body>");
out.println("</html>");
destroy();
}


if(i<10)
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet's Life Cycle</title>");
out.println("</head>");
out.println("</body>");
out.println("</h1>i value incremented in doGet method</h1>" + "<h1>"+ i + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

public void destroy()
{
i=0;
}


}


web.xml file is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app 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_2_4.xsd"
version="2.4">

<description>
Servlet and JSP Examples.
</description>
<display-name>Servlet and JSP Examples</display-name>



<servlet>
<servlet-name>servlet_lifecycle</servlet-name>
<servlet-class>servlet_lifecycle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet_lifecycle</servlet-name>
<url-pattern>/servlet_lifecycle</url-pattern>
</servlet-mapping>

</web-app>


Please help me.. i am new to servlets... i am geting error HTTP 503

HTTP Status 503 - This application is not currently available

type Status report

message This application is not currently available

description The requested service (This application is not currently available) is not currently available.

Apache Tomcat/6.0.32


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: unable to run servlet in tomcat
 
Similar Threads
Excel file not opening in Tomcat 4.1.29
HelloServlet not running ?
Running Sevlet
XDB realm
Difficulty in developing a Web Browser in Java?