如何从servlet发送消息并在jsp中显示

发布于 2024-11-08 02:26:07 字数 446 浏览 0 评论 0原文

我正在尝试做一些看起来很小但失败了的事情。我试图在登录失败时将响应消息发送回 jsp 但无法。截至目前,我只能重定向回 jsp,但无法在其上显示来自 servlet 的消息。这是重定向的 servlet 部分:

                if (count > 0) {
                res.sendRedirect("adminHome.jsp");
            } else {

                res.sendRedirect("index.jsp");
            }

我尝试使用 PrintWriter 和重定向来打印消息,但失败了,因为我无法了解如何在 JSP 中接收消息。我还读到我不应该重定向,而应该从 servlet 转发。我该怎么做?请帮助修复从 servlet 转发的代码补丁以及在 JSP 中接收的代码补丁。谢谢

I'm trying to do something that looks small but it's failing. I'm trying to send a response message back to a jsp when login fails but not being able. As of now I can only redirect back to the jsp but cannot display a message from the servlet on it. This is the servlet part of the redirection:

                if (count > 0) {
                res.sendRedirect("adminHome.jsp");
            } else {

                res.sendRedirect("index.jsp");
            }

I tried to print a message using PrintWriter and the redirect but failed because I couldn't get how to receive the message in the JSP. I also read that I shouldn't redirect but rather I should just forward from the servlet. How can I do this? Please help with the code patch to forward from servlet as well as that one to receive in JSP. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-11-15 02:26:07

如果您坚持使用重定向而不是转发,那么您有 2 个选择:

  1. 将消息作为请求参数传递

    字符串消息=“你好”;
    res.sendRedirect("adminHome.jsp?message=" + URLEncoder.encode(message, "UTF-8"));
    

    这样就可以在JSP中显示如下

    消息:${param.message}

    它也只在浏览器地址栏中可见,并且您无法通过这种方式传递非标准 Java 对象。

  2. 将其存储在会话中

    字符串消息=“你好”;
    req.getSession().setAttribute("消息", 消息);
    res.sendRedirect("adminHome.jsp");
    

    这样您就可以在 JSP 中显示(和删除)它,如下所示:

    消息:${message}

    删除很重要,否则它会在整个会话中保留在那里。


但是,如果您愿意使用转发而不是重定向,它会更优雅:

String message = "hello";
req.setAttribute("message", message);
req.getRequestDispatcher("/adminHome.jsp").forward(req, res);

并在 JSP 中按如下方式显示它

    <p>Message: ${message}</p>

另请参阅:

If you insist to use redirect instead of forward, then you have 2 options:

  1. Pass the message as request parameter

    String message = "hello";
    res.sendRedirect("adminHome.jsp?message=" + URLEncoder.encode(message, "UTF-8"));
    

    so that you can display it in JSP as follows

    <p>Message: ${param.message}</p>
    

    It's only visible in the browser address bar as well and you aren't able to pass non-standard Java objects this way.

  2. Store it in session

    String message = "hello";
    req.getSession().setAttribute("message", message);
    res.sendRedirect("adminHome.jsp");
    

    so that you can display (and remove) it in JSP as follows:

    <p>Message: ${message}</p>
    <c:remove var="message" scope="session" /> 
    

    Removing is important, otherwise it sticks there for the entire session.


However, if you're open to using forward instead of redirect, it's more elegant:

String message = "hello";
req.setAttribute("message", message);
req.getRequestDispatcher("/adminHome.jsp").forward(req, res);

and display it as follows in JSP

    <p>Message: ${message}</p>

See also:

月亮邮递员 2024-11-15 02:26:07

将消息设置为请求对象的属性:

request.setAttribute("message", messageString);

使用 RequestDispatcher 将请求和响应对象分派到 jsp:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("urlString");
dispatcher.forward(request, response);

使用表达式语言语法在 JSP 中访问它:

${message}

Set your message as an attribute of the request object:

request.setAttribute("message", messageString);

Use a RequestDispatcher to dispatch the request and response objects to the jsp:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("urlString");
dispatcher.forward(request, response);

Access it in your JSP using Expression Language syntax:

${message}
丢了幸福的猪 2024-11-15 02:26:07

您可以将消息保存在请求中,然后转发到 jsp 而不是重定向。

    request.setAttribute("msg", "The actual error message to be displayed");

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/yourpage.jsp");
    rd.forward(request, response);

然后在 jsp 页面上您可以通过请求访问该消息。

You can save the message in the request and then forward to the jsp rather than redirect.

    request.setAttribute("msg", "The actual error message to be displayed");

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/yourpage.jsp");
    rd.forward(request, response);

Then on the jsp page you have access to the message via the request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文