将数据从 Java Servlet 传递到 JSP?
我曾经是一名 PHP 开发人员,但最近需要使用 Google App Engine (Java) 处理一些项目。在 PHP 中,我可以做这样的事情(就 MVC 模型而言):
// controllers/accounts.php
$accounts = getAccounts();
include "../views/accounts.php";
// views/accounts.php
print_r($accounts);
我看一下使用 Servlet 和 JSP 的 Google App Engine Java 的一些演示。他们正在做的是这样的:
// In AccountsServlet.java
public class AccountsServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("accountid");
// do something
// REDIRECT to an JSP page, manually passing QUERYSTRING along.
resp.sendRedirect("/namedcounter.jsp?name=" + req.getParameter("name"));
}
}
基本上在 Java 情况下,它是 2 个不同的 HTTP 请求(第二个请求是自动强制的),对吧?所以在JSP文件中我无法使用Servlet中计算的数据。
有什么方法可以像 PHP 那样做到这一点吗?
I've been a PHP developer but recently need to work on some project using Google App Engine (Java). In PHP I can do something like this (in term of MVC model):
// controllers/accounts.php
$accounts = getAccounts();
include "../views/accounts.php";
// views/accounts.php
print_r($accounts);
I take a look at some demos of Google App Engine Java using Servlet and JSP. What they're doing is this:
// In AccountsServlet.java
public class AccountsServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("accountid");
// do something
// REDIRECT to an JSP page, manually passing QUERYSTRING along.
resp.sendRedirect("/namedcounter.jsp?name=" + req.getParameter("name"));
}
}
Basically in the Java case it's 2 different HTTP requests (the second one being automatically forced), right? So in JSP file I can't make use of the data calculated in the Servlet.
Is there some way I can do it similar to the PHP way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要在请求范围内设置在 servlet 中检索的数据,以便数据在 JSP 中可用
。您的 servlet 中将包含以下行。
然后在 JSP 中,您可以使用如下所示的表达式语言访问此数据
我将使用请求分派而不是
sendRedirect
,如下所示如果您可以使用
RequestDispatcher
那么您可以存储这些值在request
或session
对象中并获取其他 JSP。使用 request.sendRedirect 是否有任何特定目的?如果不使用RequestDispatcher。
请参阅此链接了解更多详细信息。
You will need to set the data retrieved in the servlet in request scope so that the data is available in JSP
You will have following line in your servlets.
Then in JSP you can access this data using the expression language like below
I would use request dispatches instead of the
sendRedirect
as followsIf you can use
RequestDispatcher
then you can store these values inrequest
orsession
object and get in other JSP.Is there any specific purpose of using
request.sendRedirect
?. If not useRequestDispatcher
.See this link for more details.
您要做的就是首先定义一个对象来表示 getAccounts() 中的信息 - 类似于 AccountBean。
然后在您的 servlet doPost 或 doGet 函数中,使用请求信息来填充您的 AccountBean 对象。
然后,您可以使用 setAttribute 方法将 AccountBean 对象存储在请求、会话或 servlet 上下文中,并将请求转发到 JSP 页面。
jsp 页面中的 AccountBean 数据是使用 和 标记提取的。
下面可能是您的 servlet 的示例:
然后您的 JSP 页面将具有以下内容来显示帐户信息:
What you want to do is first define an object to represent the information from getAccounts() - something like AccountBean.
Then in your servlets doPost or doGet function, use the request info to populate your AccountBean object.
You can then store the AccountBean object either in the request, session, or servlet context by using the setAttribute method, and forward the request to the JSP page.
The AccountBean data in your jsp page is extracted using the and tags.
Here might be an example of your servlet:
Then your JSP page would have the following to display the account information:
除了上面提到的使用表达式 lang 之外,您还可以通过请求本身传递属性。在 Servlet 的 doGet() 中,我们这样写:
在 JSP 中,我们可以从请求中检索属性:
Besides what's mentioned above about using expression lang, you can also pass attributes via request itself. In Servlet's doGet(), we write something like:
In JSP, we can retrieve the attribute from request:
在上面的代码中,servlet 不会创建 2 个不同的请求。它将转发,还将保留原始请求中的所有数据。
In the above code servlet will not create 2 different requests. It will forward, also will retain all data from original request.
这是我对你的问题的理解 - 你想重定向或分派到一个新的 JSP 页面以及 Servlet 中计算的数据,对吧?为此,您需要在分派请求之前设置请求属性。
您可以使用
HttpServletRequest
对象 (req.setAttribute("attribute name", "attribute value")
) 设置属性。属性值可以是任何Java对象。您可以通过
req.getAttribute("attribute name")
检索该值。您还需要在使用 getAttribute() 函数时对对象进行类型转换。This is my understanding of your question - you want to redirect or dispatch to a new JSP page along with the data calculated in Servlet, right? To do so you need to set request attributes before dispatching the request.
You can set attributes using
HttpServletRequest
object (req.setAttribute("attribute name", "attribute value")
). Attribute value can be any Java object.You can retrieve the value by
req.getAttribute("attribute name")
. You'll also need to type cast the object while user getAttribute() function.您可以在 java beans 中设置数据,并在控制权转到 jsp 时轻松访问 jsp 页面上的数据。使用setter在java bean中设置日期通过将该bean包含到jsp中来访问jsp页面上的这些数据。
这样你就可以访问你的 bean 类可以拥有的尽可能多的属性。
You can set the data inside java beans and easily access that data onto jsp page when control goes to jsp. set the date in java beans using setters get access those data onto jsp page by including that bean into jsp.
like this you can access as many properties your bean class can have.