aspose file tools*
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
Author

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.

//EmpEcho.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Echo emp details</title>
</head>
<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>
<br><br>
Enter Employee Name
<input type=text name=ENAME size=25>
<br><br>
Enter Employee Job
<input type=text name=JOB size=15>
<br><br>
<input type=Submit value='Send Data'>
</form>

</body>
</html>

//EmpEchoServlet.java
package com.test;

import java.io.IOException;
import java.io.PrintWriter;
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() {
super();
}

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

out.println("<html><body>");
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);
out.println("</body></html>");






}

/* (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
}

//web.xml
<servlet>
<display name>EmpEchoServlet
<servlet name>EmpEchoServlet
<servlet class>com.test.EmpEchoServlet
</servlet>
<servlet mapping>
<servlet name>EmpEchoServlet
<url-pattern>/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.

javawebproject
src
com.test (package)
EmpEchoServlet.java
webcontent
META-INF
WEB-INF
lib
web.xml
EmpEcho.html
What i should do now? Please help me to compile my first servlet program.

Thanks,
sudha.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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
author
Sheriff

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
Greenhorn

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: 3355

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.

Sudha.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in servlet program using eclipse IDE