Regarding GET & POST

When I try to run my servlet on the address bar if I use either doPost or doGet it gives me error for both saying that POST(GET) method not supported by URL.

Can I know the reason for this problem?

Who is Participating?
bloodredsunConnect With a Mentor Commented:
have you mapped the servlet properly in the web.xml?

Have you implemented/overridden the methods doPost(HttpServletRequest request, HttpServletResponse response) and doGet(HttpServletRequest request, HttpServletResponse response) in your servlet?

Also check you haven't protected your methods, i.e. other jsps and servlets work.

Posting your servlet code would help.
Here's a basic servlet to make sure that your servlet/jsp container is working


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestServlet extends HttpServlet {
      public void init(){
            //any initlializing: none here

      public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //example of getting the session
            HttpSession session = request.getSession() ;
            session.setAttribute( "status" , "friend" ) ;
            //example of getting a request parameter i.e. url section like this
            //also with null check via a ternary operator to provide a default
            String paramName = request.getParameter("name")!= null ? request.getParameter("name"): "stranger" ;
            //example of writing some content
            PrintWriter out = response.getWriter();
            out.println("<html><head><title>A Simple Servlet</title></head><body>");
            out.println("Hello " + paramName );
      public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet( request, response);
}//end TestServlet

web.xml snippet
        <!-- more servlets if any-->


Remember that you need to compile the servlet code into a class file which should be placed according to it's package under the classes folder e.g.

and also that in the web.xml, the order of the elements is important and that your must put all your <servlet> declarations before any <servlet-mapping> decalrations
other common causes are mispelling the doPost and doGet methods and calling super(request, response) in either of these methods.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Authors Comments:

>>When I try to run my servlet on the address bar

First of all when u invoke a servlet using a URL in address bar your doGet() method gets called and not doPost().
(No need to override doPost() in that case)

The above sample code by bloodredsun to write a servlet should help u.

-- See to it that ur Servlet Gets compiled succesfully. check ur code n map it accordingly. And also know how to invoke the servlet. :-o with the mapped path.

Also check the security constraints in your web.xml.
Anything going on here?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.