如何在jsp页面加载时调用servlet

use*_*263 9 java jsp servlets java-ee

我想latest_products在页面加载时调用一个servlet .index.jsp这个servlet在List中有记录.我想传递List<products>给你index.jsp.但我不想在url中显示servlet的名称.有什么方法可以做到这一点.

Bra*_*raj 12

解决方案1

要遵循的步骤:

  • 用于jsp:include从JSP调用Servlet,该JSP将在运行时包含Servlet在JSP中的响应
  • 在Servlet中设置请求中的属性,然后在JSP中读取它

示例代码:

JSP:

<body>
    <jsp:include page="/latest_products.jsp" />
    <c:out value="${message }"></c:out>
</body>
Run Code Online (Sandbox Code Playgroud)

Servlet的:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {            
    request.setAttribute("message", "hello");
}
Run Code Online (Sandbox Code Playgroud)

编辑

但我不想在url中显示servlet的名称.

简单地url-pattern为Servlet 定义一个不同且有意义的,web.xml如下所示,看起来像一个JSP页面,但在内部它是一个Servlet.

web.xml中:

<servlet>
    <servlet-name>LatestProductsServlet</servlet-name>
    <servlet-class>com.x.y.LatestProductsServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LatestProductsServlet</servlet-name>
    <url-pattern>/latest_products.jsp</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

解决方案2

要遵循的步骤:

  • 首先调用Servlet
  • 处理最新产品
  • 在请求属性中设置列表
  • 将请求转发到JSP,使用JSTL可以在JSP中轻松访问它

示例代码:

Servlet的:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {            
    request.setAttribute("message", "hello");
    RequestDispatcher view=request.getRequestDispatcher("index.jsp");
    view.forward(request,response);
}
Run Code Online (Sandbox Code Playgroud)

的index.jsp:

<body>      
    <c:out value="${message }"></c:out>
</body>
Run Code Online (Sandbox Code Playgroud)

点击URL:scheme://domain:port/latest_products.jsp将调用Servlet的doGet()方法.


Lui*_*oza 6

(...)但我不想在url中显示servlet的名称.

访问Servlet时,根本不需要使用Servlet名称.实际上,您可以通过定义正确的URL模式来创建servlet以指向所需的URL :

@WebServlet("/index.jsp")
public class LatestProductServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        List<Product> productList = ...
        //all the necessary code to obtain the list of products
        //store it as request attribute
        request.setAttribute("productList", productLlist);
        //forward to the desired view
        //this is the real JSP that has the content to display to user
        request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将拥有这样的文件夹结构

- <root folder>
  - WEB-INF
    + index.jsp
    + web.xml
Run Code Online (Sandbox Code Playgroud)

在WEB-INF/index.jsp中:

<!DOCTYPE html>
<html lang="es">
    <body>
        <!--
            Display the data accordingly.
            Basic quick start example
        -->
        <c:forEach items="${productList}" var="product">
            ${product.name} <br />
        </c:forEach>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

请注意,您的客户将访问http://<yourWebServer>:<port>/<yourApplication>/index.jsp并获得所需的内容.而且您不需要触发两个GET请求来检索特定页面的内容.

请注意,您可以进一步使用此方法:由于现在在servlet中定义了模式,因此您可以选择不为您的客户端使用index.jsp,而是使用index.html访问您的页面.只需更改Servlet中的URL模式:

@WebServlet("/index.html")
public class LatestProductServlet extends HttpServlet {
     //implementation...
}
Run Code Online (Sandbox Code Playgroud)

客户端将访问http://<yourWebServer>:<port>/<yourApplication>/index.html,这将在WEB-INF/index.jsp中显示结果.现在,您的客户和您都会感到高兴:客户将获取他们的数据,您不会在URL中显示丑陋的servlet名称.


额外

如果在web.xml中将index.jsp作为欢迎页面,则此方法将不起作用,因为应用程序servlet期望存在真正的文件index.jsp.要解决此问题,只需创建一个名为index.jsp的空文件:

- <root folder>
  - index.jsp  <-- fake empty file to trick the application server
  - WEB-INF
    + index.jsp
    + web.xml
Run Code Online (Sandbox Code Playgroud)

当访问时http://<yourWebServer>:<port>/<yourApplication>/,将如上所示工作.