Home > Enterprise >  HTTP Status 405 - Method Not Allowed - while using servlet
HTTP Status 405 - Method Not Allowed - while using servlet

Time:05-24

My code is to use java servlets and JDBC to store and retrieve the information from a database. There is no error in the IDE the program is running but, the rows aren't inserted into the database and an error occurred in the firefox browser.

The following code is from the SERVLET file

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class ServletRegister extends HttpServlet{
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter("text/html");
        
        String uname = req.getParameter("uname");
        String passwd = req.getParameter("passwd");
        String email = req.getParameter("email");
        int phno = Integer.parseInt(req.getParameter("phno"));
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hack","root","");
            PreparedStatement ps = con.prepareStatement("insert into student values(?,?,?,?)");
            ps.setString(1,uname);
            ps.setString(2,passwd);
            ps.setString(3, email);
            ps.setInt(4, phno);
            int i = ps.executeUpdate();
            if(i>0)
                out.print("Registerd Successfully");
            out.close();
        }catch(Exception e) {
            System.out.println(e);
        }
    }
}



The following code is from HTML file

<!DOCTYPE html>
<html>
    <head>
        <title>Registration</title>
    </head>
<body>
    <form action="register">
        <table>
            <tr>
                <td>Enter User Name: <input type="text" name="uname"> </td>
            </tr>
            <tr>
                <td>Enter Password: <input type="password" name="passwd"> </td>
            </tr>
            <tr>
                <td>Enter E-mail: <input type="email" name="email"> </td>
            </tr>
            <tr>
                <td>Enter Phone.no: <input type="number" name="phno" min="6000000000" max="9999999999"></td>
            </tr>
            <tr>
                <td><input type="submit"> &nbsp;&nbsp;&nbsp; <input type="reset"></td>
            </tr>
        </table>
    </form>
</body>
</html>



The following code is from web.xml file

  <servlet>
        <servlet-name>RegisterServlet</servlet-name>
        <servlet-class>com.hacker.ServletRegister</servlet-class>
  </servlet>
  <servlet-mapping>
        <servlet-name>RegisterServlet</servlet-name>
        <url-pattern>/register</url-pattern>
  </servlet-mapping>



The Error is: Error obtained in firefox browser

CodePudding user response:

You can use Postman to send a POST request to your server, or change doPost to doGet

CodePudding user response:

In HTML file tag you did not mention the method= "post" So its taking the value by default "get" request.

  • Related