0

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

最白目
  • 3,505
  • 6
  • 59
  • 114
  • I'd start by putting your mouse above the `servlets` tag below your question until a black box pops up. Then, click therein the *info* link. – BalusC Aug 24 '11 at 14:18

2 Answers2

2

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.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • can you tell me where? in the controller (servlet) or on the jsp? thanks – 最白目 Aug 24 '11 at 10:16
  • what does the line request.setAttribute("userSelectionDTO"), userSelectionDTO); does exactly? There is no session set, its not working sadly. – 最白目 Aug 24 '11 at 10:44
  • 1
    you have either not set the request attribute, or you have failed to intialize the firstname member variable of the userSelection object – NimChimpsky Aug 24 '11 at 11:35
  • a System.out.println ("request.getAttribute: "+request.getAttribute("user_ref")); shows the attribute exists and is not null, same is if I output the firstname member variable. Just on the JSP page its null. – 最白目 Aug 24 '11 at 12:08
  • 1
    Scriptlets do not share the same scope as taglibs/EL. Use EL. You also don't need jsp:useBean. – BalusC Aug 24 '11 at 14:14
  • with EL you mean Expression Language? what to use instead of jsp:useBean? UserSelectionDTO us_ref = new UserSelectionDTO() ? I really appreciate some more Information cause this is still not working. I really appreciate your help but this problem is driving me nuts right now. – 最白目 Aug 24 '11 at 15:10
  • hey, hold on, I added a "=" behind the "%" in this line value="<% userSelection.getFirstname();%> now the input field shows "null" instead of nothing, very cool, but its still not getting the right value, thanks anyway. Maybe someone can solve the rest of this? – 最白目 Aug 24 '11 at 15:17
0

Do as NimChimpsky wrote or just keep the selected user in http session.

Kris
  • 5,714
  • 2
  • 27
  • 47