避免Jsp中的Scriptlet在页面加载时显示数据

问题描述:

我意识到,当你在jsp中提交表单时,在映射的servlet中可以获得所需的数据,将其设置在合适的范围内(比如request),并将其转发给jsp,如下所示:

I realize that when you submit the form in a jsp, in the mapped servlet you can get the desired data, set it in the proper scope(say request) and forward it to jsp like this:

request.setAttribute("myList", myList); // Store list in request scope.
request.getRequestDispatcher("/index.jsp").forward(request, response);

然而,我想知道没有窗体或换句话说我们想显示数据的页面只要页面加载,我们如何有效地加载数据,而不使用scriptlet,比如

However am wondering for pages which doesn't have a form or in other words we want to display data as soon as page loads, how can we efficiently load the data without using scriptlets like

<%= myBean.populateData("String Argument_1")%>

非常感谢任何人都可以提供任何建议。

Would highly appreciate if anyone can provide any recommendations around the same.

请求来自表单的事实不会改变任何内容。 servlet接收到一个请求,然后可以执行一些处理并转发到JSP:

The fact that the request comes from a form or not doesn't change anything. The servlet receives a request, and then can do some processing and forward to a JSP:


  1. servlet获取请求参数

  2. servlets使用这些参数从数据库中获取请求的数据,并用所述数据填充一些bean。它还可以从头开始构建一些bean,以显示具有默认值的表单

  3. servlet将这些bean放入请求属性中

  4. servlet转发到JSP

  5. JSP避免使用scriptlet,而是使用JSP EL,JSTL和自定义标签来显示存储在请求范围中的bean中的信息。
  1. servlet gets request parameters
  2. servlets uses those parameters to get requested data from a database, and populate some beans with said data. It may also build some beans from scratch, to display a form with default values
  3. servlet puts those beans in request attributes
  4. servlet forwards to a JSP
  5. JSP avoids using scriptlets and rather uses JSP EL, the JSTL and custom tags to display the information stored in the beans in request scope