Java中的Servlet。如何检索属性?

时间:2014-02-02 13:38:23

标签: java jsp session servlets session-variables

我真的在努力解决这个问题。我将用户的名称和地址存储为服务器上的会话变量。在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(即:Ireland)的地址。无法弄清楚为什么它不会起作用。任何帮助表示赞赏。感谢

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

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您必须执行以下操作:

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

请给我一些反馈。

希望有所帮助。

相关问题