我想在加载latest_products
页面时调用servlet index.jsp
。此servlet在List中有记录。我想将此List<products>
传递给index.jsp
。但我不想在url中显示servlet的名称。有什么方法可以做到这一点。
答案 0 :(得分:11)
要遵循的步骤:
jsp:include
从JSP调用Servlet,该JSP将在运行时包含Servlet在JSP中的响应示例代码:
JSP:
<body>
<jsp:include page="/latest_products.jsp" />
<c:out value="${message }"></c:out>
</body>
的Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("message", "hello");
}
但我不想在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>
要遵循的步骤:
示例代码:
的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:
<body>
<c:out value="${message }"></c:out>
</body>
点击将调用Servlet的scheme://domain:port/latest_products.jsp
方法的网址:doGet()
。
答案 1 :(得分:6)
(...)但我不想在网址中显示servlet的名称。
访问Servlet时,根本不需要使用Servlet名称。实际上,您可以通过定义正确的URL模式来创建指向所需URL的servlet:
@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);
}
}
然后,您将拥有这样的文件夹结构
- <root folder>
- WEB-INF
+ index.jsp
+ web.xml
在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>
请注意,您的客户将访问http://<yourWebServer>:<port>/<yourApplication>/index.jsp
并获得所需内容。而且您不需要触发两个GET请求来检索特定页面的内容。
请注意,您可以采用这种方法:由于现在在servlet中定义了模式,您可以选择不为您的客户端使用index.jsp,而是使用index.html来访问您的页面。只需更改Servlet中的URL模式:
@WebServlet("/index.html")
public class LatestProductServlet extends HttpServlet {
//implementation...
}
客户端将访问http://<yourWebServer>:<port>/<yourApplication>/index.html
,这将在WEB-INF / index.jsp中显示结果。现在,您的客户和您都会很高兴:客户将获得他们的数据,而您不会在您的URL中显示丑陋的servlet名称。
其他强>
如果您将index.jsp作为web.xml中的欢迎页面,则此方法无法正常工作,因为应用程序servlet需要存在真正的文件index.jsp。要解决此问题,只需创建一个名为index.jsp的空文件:
- <root folder>
- index.jsp <-- fake empty file to trick the application server
- WEB-INF
+ index.jsp
+ web.xml
访问http://<yourWebServer>:<port>/<yourApplication>/
时,如上图所示。
答案 2 :(得分:0)
感谢 + 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<String> products = new ArrayList<String>();
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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:import url="/ProductList" />
<c:set var="myProducts" value="${requestScope.productsList}" />
<h1>List of products from servlet</h1>
<c:forEach var="product" items= "${myProducts}" varStatus="i">
${product}<br>
</c:forEach>
</body>
</html>