Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet program not running

 
Anjali Garg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anjali Garg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjali ,

your code looks fine....

so how you are triggering(calling) the servlet

can you send the url
 
Anjali Garg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic