繁体   English   中英

Java中的范围变量。 如何在一个范围内链接两个变量?

[英]Scoped variables in Java. How to link two variables within one scope?

我正在尝试调用存储为会话范围变量的变量。 如果我运行第一个if语句,则范围变量调用将起作用。 当我运行else语句时,仅输入一个名称,不输入任何地址(而是先前输入的名称和存储的地址),则不返回与该名称匹配的地址。 任何帮助将不胜感激,因为我对此有点迷惑。 谢谢

@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();
        session.setAttribute("username", address);

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

            out.println("The username " + name + " has been saved for "
                    + "this session. The address of this user is "
                    + (String) session.getAttribute("username"));
        } else if ((name == "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();
    }
}

我认为您应该使用等于而不是==

 if ((name.equals("username")) && (address.equals(""))) {
        out.println("The username " + name + " is already saved. The address of this user is " + (String) session.getAttribute("username"));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM