java输出。打印方法不起作用?
这是我的密码
登录控制器。爪哇
包mvc。演示。控制
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.demo.model.Authenticate;
public class LoginController extends HttpServlet {
private static final long SerialVersionUID=1L;
public LoginController()
{
super();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String s1=req.getParameter("username");
String s2=req.getParameter("password");
RequestDispatcher rd=null;
Authenticate au=new Authenticate();
String result=au.authorise(s1, s2);
if(result.equals("success"))
{
rd=req.getRequestDispatcher("/success.jsp");
}
else
{
//This is the point where i try to print error message on jsp.
PrintWriter out = resp.getWriter( );
out.print("Sorry UserName or Password Error!");
rd=req.getRequestDispatcher("/login.jsp");
rd.include(req, resp);
}
rd.forward(req, resp);
}
}
登录。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>
<title>Login Page</title>
</head>
<body>
<form method="post" action="LoginController" >
UserName: <input type="text" name="username"><BR><BR>
PassWord: <input type="password" name="password"><BR><BR>
<input type="submit" />
</form>
</body>
</html>
请检查loginController类的else块,我试图在我的jsp文件上打印错误消息,当前“out.print”方法无法反映我的登录。jsp文件。 请帮我整理这个问题,提前谢谢
# 1 楼答案
# 2 楼答案
如果您想在Jsp中打印错误消息,并首先在请求中设置错误消息
现在,您可以通过使用EL表达式在jsp中获得相同的结果
或者你可以使用脚本语言来实现
将Obj键入字符串,然后使用以下命令打印
注意:-建议使用El表达式
# 3 楼答案
您可以在请求属性中设置错误消息,并可以在JSP中获取它
现在在JSP上
# 4 楼答案
我看不见外面。在代码中使用close()。最后必须关闭PrintWriter对象