Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting 500 error when trying to run a servlet

 
Kondapally Ashwin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class eg1 {


Maybe you want to extends HttpServlet somewhere
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kondapally Ashwin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct class exists in the package and it is there in the package. Please advise.
 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its working now.

Thanks!!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And telling others why "it's working now" would be nice too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic