0

I have a start.jsp, a UserInfo.java servlet and a view.jsp. The start.jsp has a form that takes a username input, send it to the servlet which, in turn, sends it to view.jsp. However, when I press the submit button on the form, no redirect happens. I suspect there's something wrong with my paths here, but can't figure out what's wrong. Here's my directory tree:

AppName
  pages
    projects
      ProjectName
        start.jsp
        view.jsp
  src
    com
      web
        UserInfo.java
  WEB-INF
    classes
      com
        UserInfo.class
    web.xml

UserInfo.java:

public class UserInfo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.getWriter().println("GET");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String userName = request.getParameter("username");

        RequestDispatcher view= request.getRequestDispatcher("/projects/ProjectName/view.jsp");
        view.forward(request, response);

    }

}

web.xml:

<servlet-mapping>
    <servlet-name>UserInfo</servlet-name>
    <url-pattern>/User.do</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>UserInfo</servlet-name>
    <servlet-class>com.web.UserInfo</servlet-class>
</servlet>

start.jsp:

<form method="POST" action="User.do">
    <div class="form-group">
        <label for="usr">username:</label><br/><br/>
        <input type="text" class="form-control"name="username"><br/><br/>

    </div>
</form>

<button type="button" class="btn btn-primary"  type="submit">
   Get info
</button>

view.jsp:

<h3>Hello,
   <%
      out.println(request.getParameter("username"));
   %>
</h3>
parsecer
  • 4,758
  • 13
  • 71
  • 140
  • Are you not redirecting to view.jsp or not showing data ? – Avijit Barua Sep 03 '18 at 04:09
  • Perhaps put the button inside the form and check the container logs to see what it's getting and post that here. No telling what else you have wrong since there is no pom.xml or any other description of a build and deployment mechanism. Why is the classes directory in the WEB-INF source directory? Best to get a better tutorial and follow it. – K.Nicholas Oct 02 '18 at 20:28

3 Answers3

2

Here are couple of points to take note about the example posted:


(1) The Button:

(a) These define a clickable button - mostly used with JavaScript to activate a script. The following two are similar; one has a body and the other do not.

<INPUT TYPE="BUTTON" VALUE="Get Info">

<BUTTON TYPE="BUTTON">
    Get Info
</BUTTON>

(b) To submit a form with its input, as in the case of this example, a submit button is to be clicked. The form is sent to the servlet (the server-side program) specified by the ACTION attribute of the FORM. The following two are similar; one has a body and the other do not.

<INPUT TYPE="SUBMIT" VALUE="Get Info">

<BUTTON TYPE="SUBMIT">
    Get Info
</BUTTON>


(2) The Form:

All the inputs to be submitted (related to the form) are to be defined within that form - that includes the user name text input and the submit button. The corrected markup for start.jsp:

<form method="POST" action="User.do">
    <div>
        <label>username:</label><br/><br/>
        <input type="text" name="username"><br/><br/>
    </div>
    <button type="submit">
        Get info
    </button>
</form>


(3) The Servlet:

UserInfo.java:

public class UserInfo extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.getWriter().println("GET");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String userName = request.getParameter("username");
        getServletContext().log("# User name: " + userName);
        RequestDispatcher view = request.getRequestDispatcher("view.jsp");
        view.forward(request, response);
    }
}


(4) I have structured the directory tree little differently (for convenience):

servlet-1
    start.jsp
    view.jsp
src
    com
        web
            UserInfo.java
WEB-INF
    classes
        com
            web 
                UserInfo.class
     web.xml

There is no other changes in the start.jsp, web.xml and view.jsp. The deployed web app was invoked using the URL (in this case deployed on Apache Tomcat web server): http://localhost:8080/servlet-1/start.jsp .

This shows the start.jsp in the browser. Enter the text "username" and click the "Get Info" button. The result will show in the view.jsp (I guess that's what was expected).

Finally, as already mentioned the RequestDispatcher is used to either forward to another resource or include content from another resource - its not a redirect. NOTE: The request dispatcher can be acquired either from ServletContext or from ServletRequest; note the difference between the two ways of getting the dispatcher.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • Here is a link to a post I found which explains the difference between a redirect and a forward: [RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()](https://stackoverflow.com/questions/2047122/requestdispatcher-forward-vs-httpservletresponse-sendredirect) . – prasad_ Oct 03 '18 at 16:11
1

Well, you don't say what does happen.

Does the forward work? i.e. does a new page appear? Because a forward is not a redirect.

A redirect sends an explicit response to the browser that then loads a new page, and is mostly obvious by the fact that the URL changes in the browser.

But a forward does not do that. Rather, it simply changes what page is output for the request currently sent. So, you don't really say in detail what is (or is not) happening here.

But taking your question at face value, you're not getting a redirect because forward does not redirect at all.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
1

Keep the button inside form .

<form method="POST" action="User.do">
    <div>
        <label>username:</label><br/><br/>
        <input type="text" name="username"><br/><br/>
    </div>
    <button type="submit">
        Get info
    </button>
</form>