我开发了一个示例登录页面来验证用户名和密码。如果用户提供正确的凭据,该页面将导航到其他页面,否则从该页面返回一条消息servlet
到同一个登录页面。
我已附上样品code
here:
FirstJSP.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>`
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">`
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>`
</head>
<body>
<form action="Login" method="post">
<input type="text" name="Name"/><br>
<input type="password" name=Pass><br><br><br>
<input type="submit" value="submit"/><br>
<%if(request.getAttribute("message")!=(""))
out.println(request.getAttribute("message"));
else
out.println("");%>
<h1>Hi This is JSP sample code </h1>
<font color="blue" size="25">
<marquee>Please login to Work with JSP</marquee></font>
<%java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy"); %>
<h1>Current Date: <%= df.format(new java.util.Date()) %> </h1>
</form>
</body>
</html>
And servlet
代码,Login.java:
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("Name");
String password = request.getParameter("Pass");
response.setContentType("text/html");
if(username.equals("sugan") && password.equals("raj")) {
response.getWriter().println("<html><body><Marquee>Welcome to JSP!!!</marquee></body></html>");
}
else {
String message = "OOps!!! Invalid Username/Password";
request.setAttribute("message", message);
request.getRequestDispatcher("/FirstJSP.jsp").forward(request, response);
}
}
}
它工作正常,但是在加载登录页面时它会自动显示Null
。然后,如果用户提供了错误的凭据,它会显示实际的消息。我如何禁用Null
运行时消息?
您使用以下命令将消息与空字符串进行比较==
.
首先,您的比较是错误的,因为消息将为空(而不是空字符串)。
其次,这是错误的,因为对象必须与equals()
并且不与==
.
第三,这是错误的,因为您应该避免在 JSP 中使用 scriptlet,而应使用 JSP EL、JSTL 和其他自定义标记:
<c:id test="${!empty message}">
<c:out value="${message}"/>
</c:if>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)