这是我的代码
LoginController.java
package mvc.demo.control;
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);
}
}
的login.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”方法无法反映在我的login.jsp文件中。 请帮我解决这个问题,提前谢谢。
答案 0 :(得分:2)
您可以在请求属性中设置错误消息,并可以在JSP
中获取它String errorMsg = "UserName or Password is invalid !";
req.setAttribute("errorMsg", errorMsg);
req.getRequestDispatcher("/login.jsp").forward(req, res);
现在在JSP上
<h2>${errorMsg}</h2> // Print wherever you need it
答案 1 :(得分:1)
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
'Album\Controller\Albums' => 'Album\Controller\AlbumsController', // added second controller
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'add',
),
),
),
// add route to new controller
'albums' => array(
'type' => 'segment',
'options' => array(
'route' => '/albums[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Album\Controller\Albums',
'action' => 'index',
),
),
),
),
),
// ...
答案 2 :(得分:1)
我在代码中看不到out.close()。您必须最后关闭PrintWriter对象。
答案 3 :(得分:0)
如果您想在Jsp中打印错误消息并首先在请求中设置错误消息
req.setAttribute("msg","error msg configure here");
现在你可以使用EL Expression在jsp中获得相同的效果。
${msg}
或者您可以使用脚本语言来获取它。
<%Object obj=request.getAttribute("msg");%>
将Obj转换为String并使用此
进行打印 <%=str%>
注意: - 建议使用El表达式。