Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes IDEs, Version Control and other tools and the fly likes Doubt in servlet program using eclipse IDE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Doubt in servlet program using eclipse IDE" Watch "Doubt in servlet program using eclipse IDE" New topic

Doubt in servlet program using eclipse IDE

sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi all,

Today i downloaded Eclipse IDE and tried to do my first program.I created an HTML file and an servlet class.The HTML file will accept emp details and the servlet program will just Echo those details.When i run HTML file i am getting
description: The requested resource (/JavaWebProject/servlet/EmpEchoServlet) is not available.

Below is the code.

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Echo emp details</title>
<body bgcolor=cyan text=blue>
<h2>Enter emp details</h2>
<form name=EMPFORM method=GET action='servlet/EmpEchoServlet'>
Enter Employee Number
<input type=text name=EMPNO size=15>
Enter Employee Name
<input type=text name=ENAME size=25>
Enter Employee Job
<input type=text name=JOB size=15>
<input type=Submit value='Send Data'>


package com.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

* Servlet implementation class for Servlet: EmpEchoServlet
public class EmpEchoServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
public EmpEchoServlet() {

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();

out.println("<h2 align=center>Received details");
out.println("<h3> Employee Details </h3>");
//Receive data from web browser
String p1= request.getParameter("EMPNO");
String p2= request.getParameter("ENAME");
String p3= request.getParameter("JOB");

// send the parameter to the web browser
out.println("<br><br>EMPLOYEE NUMBER:"+ p1);
out.println("<br><br>EMPLOYEE NAME:"+p2);
out.println("<br><br>EMPLOYEE JOB:"+p3);


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*///protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

<display name>EmpEchoServlet
<servlet name>EmpEchoServlet
<servlet class>com.test.EmpEchoServlet
<servlet mapping>
<servlet name>EmpEchoServlet
</servlet mapping>

Please tell me what i am doing wrong.
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
Where are the .class files located?
what is the directory structure?

to give an example the following structure works for me:

[ December 28, 2007: Message edited by: prakash chauhan ]
[ December 28, 2007: Message edited by: prakash chauhan ]
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi Prakash,

Thanks for your reply. I couldn't see any classes folder under WEB-INF.When i am trying to create one, i am getting clases already exists.Below is the directory structure.

com.test (package)
What i should do now? Please help me to compile my first servlet program.

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Hi Sudha,

Please try to paste the code using "Code" tags so that it will make the code visible clearly without uncluttered.

Try setting your classpath for the project in eclipse as "WEB-INF/classes". You can do so by right clicking on the project and its "BuildPath" settings. In that case, it automatically places the .class files in the location where you set.

Moreover, eclipse as an IDE does NOT create the folder structure for you. And a J2EE application should adhere its structure as per Sun's suggestions and guidelines. The class files are retrieved from the web/application server from the "<ProjectRoot>/WEB-INF/classes" directory. Remember, the WEB-INF is case sensitive!

Please have a look at the image "7.3 Web Application Directory Structure" in this url for further understanding on this. Create a folder structure like this first and then create a project in eclipse based out of this structure.

Hopefully this should work.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Which servlet container / web server are you using?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mark A Brown

Joined: Jan 01, 2008
Posts: 3
The main problem is in your HTML file. You've listed the form's action to be servlet/EmpEchoServlet. However, in your web.xml file, you've listed the url-pattern to be just /EmpEchoServlet. Try removing the servlet prefix in the HTML form's action attribute.

I'm assuming that you've got your servlet class file located in the com\test subdirectory structure below WEB-INF\classes directory.

SCJP 1.4<br />SCWCD 1.4
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Howdy Mark A Brown,

Welcome to JavaRanch

Yes, thats one of the good suggestions.
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Thank you Mark. I removed servlet in 'servlet/EmpEchoServlet' but still i am getting the same problem. I tried giving '/JavaWebProject/EmpEchoServlet' in my html file, surprise it
worked. Thank you all for your suggestions.

I agree. Here's the link:
subject: Doubt in servlet program using eclipse IDE
jQuery in Action, 3rd edition