File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in servlet program using eclipse IDE

 
sudha javvadi
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which servlet container / web server are you using?
 
Mark A Brown
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Mark A Brown,

Welcome to JavaRanch

Yes, thats one of the good suggestions.
 
sudha javvadi
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic