【问题标题】:Sending messages from Servlet -> Servlet and Servlet->JSP从 Servlet -> Servlet 和 Servlet->JSP 发送消息
【发布时间】:2015-06-07 00:27:44
【问题描述】:

我有一个 Web 应用程序,我必须将消息从一个 servlet 发送到另一个,并将 servlet 发送到 JSP。默认的做法是使用RequestDispatcher 并调用request.setAttribute("name",message);

没错,但我在某些地方必须使用 response.sendRedirect() 才能停止手动引用时多次提交表单。

在这种情况下,最知名的发送消息的方法是使用 Session 并将消息添加到会话中。

但是使用会话传递消息并不是一个好主意,因为消息实际上是一个应该属于request scope 的项目。那么,还有其他将它传递给 servlet 和 Jsp 的好方法吗?在 JSP 中,我使用的是 JSTL,所以请告诉我如何使用它来获取传递的对象。

【问题讨论】:

  • 你在使用 Struts 2 吗?
  • @exoddus: 没有............

标签: java jsp servlets httpresponse


【解决方案1】:

可能你可以使用cookiesQuery Stringurl re-writing

查询字符串:

String path = 'next.jsp';
path += '?name=' + messge;

你可以使用:

response.sendRedirect(path);

request.getRequestDispatcher(path).forward(request, response);

在登陆页面你可以通过request.getParameter("name")得到消息

【讨论】:

    【解决方案2】:

    如果您使用任何框架,如 Struts 2,您可以将其存储到 application scope https://struts.apache.org/docs/accessing-application-session-request-objects.html

    如果您使用的是简单的 Servlet 并且 session 没有足够的空间来存储您想要的内容,您可以考虑将其存储到文件磁盘或数据库中,然后从您想要的任何 Servlet 中读取它。

    如果是你的情况,这个问题是相关的:What is the best place to store a configuration file in a Java web application (WAR)?

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 2013-08-13
      • 2011-08-28
      • 1970-01-01
      • 2020-03-06
      • 2013-03-29
      • 2021-08-03
      相关资源
      最近更新 更多