permaculture playing cards*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet  program not running 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 "Servlet  program not running" Watch "Servlet  program not running" New topic
Author

Servlet program not running

Anjali Garg
Greenhorn

Joined: Sep 19, 2007
Posts: 11
Hi,
I have created one servlet,but it is not running.
In Tomcat its giving HTTP 404 Not found.






Thanks
Anjali Garg
SCJP(81%)
SCWCD under process
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Hi Anjali,

Please give some more information that how are you deploying your application. By the way the error code "404" indicates that container isn't able to locate the resource requested, which might because of improper <url-pattern> in your DD. Please check it, and let's know if still problem persists ...

Best Regsrds


SCJP 1.4, SCWCD 1.5
Anjali Garg
Greenhorn

Joined: Sep 19, 2007
Posts: 11
Originally posted by raunak saxena:
Hi Anjali,

Please give some more information that how are you deploying your application. By the way the error code "404" indicates that container isn't able to locate the resource requested, which might because of improper <url-pattern> in your DD. Please check it, and let's know if still problem persists ...

Best Regsrds

Thanks for reply.
this is java file.

import javax.servlet.*;
import java.io.*;
public class ExServlet1 extends GenericServlet
{
String name,add;
public void init(ServletConfig con)
{
name="Anjali";
add="India";
}
public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.print("<br>Name:"+name);
out.print("<br>Address:"+add);
}
public void destroy()
{
}
}
and this is DD

<web-app>
<servlet>
<servlet-name>ExServlet1</servlet-name>
<servlet-class>ExServlet1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ExServlet1</servlet-name>
<url-pattern>/ExServlet1</url-pattern>
</servlet-mapping>
</web-app>
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83
Hi anjali,

As per your code... you are extending GenericServlet, but usually people do extend HttpServlet, this is not the point i want to show you, The point i want you to see is..!!!

As per the JAVA API when you over ride the parametered version of init.

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).


Hope you got it.
Anjali Garg
Greenhorn

Joined: Sep 19, 2007
Posts: 11
Originally posted by Sarat Koduri:
Hi anjali,

As per your code... you are extending GenericServlet, but usually people do extend HttpServlet, this is not the point i want to show you, The point i want you to see is..!!!

As per the JAVA API when you over ride the parametered version of init.


Hope you got it.


hi
i have tried it with HttpServlet also but its not running.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Originally posted by Sarat Koduri:
Hi anjali,

As per your code... you are extending GenericServlet, but usually people do extend HttpServlet, this is not the point i want to show you, The point i want you to see is..!!!

As per the JAVA API when you over ride the parametered version of init.


Hope you got it.


@Sarat
What you say is correct, but I do not think it should result in 404.

@Anjali
What is the URL you are using to access the servlet? It must be something like:
http://localhost:8080/<WebApplication>/ExServlet1


Thanks and Regards
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Put your class into a package other than the default one. Don't override service. Override the doGet and/or doPost methods. Map the Servlet to the new package location.
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Hi Anjali,
Create directory structure as shown. Put class files in proper packages, put servlet/servlet-mapping entries as shown in your DD. And use the URL given (Use it for reference... change it accordingly .... like ... port_no, web_app_root ... etc accordingly ), you'll get your servlet run.

Please try this and let me know if still problem occurs.


package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* Servlet implementation class ExServlet1
*/
public class ExServlet1 extends GenericServlet {
private static final long serialVersionUID = 1L;

String name,add;
public void init(ServletConfig con)
{
name="Anjali";
add="India";
}
public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.print("<br>Name:"+name);
out.print("<br>Address:"+add);
}
public void destroy()
{
}
}



<servlet>
<servlet-name>ExServlet1</servlet-name>
<servlet-class>
com.controller.ExServlet1</servlet-class>
</servlet>


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


And URL is : http://localhost:8282/WebAppName/ExServlet1


Please try this and let me know. It'll work......

Best Regards
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Anjali ,

your code looks fine....

so how you are triggering(calling) the servlet

can you send the url
Anjali Garg
Greenhorn

Joined: Sep 19, 2007
Posts: 11
Originally posted by raunak saxena:
Hi Anjali,
Create directory structure as shown. Put class files in proper packages, put servlet/servlet-mapping entries as shown in your DD. And use the URL given (Use it for reference... change it accordingly .... like ... port_no, web_app_root ... etc accordingly ), you'll get your servlet run.

Please try this and let me know if still problem occurs.


package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* Servlet implementation class ExServlet1
*/
public class ExServlet1 extends GenericServlet {
private static final long serialVersionUID = 1L;

String name,add;
public void init(ServletConfig con)
{
name="Anjali";
add="India";
}
public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.print("<br>Name:"+name);
out.print("<br>Address:"+add);
}
public void destroy()
{
}
}



<servlet>
<servlet-name>ExServlet1</servlet-name>
<servlet-class>
com.controller.ExServlet1</servlet-class>
</servlet>


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


And URL is : http://localhost:8282/WebAppName/ExServlet1


Please try this and let me know. It'll work......

Best Regards


Hi raunak,
I tried your program.It is running by typing URL in IE.but when I use it with Tomcat Manager then it gives HTTP-404.I am also using one html file that is follows.
<html>
<body>
<form action="http://localhost:8080/Servlet/ExServlet1">
click here.....
<input type="submit" value="click">
</form>
</body>
</html>
before two months all these programs were running,but now they are not working.

Thanks
Anjali Garg
Anjali Garg
Greenhorn

Joined: Sep 19, 2007
Posts: 11
Originally posted by seetharaman venkatasamy:
Hi Anjali ,

your code looks fine....

so how you are triggering(calling) the servlet

can you send the url


Hi seetharaman,
URL is-http://localhost:8080/Servlet/ExServlet1
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83

<form action="http://localhost:8080/Servlet/ExServlet1">


anjali,

i think that action value should be just ExServlet1 instead of the entire path. i mean the form action should look like

because the server is obviously going to append the context path to the action item. please do correct me if am wrong.


[ September 16, 2008: Message edited by: Sarat Koduri ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Not a problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet program not running