在JSP页面中作为null接收的变量值

Tin*_*uar 3 java jsp servlets

我做了一个简单的登录应用程序.但是每当发送表单时,servlet都会收到空数据.

以下是登录页面

<body>
<form action="login">
    Enter login Name : <input type="text" name="userName"/><br/>
    Enter password   : <input type="password" name="userPassword"/><br/>
    <input type="submit" value="Log In"/>
</form>
Run Code Online (Sandbox Code Playgroud)

以下是用于检查输入的变量值的servlet代码

@WebServlet("/login")
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public loginServlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name = (String)request.getAttribute("userName");
    String password = (String)request.getAttribute("userPassword");

    System.out.print("in doGet method");
    System.out.print(name+" "+password+" are these");


    if(name!=null && password!=null){
        System.out.print("in null method");
        if(name=="admin" && password=="admin"){
//              Cookie cookie = new Cookie("userName", name);
//              response.addCookie(cookie);
//              RequestDispatcher dispatch = request.getRequestDispatcher("success.jsp");
//              dispatch.forward(request, response);

            response.sendRedirect("success.jsp");
        }
        else{
            System.out.print("in error method");
            response.sendRedirect("error.jsp");
        }
    }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
Run Code Online (Sandbox Code Playgroud)

}

我正在使用System.out.print方法在Tomcat控制台中打印值.

Ell*_*sch 5

这些电话 Request.getAttribute(String)

String name = (String)request.getAttribute("userName");
String password = (String)request.getAttribute("userPassword");
Run Code Online (Sandbox Code Playgroud)

应调用Request.getParameter(String)(根据Javadoc,返回请求参数的值作为aString).喜欢

String name = request.getParameter("userName");
String password = request.getParameter("userPassword");
Run Code Online (Sandbox Code Playgroud)

也,

if(name=="admin" && password=="admin"){
Run Code Online (Sandbox Code Playgroud)

应该是这样的

if (name.equals("admin") && password.equals("admin")) {
Run Code Online (Sandbox Code Playgroud)

因为StringObject,你想要等价的值(不是相同的引用).