将值从 Servlet 传递到 JSP

发布于 2024-12-01 07:01:19 字数 2571 浏览 0 评论 0原文

我想从列表中选择一个用户,并在输入字段中显示值(以便管理员可以更改它们),

JSP 有一个表单可以从用户列表中选择用户:

         <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>

UserSelectionController 从数据库中读取值并看起来像这样:(这里只有 dopost 方法)

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);
}

它还使用 getter 方法将值带回 JSP:

    public String getFirstname() {
    return firstname;
}

系统 out println 测试显示 servlet 中的正确值。重定向到数据来自的同一个 JSP。回到 JSP,我想显示输入字段中的值:

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

遗憾的是这里的值是 null。我能做些什么。任何线索都会很棒。 最好的问候,丹尼尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-12-08 07:01:19

您正在使用 servlet 作为 dto。这不好。

创建另一个 pojo,它将是一个 DTO。它只有字符串成员变量,以及 getters/setters。使用查询结果填充其成员,并将 dto 放入会话中:

request.setAttribute("userSelectionDTO",userSelectionDTO);

然后更改 jsp,使其引用 dto:

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

可能有用,并使用JSTL

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.

美煞众生 2024-12-08 07:01:19

按照 NimChimpsky 编写的操作或仅将所选用户保留在 http 会话中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文