【问题标题】:Servlet in Java. How to retrieve attributes?Java 中的 Servlet。如何检索属性?
【发布时间】:2014-02-25 23:41:36
【问题描述】:

我真的在努力解决这个问题。我将用户的姓名和地址作为会话变量存储在服务器上。在else if 语句中,我想搜索以前输入的名称,以将输入的名称(地址字段为空白)与为该名称输入的相应地址相匹配。

For example:
First entered details: Name=John, Address=Ireland,
Second entered details: Name=Mary, Address=France,
Third entered details: Name=John, Address=null,(EMPTY FIELD)

当为“John”输入第三个详细信息时,我想检索之前为该用户输入的 John 的地址(即:爱尔兰)。无法弄清楚为什么它不起作用。任何帮助表示赞赏。谢谢

@WebServlet("/Test2") // tells server under which URL to offer this servlet
public class UserRegistration extends HttpServlet {

    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        // set content-type header before accessing the Writer
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        // then write the response
        out.println("<html>" + "<head><title>Online Shopping Directory</title></head>");
        //Get the identifier of the book to display
        out.println("<body bgcolor=\"#ffffff\">"
                + "<h2>Please enter your name:</h2>" + "<form method=\"get\">"
                + "<input type=\"text\" name=\"username\" size=\"25\">"
                + "<p></p>"
                + "<h2>Please enter your address:</h2>" + "<form method=\"get\">"
                + "<input type=\"text\" name=\"useraddress\" size=\"25\">"
                + "<p></p>"
                + "<input type=\"submit\" value=\"Submit\">"
                + "<input type=\"reset\" value=\"Reset\">"
                + "</form>");

        String name = request.getParameter("username");
        String address = request.getParameter("useraddress");
        HttpSession session = request.getSession(true);

        if ((name != null) && (name.length() > 0) && (address != null) && (address.length() > 0)) {

            session.setAttribute("username", address);
            out.println("The username " + name + " has been saved for "
                    + "this session. The address of this user is "
                    + (String) session.getAttribute("username"));
        } else if ((name.equals("username")) && (address == null)) {
            out.println("The username " + name + " is already saved. "
                    + "The address of this user is "
                    + (String) session.getAttribute("username"));
        }
        out.println("</body></html>");
        out.close();
    }
}

【问题讨论】:

  • 你的目标是意见箱吗?
  • 我是编程新手,我只是在寻找一些帮助来解决我花了很多时间试图弄清楚自己的问题。如果帮助是像你说的那样以“建议”的形式出现,我可以,但如果你不想提供帮助,我也可以。
  • 对不起,我的错误。我的意思是显示之前输入的数据和输入的信息的方法。
  • 试试request.getSession(false)

标签: java jsp session servlets session-variables


【解决方案1】:

您必须执行以下操作:

 if ((name != null) && (name.length() > 0) ){

 session.setAttribute("username", name);

out.println("The username " + name  );


}else {
out.println("The username " + (String) session.getAttribute("username") );
}





if ((address != null) && (address.length() > 0)){
session.setAttribute("address", address);

  out.println("The address " + adress );

}else {
out.println("The address " + (String) session.getAttribute("address") );
}

请给我一些反馈。

希望对您有所帮助。

【讨论】:

  • 感谢您的帮助。这对我帮助很大,很抱歉这么慢才承认帮助!
【解决方案2】:

如果你想弹出一个列表,如果用户在已经输入的文本中输入了某个前缀,你必须先存储文本。使用列表对象在提交到服务器时添加信息并将其存储在用户的会话中。请记住,当会话超时或被删除时,您的列表将不再可用。要以更持久的方式存储输入信息列表,您可以改用数据库。

当用户请求页面时,应在其中输入信息,您从她的会话中检索列表并将其写入页面(查找自动建议框以了解如何使用 javascript 进行操作)。

【讨论】:

    【解决方案3】:

    您实际上是在创建一个新的会话实例,您应该在需要检索属性时获取与请求关联的会话实例

    使用request.getSession(false); 这将返回与请求关联的会话或返回null。您在管理会话时应小心处理会话,即何时创建新会话或检索已为用户创建的会话

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2011-06-07
      • 2023-03-10
      相关资源
      最近更新 更多