Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Problems

 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am new to Servlets and I will greatly appreciate if anyone could advise me on the problems I faced.

I am currently using eclipse IDE to write the servlet and I kept facing errors like:
"HTTP Status 404 - Servlet EasyQnServlet is not available" or sometimes "HTTP Status Report 500 Exception Report"
whenever I tried to pass some parameters to the servlet from my html page.

HTML code:


Servlet Code:
package simuleqn;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class EasyQnServlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// Set the response message's MIME type (in Content-Type response header)
response.setContentType("text/html");
// Get an output Writer to write the response message over the network
PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>");
out.println("<html>");
out.println("<head><title>Echo Servlet</title></head>");
out.println("<body><h2>You have enter</h2>");

// Retrieve the value of the query parameters "title", "instructions
String title = request.getParameter("title");
String instructions = request.getParameter("instructions");

// Get null if the parameter is missing from query string.
// Get empty string or string of white spaces if user did not fill in
// Parameter: title
if (title == null || title.trim().equals(""))
{
out.println("

Please enter the title.

");
}
else
{
out.println("

Title: " + title + "

");
}

// Parameter: instructions
if (instructions == null || instructions.trim().equals(""))
{
out.println("

Instruction: NONE

");

}
else
{
out.println("

Title: " + title + "

");
}

out.println("</body></html>");
}

}
}


web.xml


I have kept my EasyQnServlet.java file in src>simuleqn and my html and web.xml file in webapps folder.

I can launch my html in eclipse (tomcat server), but when I clicked 'submit' after entering the inputs, I get HTTP Status (404 / 500) Error Report. I am not sure where the problem lies. Any help will be greatly appreciated!!
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my html and web.xml file in webapps folder.


put you web.xml in webapps /WEB-INF/web.xml

Thanks.
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:Hi,
my html and web.xml file in webapps folder.


put you web.xml in webapps /WEB-INF/web.xml

Thanks.


Hi Vijay Tidake,

Thank you for your prompt reply. Yup, my web.xml was already in the webapps/WEB_INF/web.xml. But the problem still remains.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apple Khoa,

what you have posted(code) is perfectly fine and I don't see any error as such.

again is this typo WEB_INF?,if yes change it to WEB-INF

Hope this helps
Thanks
 
Lalit Jugran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apple Khoa,
make sure your EasyQnServlet class file is inside WEB-INF\classes folder with proper package structure & all libraries are inside WEB-INF\lib folder. Rest of the code is fine. It should work correctly.
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit Jugran,

You mentioned about my EasyQnServlet class file? What do you mean by class file? Is it the same as my EasyQnServlet.java file which I put under src>simuleqn folder?

By the way, am I right to put my EasyQnServlet.java file in the src>simuleqn folder? (simuleqn is the package name).
Eclipse automatically put that java file under src (not WEB-INF folder) when I created that servlet.
 
Lalit Jugran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apple Khoa,

I mean after compilation of EasyQnServlet.java file, EasyQnServlet.class file should reside inside WEB-INF\classes folder with proper package structure. Our web application has nothing to do with .java file so it doesn't matter where you are putting your .java file inside project. The only thing is that classes after compilation should reside inside WEB-INF\classes folder. Feel free to ask me if you have further queries regarding this.


 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code is working fine. Check if you maintain correct folder structure.

 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit Jugran,

Thank you so much for your reply. May I know what can I do to get the class file? You mentioned that class file is a file we get after we compiled the .java file. Does 'compile' means pressing the 'Run' button in the eclipse IDE? I have run the .java file (not servlet) many times when I wrote my mathematics java program.

I am sorry if my question is dumb. This is my first time using an IDE, and also writing a java program and servlet.

Thank you for your help once again.
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's simple dear....
java file is the source file you are coding...in your case it is a servlet file...it can be any java file in your application and must be put in the WEB-INF\classes directory under proper package structure...
when the application is deployed into ear or war.... all the java files in the WEB-INF\classes directory are compiled and converted to class files...and put into WEB-INF\classes in the proper directory structure....
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got it working!! You guys and this forum rocks!!! I finally see some light after spending one whole say trying to figure out what's wrong!!! Thanks everyone!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic