I want to write a web application so that the parameter is received via the index.jsp file, passed to the servlet, processed and displayed in the browser. A simple program, but I can not and I can not understand why.

Jsp file code:

 <!DOCTYPE html> <html> <body> <form action="/numbOutput/src/main/resources/ReturnNumberServlet"> Enter the number: <input type="text" name="number"> <input type="submit" value="Submit"> </form> </body> </html> 

Servlet code:

 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 ReturnNumberServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String number = request.getParameter("number"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("the selected number is: "+number); pw.close(); } } 

File structure:

enter image description here

jsp-file starts and runs:

enter image description here the result of the program:

enter image description here

Please help to figure out if anyone knows how to do this.

    2 answers 2

    First, do not put java-files in src/main/resources , put them in src/main/java .

    Second, your form sends a request to "/numbOutput/src/main/resources/ReturnNumberServlet" . Those. You clearly indicated the path to the file on the disk, and even to the source. In fact, there should be the path that is assigned to the ReturnNumberServlet servlet in the web.xml (which you did not bring).

    For example. The web.xml following mapping:

     <servlet> <servlet-name>returnNumber</servlet-name> <servlet-class>ReturnNumberServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>returnNumber</servlet-name> <url-pattern>/returnNumber</url-pattern> </servlet-mapping> 

    Where:

    • returnNumber is the logical name of the servlet;
    • ReturnNumberServlet is the name of the servlet class;
    • /servlet/returnNumber is the URL assigned to the servlet.

    Then the form can be sent to the described URL:

     <form action="servlet/returnNumber"> 
    • Thanks, earned. Indeed it was in the location of the java-file - new

    Do you know what the web.xml file is? After all, it describes the way a path is translated into a servlet. Read the documentation