簡體   English   中英

Java中的Servlet。 如何檢索屬性?

[英]Servlet in Java. How to retrieve attributes?

我真的在這個問題上掙扎。 我將用戶的名稱和地址存儲為服務器上的會話變量。 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)

當為“約翰”輸入這些第三個詳細信息時,我要檢索先前為該用戶輸入的約翰的地址(即愛爾蘭)。 無法弄清楚為什么它不起作用。 任何幫助表示贊賞。 謝謝

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

如果要彈出列表,則如果用戶為已經輸入的文本輸入了某個前綴,則必須先存儲文本。 使用列表對象在信息提交到服務器時添加信息,並將其存儲在用戶會話中。 請記住,當會話超時或被刪除時,您的列表將不再可用。 要以更持久的方式存儲輸入信息的列表,可以改用數據庫。

當用戶請求應在其中輸入信息的頁面時,您將從其會話中檢索列表並將其寫入頁面(查找自動建議框以查找使用JavaScript的方法)。

您實際上是每次都在創建一個新的會話實例,您應該在獲取屬性時獲取與請求關聯的會話實例。

使用request.getSession(false); 這將返回與請求關聯的會話或返回null。 您在管理會話時應謹慎處理會話,即何時創建一個新會話或檢索為該用戶創建的會話

您必須執行以下操作:

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

請給我一些反饋。

希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM