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

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

解决方案1

要遵循的步骤:

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

示例代码:

JSP:

     

Servlet的:

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

编辑

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

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

web.xml中:

  LatestProductsServlet com.xyLatestProductsServlet   LatestProductsServlet /latest_products.jsp  

解决方案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); } 

的index.jsp:

    

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

(…)但我不想在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 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); } } 

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

 -  - WEB-INF + index.jsp + web.xml 

在WEB-INF / index.jsp中:

      ${product.name} 

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

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

 @WebServlet("/index.html") public class LatestProductServlet extends HttpServlet { //implementation... } 

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


额外

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

 -  - index.jsp <-- fake empty file to trick the application server - WEB-INF + index.jsp + web.xml 

当访问http://:// ,将如上所示工作。

感谢+ Braj ,我能够使用表达语言做出回答。 显然,这应该比jsp脚本/标签更好。

Servlet –

 import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ProductList extends HttpServlet { private static final long serialVersionUID = 1L; public ProductList() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List products = new ArrayList(); products.add("Car"); products.add("Gun"); products.add("Shades"); request.setAttribute("productsList", products); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } 

JSP –

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>     Insert title here     

List of products from servlet

${product}