Reputation: 3654
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
Upvotes: 0
Views: 2574
Reputation: 5792
Do as NimChimpsky wrote or just keep the selected user in http session.
Upvotes: 0
Reputation: 47310
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.
Upvotes: 2