0

I have a simple program which get username and password, if the password is servlet then it will forward to the welcome page, else, it will show a message and includes html login form. When I enter username and password and click on login, no matter what, it always shows me the "HTTP Status 405 : HTTP method GET is not supported by this URL" error. I have read a lot of questions and answers here, none of them helped me to solve this problem.

Here's my code:

Simple.java

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

import javax.servlet.*;
import javax.servlet.http.*;


public class Simple extends HttpServlet {

        @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

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

        String p=request.getParameter("userPass");
        if(p.equals("servlet")){
            RequestDispatcher rd=request.getRequestDispatcher("welcome");
            rd.forward(request, response);

        }
        else{
            out.print("Sorry username or password error!");
            RequestDispatcher rd=request.getRequestDispatcher("index.html");
            rd.include(request, response);
        }
    }
}

WelcomeServlet.java

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;


public class WelcomeServlet extends HttpServlet {

        @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

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

        String n=request.getParameter("userName");
        out.print("Welcome "+n);
    }

}

index.html

<form action="go" method="post">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPass"/><br/>
<input type="submit" value="login"/>
</form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Simple</servlet-name>
    <servlet-class>Simple</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>WelcomeServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>Simple</servlet-name>
    <url-pattern>/go</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
</web-app>
HMD
  • 468
  • 1
  • 5
  • 21
  • 1
    I copy-pasted your implementation and tested it with Tomcat 8.5 and it works fine. Which servlet container are you using? – haba713 Jan 14 '18 at 11:05
  • @haba713 Tomcat 8.5 as well !! at first, the implementation of this program was with `doGet()` somewhere I read that using `GET` causes browser saves cookies of the page and won't apply updates to the page. I don't if this is the real problem – HMD Jan 14 '18 at 11:24
  • Weird... Which Java version? I have OpenJDK 1.8.0_151 64-bit in Linux (Debian Stretch). – haba713 Jan 14 '18 at 11:31
  • I have JDK 1.8.102 In windows 8.1 pro – HMD Jan 14 '18 at 11:33
  • See the link below. I exported a war file if you like to test it. https://drive.google.com/file/d/17CK1hSqhLO6TO3el4qU2i13tiNExh-Y1/view – haba713 Jan 14 '18 at 11:36

0 Answers0