import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
{
private void sendLoginPage (HttpServletResponse res, HttpServletRequest req, boolean error)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter o = res.getWriter();
o.println("<html><head><title>Sample Login Page</title></head><body>Welcome to Login Page : ");
if (error)
o.println("<fieldset><legend>Login Form : </legend>");
o.println("Login Failed, Please Try Again");
o.println("<form method="+"post"+">");
o.println("<br/><input type="+"text"+"value="+"username"+"/>");
o.println("<br/><input type="+"password"+"value="+""+"/>");
o.println("<br/><input type="+"button"+"value="+"Submit"+"/>");
o.println("</form></fieldset></body></html>");
}
public void doGet (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
sendLoginPage ( res, null, false ) ;
}
public void doPost (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
if ( username.equals("*******") && password.equals("*******") )
{
res.sendRedirect ("http://localhost:7001/ten/r1");
}
else
{
sendLoginPage ( res, null, true ) ;
}
}
}
Well this Servlet compiles okay without any errors and also gets deployed on the server
but shows this error when trying to access it through url :
HTTP method GET is not supported by this URL
You have the HttpServletResponse and HttpServletRequest parameters reversed on your methods.
Side note: Using the @Override annotation on overridden methods would cause a compile error for this (assuming you’re using at least Java 1.5)