将数据从Java Servlet传递到JSP?

huy*_*huy 18 java google-app-engine jsp servlets

我一直是PHP开发人员,但最近需要使用Google App Engine(Java)开展一些项目.在PHP中,我可以做这样的事情(根据MVC模型):

// controllers/accounts.php
$accounts = getAccounts();
include "../views/accounts.php";

// views/accounts.php
print_r($accounts);
Run Code Online (Sandbox Code Playgroud)

我使用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"));
  }
}
Run Code Online (Sandbox Code Playgroud)

基本上在Java情况下,它是2个不同的HTTP请求(第二个是自动强制的),对吧?所以在JSP文件中我无法利用Servlet中计算的数据.

有没有什么方法可以做到类似于PHP的方式?

ash*_*ram 31

您需要在请求范围内设置servlet中检索的数据,以便在JSP中提供数据

您将在servlet中拥有以下行.

List<Account> accounts = getAccounts();  
request.setAttribute("accountList",accounts);
Run Code Online (Sandbox Code Playgroud)

然后在JSP中,您可以使用下面的表达式语言访问此数据

${accountList}
Run Code Online (Sandbox Code Playgroud)

我会使用请求调度而不是sendRedirect如下

  RequestDispatcher rd = sc.getRequestDispatcher(url);
  rd.forward(req, res);
Run Code Online (Sandbox Code Playgroud)

如果您可以使用,RequestDispatcher那么您可以将这些值存储在requestsession对象中,并获取其他JSP.

是否有使用的具体目的request.sendRedirect?如果不使用RequestDispatcher.

有关详细信息,请参阅此链接.

public class AccountServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    List<Account> accounts = getAccountListFromSomewhere();

    String url="..."; //relative url for display jsp page
    ServletContext sc = getServletContext();
    RequestDispatcher rd = sc.getRequestDispatcher(url);

    request.setAttribute("accountList", accounts );
    rd.forward(request, response);
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 还有getServletContext().getRequestDispatcher(url).forward(request,response); (3认同)

Evi*_*l E 8

您要做的是首先定义一个对象来表示来自getAccounts()的信息 - 类似于AccountBean.

然后在servlet的doPost或doGet函数中,使用请求信息填充AccountBean对象.

然后,您可以使用setAttribute方法将AccountBean对象存储在请求,会话或servlet上下文中,并将请求转发到JSP页面.

使用和标记提取jsp页面中的AccountBean数据.

这可能是您的servlet的一个示例:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) {

  // get data from request querystring
  String accountId = req.getParameter("accountid");

  // populate your object with it (you might want to check it's not null)
  AccountBean accountBean = new AccountBean(accountId);

  // store data in session
  HttpSession session = req.getSession();
  session.setAttribute("accountBean", accountBean);

  // forward the request (not redirect)
  RequestDispatcher dispatcher = req.getRequestDispatcher("account.jsp");
  dispatcher.forward(req, resp);
}
Run Code Online (Sandbox Code Playgroud)

然后您的JSP页面将显示以下内容以显示帐户信息:

<jsp:useBean id="accountBean" type="myBeans.AccountBean" />
Your account is <jsp:getProperty name="accountBean" property="status" />
Run Code Online (Sandbox Code Playgroud)


小智 5

除了上面提到的关于使用表达式lang的内容之外,您还可以通过请求本身传递属性.在Servlet的doGet()中,我们写了类似的东西:

Account[] accounts = AccountManager.getAccountList();
request.setAttribute("accountList", accounts );
RequestDispatcher rd = req.getRequestDispatcher(nextJSPurl);
rd.forward(req, resp);
Run Code Online (Sandbox Code Playgroud)

在JSP中,我们可以从请求中检索属性:

 <%
    Account[] accounts= (Account[])request.getAttribute("accountList");

       if (accounts.length>0) {       
       for (Account account: accounts) {            
                %>
                <blockquote>account name: <%= account.getName() %></blockquote>
                <%
            }
        }
 %>
Run Code Online (Sandbox Code Playgroud)