在jsp中显示消息 [英] Display a message in jsp

查看:183
本文介绍了在jsp中显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,loginMsg显示null。谁能知道为什么?

预期结果是当用户注销时它应该使会话无效并转到登录页面Logout!消息。怎么做?我正在使用Java



In this code 'loginMsg' displays 'null'. can anyone know why?
Expected outcome is when user logout it should invalidate session and go to login page with "Logout!" message.How to do that? I'm Using Java

private void logoutSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        HttpSession  session = request.getSession();
        try{
            session.invalidate();
            String message = "Logout!";
            request.setAttribute("loginMsg", message);
            response.sendRedirect(request.getContextPath() + "/login2.jsp");
            //session.removeAttribute("loggedUser");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

推荐答案

Hello Mali,



由于您正在执行 sendRedirect ,因此浏览器通常会收到301响应,如下所示。此响应指示浏览器已将请求的资源移动到其他位置,并且浏览器应对响应的location属性中指定的URL发出新请求。因此,浏览器会生成一个新请求并将其发送到服务器,这就是新请求中 loginMsg 属性值为空的确切原因。
Raw Redirect Ressponse

Hello Mali,

Since you are doing a sendRedirect, the browser is typically going to receives 301 response as shown below. This response instructs the browser that the requested resource has been moved to a different location and the browser should make a new request to the URL specified in the location attribute of the response. Thus the browse generates a new request and sends it to the server, that's the precise reason why the value of loginMsg attribute is null in the new request.
Raw Redirect Ressponse
HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/
Content-Type: text/html
Content-Length: 174
 
<html>
<head>
<title>Moved</title>
</head>
<body>
<h1>Moved</h1>
<p>This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.</p>
</body>
</html>



为了能够在重定向后显示loginMsg,您可以通过两种方式进行操作。


To be able to display the loginMsg after redirect you can do it in two ways.



  1. 将loginMsg作为URL的一部分,如下所示。然后在你的login2.jsp中通过queystring( HttpServletRequest.getparameter(String))检索此消息并显示它。


  1. Put the loginMsg as part of the URL as shown below. Then in your login2.jsp retrieve this message via queystring (HttpServletRequest.getparameter(String)) and display it.
response.sendRedirect(request.getContextPath() + "/login2.jsp?loginMsg=" + URLEncoder.encode(message));



login2的部分代码。显示消息的jsp如下所示


Partial code for login2.jsp to display the message is shown below

<%@ page info="Login" language="java" contentType="text/html;charset=utf-8" %<

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:out value="


{param.loginMsg} / >
//剩余的HTML代码
...
{param.loginMsg}"/> // Your remaining HTML Code ...

  • 在HttpSession中添加登录消息(在重定向之前,在使当前无效之后) session)并在login2.jsp中检索此值以显示消息。

    注意:请不要忘记在登录之前在登录servlet中调用session.invalidate()。
  • Add the login message in HttpSession (just before the redirect, after invalidating the current session) and retrieve this value in login2.jsp to display the message.
    Note: Please don't forget to call session.invalidate() in your login servlet before you actually login.


  • 问候,


    Regards,


    尝试EL(表达式语言)而不是jsp scriptlet。只需输入
    Try EL(Expression Language) instead of jsp scriptlet. Simply put


    这篇关于在jsp中显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆