简体   繁体   中英

Passing Values from a Servlet to a JSP

I want to select a User from a list, and show the values in the input fields (so an admin can change them)

a JSP has a form to select a user out of a User List:

         <form action="UserSelectionController" method="POST">
             <select name="selectedUser" onchange="this.form.submit()">
                <%
                  Object[] userList_ref = UserListService.getUserList();
                  for (int i = 0; i < userList_ref.length; i++) {%>
                        <option  size="5" value="<%=userList_ref[i]%>">           <%=userList_ref[i]%></option> <% }%>
             </select>
        </form>

the UserSelectionController reads out values from a database and looks like this: (only the dopost method here)

Protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    this.connectToDataBase();

    Statement stmt_ref = null;
    try {
        stmt_ref = (Statement) connection.createStatement();
        ResultSet results_ref = stmt_ref.executeQuery("SELECT salutation,firstname,lastname,street,houseNr,zip,city,country,email,password FROM User WHERE email = '" + request.getParameter("selectedUser") + "'");

        while (results_ref.next()){
            salutation = results_ref.getString("salutation");
            firstname = results_ref.getString("firstname");
            lastname = results_ref.getString("lastname");
            street = results_ref.getString("street");
            houseNr = results_ref.getInt("houseNr");
            zip = results_ref.getInt("zip");
            city = results_ref.getString("city");
            country = results_ref.getString("country");
            email = results_ref.getString("email");
            password = results_ref.getString("password");
        }
    } catch (SQLException ex) {
        Logger.getLogger(UserSelectionController.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println ("Paramter SelectedUser: "+request.getParameter("selectedUser"));
    System.out.println ("Firstname: "+firstname);

    response.sendRedirect(AbsoluteTerms.DOMAIN_USER_SETTINGS);
}

it also Uses getter Methods to bring the values back to the JSP:

    public String getFirstname() {
    return firstname;
}

the system out println tests show the correct Value in the servlet. The Redirect goes to the same JSP where the data came from. Back in the JSP I want to show the value in the input field:

   <jsp:useBean id="userSelection" class="servlets.UserSelectionController" />
   <input type="text" name="firstname" value="<% userSelection.getFirstname();%>" />

Sadly here the values are null. What can I do. Any clue would be great. best regards, Daniel

You are using your servlet, as a dto. This is not good.

Create another pojo, it will be a DTO . It will just have member variable that are strings, and accopmanying getters/setters. Populate its members with the result of your query, and put dto into session :

request.setAttribute("userSelectionDTO",userSelectionDTO);

then change the jsp so it references the dto:

 <jsp:useBean id="userSelection" class="servlets.UserSelectionDTO" />
 <input type="text" name="firstname" value="<% userSelection.getFirstname();%>"

This may be useful, and use JSTL in the jsp.

如同NimChimpsky所写或仅将所选用户保留在http会话中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM