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?

Thanks.
icbwubrAsked:
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.
0
 
bloodredsunCommented:
Here's a basic servlet to make sure that your servlet/jsp container is working

TestServlet.java
--------
package com.bloodredsun.dev;

import java.io.IOException;
import java.io.PrintWriter;

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 servlet.do?name=ernie
            //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 );
            out.println("</body></html>");
            out.close();
      }      
      
      public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet( request, response);
      }
}//end TestServlet


web.xml snippet
------------------------
      <servlet>
            <servlet-name>testServlet</servlet-name>
            <servlet-class>com.bloodredsun.dev.TestServlet</servlet-class>
      </servlet>
        <!-- more servlets if any-->
      <servlet-mapping>
            <servlet-name>testServlet</servlet-name>
            <url-pattern>/TestServlet.do</url-pattern>
      </servlet-mapping>


---------

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.
<webapp>/WEB-INF/classes/com/bloodredsun/dev/TestServlet.class

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
0
 
bloodredsunCommented:
other common causes are mispelling the doPost and doGet methods and calling super(request, response) in either of these methods.
0
Upgrade your Question Security!

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

 
arun99907Commented:
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.


0
 
cincin77Commented:
Also check the security constraints in your web.xml.
0
 
bloodredsunCommented:
Anything going on here?
0
 
bloodredsunCommented:
agreed
0
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.