23

I am having trouble getting the page to work. I have my form method to post and my servlet implements doPost(). However, it keeps showing me that I am not supporting the POST method.

I am just trying to do a simple website and insert values into my MySQL DB.

*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).*

the static page:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
        "http://www.wapforum.org/DTD/xhtml-mobile10.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>XHTML Mobile Profile Document</title>
        <!-- 
            Change href="style.css" below to the file name and
            relative path or URL of your external style sheet.
          --> 
        <link rel="stylesheet" href="index.css" type="text/css"/>
        <!-- 
        <style> document-wide styles would go here </style>
        -->
    </head>
    <body>

        <h1> Register Here </h1>
       <form action="regSuccess.do" method = "POST">
         UserName: <input type="text" name="txtregUsername" size="15" maxlength="15">
                   <br/>
         Password: <input type="password" name="txtregPassword" size="15" 
                    maxlength="15"><br/>
         Name: <input type="text" name="txtregName" size="20" maxlength="30"><br/>
         Email: <input type="text" name="txtregEmail" size="20" maxlength="30">
                <br/><br/> 
               <input type="submit" name="btnRegister" value="Register Me"/>
        </form>
    </body>
</html>

the servlet:

package core;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class handlingReg extends HttpServlet {

    //database parameters
    private static final String db_server = "localhost/";
    private static final String db_name ="bus_guide";
    private Connection con = null;

    //init of connection to dabatase
    public void init(ServletConfig config) throws ServletException {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
    catch (Exception e) {
        System.out.println("Exception in init(): unable to load JDBC DriverA");
        }
    try {
    con = DriverManager.getConnection("jdbc:mysql://"+ db_server + db_name , "root" , "");
        System.out.println("conn: "+con);
        }
    catch (Exception e) {
        System.out.println(e.getMessage());
        }
    }
    //end init()

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       //response handling

       response.setContentType("text/html");
       PrintWriter out = response.getWriter();

       //handling request
       String enteredUsername = request.getParameter("txtregUsername");
       String enteredPw = request.getParameter("txtregPassword");
       String enteredName = request.getParameter("txtregName");
       String enteredEmail = request.getParameter("txtregEmail");

        //inserting values into database
        try {
            Statement stmnt = con.createStatement();
            stmnt.executeUpdate("INSERT INTO regUsers VALUES('"+enteredUsername+"','"+enteredPw+"','"+enteredName+"','"+enteredEmail+"')");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

       //output html out.println("");
       out.println("<?xml version = \"1.0\" encoding =\"utf-8\"?>");
       out.println("<!DOCTYPE html PUBLIC \"-//WAPFORUM/DTD XHTML Mobile 1.0//EN\"");
       out.println("    \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">");
       out.println("<html xmlns=\"http://www.w3.org/1000/xhtml\">");

       out.println("<head>");
       out.println("<title></title>");
       out.println("</head>");
       out.println("<body>");
       out.println("Register Success!");
       out.println("<a href = \"index.xhtml\"> Click here to go back to main page. 
                    </a>");
       out.println("</body>");
       out.println("</html>");
    }

}

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

 <!--Self declared servlet mapping starts here-->
 <servlet>
  <servlet-name>handleRegister</servlet-name>
  <servlet-class>core.handlingReg</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>handleRegister</servlet-name>
  <url-pattern>/regSuccess.do</url-pattern>
 </servlet-mapping>

 <!--Self declared servlet mapping ends here-->  
 <servlet-mapping>
  <servlet-name>invoker</servlet-name>
  <url-pattern>/servlet/*</url-pattern>
 </servlet-mapping>
 <mime-mapping>
  <extension>xhtml</extension>
  <mime-type>text/html</mime-type>
 </mime-mapping>
 <mime-mapping>
  <extension>jad</extension>
  <mime-type>text/vnd.sun.j2me.app-descriptor</mime-type>
 </mime-mapping>
 <mime-mapping>
  <extension>jar</extension>
  <mime-type>application/java-archive</mime-type>
 </mime-mapping>
</web-app>

edit:removed doGet(request,response);

entpnerd
  • 8,063
  • 5
  • 36
  • 60
sutoL
  • 1,697
  • 10
  • 27
  • 45
  • 2
    Opening a DB connection in `init()` of a servlet is by the way a very bad idea. Opening and closing should go in the very same try-finally block as you execute the query. – BalusC Nov 28 '10 at 14:12

5 Answers5

25

It's because you're calling doGet() without actually implementing doGet(). It's the default implementation of doGet() that throws the error saying the method is not supported.

Martin Algesten
  • 11,444
  • 2
  • 46
  • 74
  • i have just removed the doGet(request,response); however i still get the same error. – sutoL Nov 28 '10 at 13:59
  • Are you 110% sure you're not using the old .class file or something? The doGet() call would completely explain the problem. I can't spot any other problem with the code... – Martin Algesten Nov 28 '10 at 14:02
  • yes, i have deleted my compiled classes and recompile them to make sure i get a new compiled class then i replaced the one's in my WEB-INF/classes/core – sutoL Nov 28 '10 at 14:05
  • Then I'm at a loss. It definitely looks okay. Are you using java > 1.5? In that case, can you add an @Override annotation to the doPost() method - just to make extra sure it is override the parent default implementation. javac should throw an error if it doesn't. – Martin Algesten Nov 28 '10 at 14:10
  • 8
    why do this answer has been marked as "accepted" if the last comment to this same answer begin as "Then I'm at a loss. It definitely looks okay." ? – linuxatico Aug 20 '14 at 15:40
12

if you are using tomcat you may try this

<servlet-mapping>

    <http-method>POST</http-method>

</servlet-mapping>

in addition to <servlet-name> and <url-mapping>

Mohammad Faisal
  • 5,241
  • 14
  • 63
  • 114
kumar
  • 121
  • 1
  • 2
3

It says "POST not supported", so the request is not calling your servlet. If I were you, I will issue a GET (e.g. access using a browser) to the exact URL you are issuing your POST request, and see what you get. I bet you'll see something unexpected.

Enno Shioji
  • 25,422
  • 13
  • 67
  • 104
  • 1
    This may happen for method="post" action="/something" So it's not your servlet that is being called but a default servlet or a JSP in welcome-file-list. – weberjn Sep 27 '16 at 20:59
2

This happened to me when:

  • Even with my servlet having only the method "doPost"
  • And the form method="POST"

  • I tried to access the action using the URL directly, without using the form submitt. Since the default method for the URL is the doGet method, when you don't use the form submit, you'll see @ your console the http 405 error.

Solution: Use only the form button you mapped to your servlet action.

0

If you're still facing the issue even after replacing doGet() with doPost() and changing the form method="post". Try clearing the cache of the browser or hit the URL in another browser or incognito/private mode. It may works!

For best practices, please follow this link. https://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

Barefooter
  • 61
  • 4