1

I'm trying to get input from the end user through a form. I made the form by using jsp.

welcome.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Welcome</title>
</head>
<body>
     <form action="welcome" method="post">
        <input type="text" value="username" />
        <input type="text" value="password" />
        <input type="submit" value="login" />
     </form>
</body>
</html>

The information that the user will enter will then go to a servlet where it will be printed out to the console.

MyApp.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/welcome")
public class MyApp extends HttpServlet {

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

    req.getRequestDispatcher("/WEB-INF/welcome.jsp").forward(req, resp);

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

    System.out.println("Name: " + name);
    System.out.println("Password: " + password);
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1"
>

<servlet>
    <servlet-name>MyApp</servlet-name>
    <servlet-class>main.com.myfirstapp.MyApp</servlet-class>
</servlet>
</web-app>

I encounter my problem when I execute my program on the server. This is the error I am getting

HTTP Status 405 - HTTP method GET is not supported by this URL
type: Status report
message: HTTP method GET is not supported by this URL
description:  The specified HTTP method is not allowed for the requested resource.
TheRealRave
  • 373
  • 1
  • 8
  • 21
  • *"when I execute my program on the server"* This is a really strange description of opening a JSP page in a webbrowser. Or did you do something like *rightclick servlet class in my IDE, choose option run on server* orso? What exactly is the URL in browser's address bar? Is it the one of the JSP containing the HTML form, or is it the one of the servlet which doesn't have a `doGet()` method at all? Have you for instance read http://stackoverflow.com/q/2349633 which is linked in the duplciate of your previous question? – BalusC Jun 23 '16 at 13:37
  • in the form and specially in the action don't start it with "/" just let it "welcome" be cause the annotation had already a slash on it – PacMan Jun 23 '16 at 14:21
  • @BalusC Yes I did rightclick on MyApp.java -> run on server. The URL in the browser is `http://localhost:8080/MyFirstApp/welcome`. I also read through that question and have updated my question. I am still getting the same error even after it though. – TheRealRave Jun 23 '16 at 15:34
  • @PacMan I tried that and the error is still occurring. – TheRealRave Jun 23 '16 at 15:35
  • @TheRealRave are you putting the welcome.jsp in the web-inf folder ?? – PacMan Jun 23 '16 at 15:39
  • Yes that's currently where I have it. Will I instead put it into `WebContent`? – TheRealRave Jun 23 '16 at 15:42
  • 1
    the web-inf folder is a secured folder where you can not put your jsp files (unless in some frameworks which is not the case) you should put it directly in the webContent and don't forget to change your form as i showed in the answer – PacMan Jun 23 '16 at 15:54
  • @PacMan: you are completely wrong. Putting it in /WEB-INF is **exactly** the reason why the JSP may not be directly opened without invoking the servlet first. The servlet in its current form can actually forward to it (how else would those "some framework" work under the covers do you think?). Here's some food for reading: http://stackoverflow.com/q/12088103 and http://stackoverflow.com/q/2349633 and http://stackoverflow.com/q/3541077. TheRealRave, please ignore his "advice". – BalusC Jun 24 '16 at 08:48
  • @BalusC Thanks for all help. My problem turned into a `404 error` or else it was a `404 error` all along and my tired brain saw it as a `405 error`. But either way I managed to fix everything by doing `@WebServlet(name = "welcome", value="/")`. I also kept the `web.xml` because if I don't I get a `405 error.` – TheRealRave Jun 24 '16 at 13:11
  • That's not the correct solution. You will run into future trouble on static resources such as CSS/JS/image files. Your web application will be unable to serve them properly. See also http://stackoverflow.com/q/4140448 and http://stackoverflow.com/q/33248473 The duplicate contains the correct solution. Your main mistake is just that you "executed" the application in a strange and wrong way. You should just add the project to the server and then start the server in *Servers* view and finally like as a normal enduser enter the desired URL in address bar of your favourite webbrowser. – BalusC Jun 24 '16 at 13:13

1 Answers1

1

i have already suffering from the same problem, well i suggest you to remove that slash from the action and let it just <form action="welcome" method="post"> , since you are using @annotation so there is no need for web.xml you can delete it . another thing you will not get the waited result in your servlet cause there is no name in the form , here is an example you type String name = req.getParameter("name"); in the form you have to set name insted of value like this <input type="text" name="name" /> and the same thing for the finally your form will look like

<form action="welcome" method="post">
        <input type="text" name="name" />
        <input type="text" name="passcode" />
        <input type="submit" value="submit" />
</form>
PacMan
  • 1,358
  • 2
  • 12
  • 25
  • Thanks for the help, this helped because I was doing wrong beforehand. But I was still getting the error so I looked at other servlet examples on github and by luck I found one that used `@WebServlet(name = "welcome", value = "/")` – TheRealRave Jun 24 '16 at 13:13