Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   HTTP Status 405 - HTTP method POST is not supported by this URL (http://www.programmingforums.org/showthread.php?t=8965)

hemanth.balaji Mar 21st, 2006 1:54 AM

HTTP Status 405 - HTTP method POST is not supported by this URL
 
I am using Apache Tomcat

I got the following error when invoking a jsp.

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

My 2 files are

LoginServlet1
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class LoginServlet1 extends HttpServlet {

private void sendLoginForm(HttpServletResponse response,
boolean withErrorMessage)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Login</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");

if (withErrorMessage)
out.println("Login failed. Please try again.<BR>");

out.println("<BR>");
out.println("<BR>Please enter your user name and password.");
out.println("<BR><FORM METHOD=POST>");
out.println("<BR>User Name: <INPUT TYPE=TEXT NAME=userName>");
out.println("<BR>Password: <INPUT TYPE=PASSWORD NAME=password>");
out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");

}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
sendLoginForm(response, false);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (userName!=null && password!=null &&
userName.equals("hemanth") && password.equals("balaji")) {
RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet");
rd.forward(request, response);
}
else {
sendLoginForm(response, true);
}

}
}


and,

WelcomeServlet.java

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

public class WelcomeServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Hi from Servlet Demo</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Demo");
out.println("</BODY>");
out.println("</HTML>");
}
}


Thanks and regards,
Hemanth
http://www.freejavaguide.com

Nebula Mar 21st, 2006 2:37 AM

:

LoginServlet1
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class LoginServlet1 extends HttpServlet {

private void sendLoginForm(HttpServletResponse response,
boolean withErrorMessage)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Login</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");

if (withErrorMessage)
out.println("Login failed. Please try again.<BR>");

out.println("<BR>");
out.println("<BR>Please enter your user name and password.");
out.println("<BR><FORM METHOD=POST>");
out.println("<BR>User Name: <INPUT TYPE=TEXT NAME=userName>");
out.println("<BR>Password: <INPUT TYPE=PASSWORD NAME=password>");
out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");

}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
sendLoginForm(response, false);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (userName!=null && password!=null &&
userName.equals("hemanth") && password.equals("balaji")) {
RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet");
rd.forward(request, response);
}
else {
sendLoginForm(response, true);
}

}
}


and,

WelcomeServlet.java

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

public class WelcomeServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Hi from Servlet Demo</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Demo");
out.println("</BODY>");
out.println("</HTML>");
}
}


Alot easier to read your code if you use the code bars. :)

hemanth.balaji Mar 21st, 2006 2:42 AM

Hi i see no change in the code.

Nebula Mar 21st, 2006 2:46 AM

I never said nor implied that their was a change in the code but it is ALOT easier to read and understand their for getting a better reply.

The Dark Mar 21st, 2006 3:35 AM

Quote:

Originally Posted by Nebula
I never said nor implied that their was a change in the code but it is ALOT easier to read and understand their for getting a better reply.

Not really, the main point about the code tags is to preserve indenting, so that you can line up all those ifs and whiles.

The Dark Mar 21st, 2006 3:48 AM

Maybe it is the "WelcomeServlet" page that can't handle POSTs. Maybe you could try doing something simple when you get the original POST, say just returning back some simple html.


All times are GMT -5. The time now is 5:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC