Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever?

I'm a beginner and making a small registration program with database But i'm trying to run this but it's giving me some errors pls help:

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

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.
Apache Tomcat/8.0.5

And here is my register.html codes:

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="Register" method="post">
    Name: <input type="text" name="name">
    Email: <input type="text" name="email">
    Password: <input type="password" name="password">
    Country:

    <select name="userCountry">
        <option>India</option>
        <option>Pakistan</option>
        <option>Other</option>
    </select><br><br>

    <input type="submit" value="register">
</form>
</body>
</html>

Here is my Register.java codes:

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

public class Register extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        String n=request.getParameter("name");
        String p=request.getParameter("password");
        String e=request.getParameter("email");
        String c=request.getParameter("userCountry");

        try{
            Connection con=DriverManager.getConnection(
                    "jdbc:mysql://localhost:8888", "root", "1234"
            );

            PreparedStatement ps=con.prepareStatement("insert into REGISTERUSER values(?, ?, ?, ?)");

            ps.setString(1,n);
            ps.setString(2,p);
            ps.setString(3,e);
            ps.setString(4,c);

            int i=ps.executeUpdate();
            if(i>0){
                out.print("Registered successfully..");
            }

        }catch(Exception d){d.printStackTrace();}
        out.close();
    }
}

And here is my Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>Register</servlet-name>
        <servlet-class>Register</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/register.html</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>register.html</welcome-file>
    </welcome-file-list>
</web-app>

Help would be appreciated!!

like image 541
user3631223 Avatar asked May 13 '14 06:05

user3631223


People also ask

How do I fix HTTP method POST is not supported by this URL?

Solution: Use only the form button you mapped to your servlet action. Show activity on this post. 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.


3 Answers

The problem is that you mapped your servlet to /register.html and it expects POST method, because you implemented only doPost() method. So when you open register.html page, it will not open html page with the form but servlet that handles the form data.

Alternatively when you submit POST form to non-existing URL, web container will display 405 error (method not allowed) instead of 404 (not found).

To fix:

<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>
like image 156
Leos Literak Avatar answered Oct 19 '22 10:10

Leos Literak


Override service method like this:

protected void service(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {
        doPost(request, response);
}

And Voila!

like image 45
Mihir Deshpande Avatar answered Oct 19 '22 10:10

Mihir Deshpande


I think your issue may be in the url pattern. Changing

<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>

and

<form action="/Register" method="post">

may fix your problem

like image 24
Totò Avatar answered Oct 19 '22 09:10

Totò