本文介绍了将数据从Java Servlet传递到JSP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre code $ controller / accounts.php
$ accounts = getAccounts();
包含../views/accounts.php;

// views / accounts.php
print_r($ accounts);

我使用Servlet和JSP查看Google App Engine Java的一些演示。他们正在做的是这样的:

  //在AccountsServlet.java中
公共类AccountsServlet扩展HttpServlet {
$ b @Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException {
String action = req.getParameter(accountid);
//执行某些操作
//重定向到JSP页面,手动传递QUERYSTRING。
resp.sendRedirect(/ namedcounter.jsp?name =+ req.getParameter(name));






$ b基本上在Java情况下,它有2个不同的HTTP请求(第二个被自动强制),对吧?因此,在JSP文件中,我无法使用Servlet中计算的数据。



有没有什么办法可以像PHP那样做?

解决方案

您将需要在请求范围中设置servlet中检索的数据,以便数据在JSP中可用。
$ b

您将在您的servlet中拥有以下行。

  List< Account> accounts = getAccounts(); 
request.setAttribute(accountList,accounts);

然后在JSP中,您可以使用像下面这样的表达式语言来访问这些数据:

  $ {accountList} 

我将使用请求分派而不是 sendRedirect ,如下所示:

  RequestDispatcher rd = sc.getRequestDispatcher(url); 
rd.forward(req,res);

如果您可以使用 RequestDispatcher ,那么您可以将这些值存储在 request session 对象中,并存入其他JSP中。



是否有使用 request.sendRedirect ?的任何特定目的。如果不使用 RequestDispatcher



。 class AccountServlet extends HttpServlet {
$ b $ protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {

List< Account> accounts = getAccountListFromSomewhere();

String url =...; //显示jsp页面的相对url
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);

request.setAttribute(accountList,accounts);
rd.forward(request,response);
}
}


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?

解决方案

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.

List<Account> accounts = getAccounts();
request.setAttribute("accountList",accounts);

Then in JSP you can access this data using the expression language like below

${accountList}

I would use request dispatches instead of the sendRedirect as follows

  RequestDispatcher rd = sc.getRequestDispatcher(url);
  rd.forward(req, res);

If you can use RequestDispatcher then you can store these values in request or session object and get in other JSP.

Is there any specific purpose of using request.sendRedirect?. If not use RequestDispatcher.

See this link for more details.

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);
  }
}

这篇关于将数据从Java Servlet传递到JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:37