aspose file tools*
The moose likes Servlets and the fly likes Servlet Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Problems" Watch "Servlet Problems" New topic
Author

Servlet Problems

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
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

Joined: Nov 04, 2008
Posts: 146

Hi,
my html and web.xml file in webapps folder.


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

Thanks.


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
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

Joined: Nov 04, 2008
Posts: 146

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

Joined: Jan 05, 2011
Posts: 6
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

Joined: Jul 27, 2011
Posts: 37
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

Joined: Jan 05, 2011
Posts: 6
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

Joined: Jul 09, 2011
Posts: 272

your code is working fine. Check if you maintain correct folder structure.


Ashwin Sridhar
SCJP | SCWCD | OCA
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
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

Joined: Oct 18, 2010
Posts: 42

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

Joined: Jul 27, 2011
Posts: 37
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Problems