Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSP重定向问题_Java_Jsp_Redirect_Include_Forward - Fatal编程技术网

Java JSP重定向问题

Java JSP重定向问题,java,jsp,redirect,include,forward,Java,Jsp,Redirect,Include,Forward,我对jsp中的重定向没有什么问题 login.jsp <% Object user = (String) request.getSession().getAttribute("User"); if(user != null){ PrintWriter out = response.getWriter(); out.println("<font color=red>You are already logged &l

我对jsp中的重定向没有什么问题

login.jsp

<%
Object user = (String) request.getSession().getAttribute("User"); 
if(user != null){
                PrintWriter out = response.getWriter();
                out.println("<font color=red>You are already logged </font>");
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
                rd.forward(request, response);
            }
                %>


当用户尝试访问login.jsp页面时,当他已经登录时,该页面应该将他重定向到主页面(index.jsp)。它可以工作,但当我使用前向重定向时,我看不到来自out.println~的消息,而且当我使用include重定向时,我有两个主页(但带有消息:D)。我怎样才能修好它?在jsp页面中不使用java代码是否有更好的方法来实现这一点?

您可以使用
response.sendRedirect(“url”)

如果您希望用户短暂地看到一条消息,然后将其放在另一个url上,则该消息不称为(HTTP)重定向,这是普通的旧元刷新:

<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="refresh" content="2;URL=http://www.example.com" >
  </head>
  <body>
    A brief message displayed for 2 seconds...
  </body>
</html>

短消息显示2秒钟。。。