Java 如何在jsp页面加载时调用servlet
我想在加载Java 如何在jsp页面加载时调用servlet,java,jsp,jakarta-ee,servlets,Java,Jsp,Jakarta Ee,Servlets,我想在加载index.jsp页面时调用一个servletlatest_products。这个servlet在列表中有记录。我想把这个列表传递到index.jsp。但我不想在url中显示servlet的名称。有什么方法可以做到这一点。解决方案1 应采取的步骤: 使用jsp:include从jsp调用Servlet,该Servlet将在运行时在jsp中包含Servlet的响应 在Servlet中的请求中设置属性,然后在JSP中读取它 示例代码: JSP: 编辑 但我不想在url中显示servle
index.jsp
页面时调用一个servletlatest_products
。这个servlet在列表中有记录。我想把这个列表
传递到index.jsp
。但我不想在url中显示servlet的名称。有什么方法可以做到这一点。解决方案1
应采取的步骤:
- 使用
从jsp调用Servlet,该Servlet将在运行时在jsp中包含Servlet的响应jsp:include
- 在Servlet中的请求中设置属性,然后在JSP中读取它
web.xml
中为Servlet定义一个不同且有意义的url模式
,如下所示,它看起来像一个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>
index.jsp:
<body>
<c:out value="${message }"></c:out>
</body>
- <root folder>
- index.jsp <-- fake empty file to trick the application server
- WEB-INF
+ index.jsp
+ web.xml
点击网址:
scheme://domain:port/latest_products.jsp
将调用Servlet的doGet()
方法。多亏了+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 {
}
}
<%@ 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>
import java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
公共类ProductList扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
公共产品列表(){
超级();
//TODO自动生成的构造函数存根
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
列表产品=新的ArrayList();
产品。添加(“汽车”);
产品。添加(“枪”);
产品。添加(“阴影”);
setAttribute(“productsList”,products);
}
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
}
}
JSP-
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 {
}
}
<%@ 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>
在此处插入标题
servlet中的产品列表
${product}
(…)但我不想在url中显示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);
}
}
客户端将访问http://:///index.html
,它将在WEB-INF/index.jsp中显示结果。现在,您的客户机和您都会很高兴:客户机将获得他们的数据,而您不会在URL中显示难看的servlet名称
附加的 如果您在web.xml中使用index.jsp作为欢迎页面,那么这种方法将不起作用,因为应用程序servlet希望存在一个真正的文件index.jsp。要解决此问题,只需创建一个名为index.jsp的空文件:
<body>
<c:out value="${message }"></c:out>
</body>
- <root folder>
- index.jsp <-- fake empty file to trick the application server
- WEB-INF
+ index.jsp
+ web.xml
-
-index.jsp您可以使用Ajax实现此目的。@developerwjk这不是一个好主意,它将向服务器发出两个请求:1)获取页面请求,2)获取servlet请求。您的问题的解决方案在这里介绍:,Hello World#2(预处理请求)
和
之间有什么区别?@Braj-我看不出有什么区别。但是,在一个论坛上,他们说最好使用EL而不是旧的jsp标记和脚本。我不知道为什么。因为JSTL易于使用,而且不容易出错。有时很难在代码中找到大括号的开头和结尾。阅读更多关于@Braj的内容这不是一场竞赛,看谁对Java web开发了解得更多。因为问题的发布方式看起来像是你为什么要这样做?在等待你的答案,以证明你是错的,并张贴一个精彩的评论揭示答案。如果你有/需要告诉一些东西来帮助展示当前的知识,那么就说出来,不要和这样的人玩。我演示了如何在URL中不使用servlet名称。@Luiggmendoza现在我明白了你的意思,谢谢。@Luiggmendoza顺便问一下,使用serlvet名称的问题在哪里。我们可以将它定义为/latest_products.jsp
,它看起来像是在点击一个jsp页面。是的,我希望从您的编辑中看到这一点。但你没有。取而代之的是,您继续使用2GET请求方法,我仍然认为这是一种糟糕的方法。@LuiggiMendoza我在上一次声明中已经建议过它。我从来没有这样想过+谢谢你的回答。我喜欢。
- <root folder>
- index.jsp <-- fake empty file to trick the application server
- WEB-INF
+ index.jsp
+ web.xml