aspose file tools*
The moose likes Servlets and the fly likes getting 500 error when trying to run a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting 500 error when trying to run a servlet" Watch "getting 500 error when trying to run a servlet" New topic
Author

getting 500 error when trying to run a servlet

Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Hi,

I am compiling a servlet and placing the class file at C:\Tomcat 5.0\webapps\anji\WEB-INF\classes\eg1(eg1 is the package name) with the servlet name eg1.

The following is the code for my servlet:

package eg1;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class eg1 {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String aa = req.getParameter("state");
out.println("<html>");
out.println("<body>");
out.println("hi"+"</body>"+"</html>");
out.flush();
out.close();

}
}

And the following is my web.xml content:

<web-app>
<servlet>
<servlet-name>ashwin</servlet-name>
<servlet-class>eg1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ashwin</servlet-name>
<url-pattern>/eg1</url-pattern>
</servlet-mapping>
</web-app>


When I type http://localhost:8080/anji/eg1 in the browser I am getting the following error:

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Class eg1.eg1 is not a Servlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:536)


root cause

java.lang.ClassCastException
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:536)


Please advise.

Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

public class eg1 {


Maybe you want to extends HttpServlet somewhere


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And you should change the class name too.
For example, MyServlet

Then in web.xml :
<servlet-class>eg1.MyServlet</servlet-class>
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Hi,
first thing i would like to inform you about java is Object Oriented Language there are some standards in java like where all name of Classes are started from Capital letter, in your code you have created class for Servlet, you gave the name of the class with small letter so plz make it correct first and second most important thing about Servlet is to follow the Life Cycle of Servlets. plz go throw this links.. you would find lacking

Regards,
SAif uddin

Servlet Life Cycle

Servlet Example & Configuration


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Thanks for the replies.

I am having one more problem.

For the following servlet code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class myServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String aa = req.getParameter("state");
out.println("<html>");
out.println("<body>");
out.println(aa+"</body>"+"</html>");
out.flush();
out.close();

}
}

and with the html file(hi.html placed in C:\Tomcat 5.0\webapps\anji) whose code is

<HTML>
<BODY>
<form action = "eg1" method = "POST">
<select name = "state" >
<option value = "aa">aa</option>
<option value = "bb">bb</option>
</select>
<input type = "submit" />
</form>
</BODY>
</HTML>

and the web.xml file as

<web-app>
<servlet>
<servlet-name>ashwin</servlet-name>
<servlet-class>eg1.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ashwin</servlet-name>
<url-pattern>/eg1</url-pattern>
</servlet-mapping>
</web-app>

I am getting an error :

HTTP Status 405 - HTTP method POST is not supported by this URL

--------------------------------------------------------------------------------

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).

And Saifuddin thanks for your advice. I would follow it henceforth.

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1. Did you leave myServlet in its package ? (eg1)
2. Check that you don't have an old version in the classes directory
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
The correct class exists in the package and it is there in the package. Please advise.
Vishal Mungi
Greenhorn

Joined: Mar 24, 2005
Posts: 24
Strange, i dont get this error , i am using tomcat.
Maybe you should restart to clear the cache & let the server use the changed files
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Its working now.

Thanks!!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Kondapally Ashwinm,
Welcome to JavaRanch!

Tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And telling others why "it's working now" would be nice too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting 500 error when trying to run a servlet