我想latest_products在页面加载时调用一个servlet 。index.jsp此servlet在List中有记录。我想把这个List<products>传给index.jsp。但是我不想在url中显示servlet的名称。有什么办法可以做到这一点。
latest_products
index.jsp
List<products>
遵循的步骤:
jsp:include
样例代码:
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。
url-pattern
web.xml
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>
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>
点击URL:scheme://domain:port/latest_products.jsp将调用Servlet的doGet()方法。
scheme://domain:port/latest_products.jsp
doGet()