0

I have a servlet and a jsp page. The jsp page contains a form which the end user will fill out.

<html>
<head>
    <title>Please log in to your profile</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/login_servlet" method="post">
        Email: <input type="text" size="5" name="email"/>
        &nbsp;&nbsp;
        Password: <input type="text" size="5" name="password"/>
        &nbsp;&nbsp;
        <input type="submit" value="Sign In" />
    </form>
    </body>
</html>

I will then use a doPost() method in the servlet because the form has a post method. I get the parameters in the servlet so I can print then out to the console.

@WebServlet("/login_servlet")
public class LoginServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, 
            HttpServletResponse resp) throws ServletException, IOException {

        String email = req.getParameter("email");
        String password = req.getParameter("password");

        System.out.println("Email: " + email);
        System.out.println("Password: " + password);
    }
}

When I try visit the URL which is http://localhost:8080/StudentPortal/login_servlet I get this error; HTTP Status 405 - HTTP method GET is not supported by this URL with the description that "The specified HTTP method is not allowed for the requested resource."

I'm close to being blocked from asking anymore questions. So please before this is marked as duplicate, I want you to know that I have looked at similar questions and have followed the advice given to no avail.

I have to learn servlets because I'm been put on a Spring project for work soon.

TheRealRave
  • 333
  • 1
  • 2
  • 19
  • Could you please link to the similar questions that you have followed the advice of so that we can rule those solutions out. – Jonny Henly Jun 28 '16 at 09:22
  • You do not have a `doGet()` method, when you type the servlet’s path in address bar directly, the web container like Tomcat will try to invoke the `doGet()` method. – Mihir Jun 28 '16 at 09:26
  • @Mihir What should I have in the `doGet()` method? Right now I tired adding it in and inside it I had called `doPost()`; `doPost(req, resp)`. – TheRealRave Jun 28 '16 at 09:43
  • @JonnyHenly [Q1](http://stackoverflow.com/questions/37993197/http-status-405-http-method-get-is-not-supported-by-this-url-when-using-jsp?noredirect=1#comment63470852_37993197) || [Q2](http://stackoverflow.com/questions/2349633/doget-and-dopost-in-servlets) || [Q3](http://stackoverflow.com/questions/12105584/http-status-405-http-method-get-is-not-supported-by-this-url) || [Q4](http://stackoverflow.com/questions/27499323/servlet-error-http-status-405-http-method-get-is-not-supported-by-this-url) – TheRealRave Jun 28 '16 at 09:44
  • If you don't want to access your `jsp` directly and you want to access your `servlet` path directly putting URL in address bar then you have to invoke the JSP page from a `servlet` through functionality of the standard `javax.servlet.RequestDispatcher` interface. You can put that code in side `doGet` or inside a `Filter`. – Mihir Jun 28 '16 at 10:07

1 Answers1

0

When you visit your url, you are using get method. And in your servlet you have declared only doPost method. Try to use postman extension for chrome to use other http methods.

*Try to access directly to your jsp page: localhost:8080/StudentPortal/pathToYourJspPage

*You can also add doGet method in your servlet and call redirect from there to jsp page: response.sendRedirect(location) or request.getRequestDispatcher(location).forward(request,response)

Sergey Frolov
  • 1,013
  • 13
  • 26
  • But why do I need to download an extension? Surely it must be something wrong with my code. – TheRealRave Jun 28 '16 at 09:25
  • Well, you should go to your jsp page directly, not to servlet. via `http://localhost:8080/StudentPortal/login_servlet` your goes into servlet. Try with this url: http://localhost:8080/StudentPortal/pathToYourJspPage – Sergey Frolov Jun 28 '16 at 09:28
  • is that not bad practice though? Lets say you have `...home.jsp`, would you not run into a problem when trying to access something like `home/profile/activity`? – TheRealRave Jun 28 '16 at 09:38
  • It's not bad practice. It's up to you. You can direct go to the jsp page or you can add `doGet` method in your servlet and call redirect from there to jsp page: `HttpServletResponse.sendRedirect(String location)` – Sergey Frolov Jun 28 '16 at 09:42