在页面加载时从 JSP 文件调用 servlet [英] Calling a servlet from JSP file on page load

查看:74
本文介绍了在页面加载时从 JSP 文件调用 servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在不使用 HTML 表单的情况下从 JSP 文件调用 servlet 吗?

Can I call a servlet from JSP file without using a HTML form?

例如,在页面加载期间在 HTML 表格中显示数据库中的结果.

For example, to show results from database in a HTML table during page load.

推荐答案

您可以使用 doGet() servlet 的方法<强>预处理请求并将请求转发到 JSP.然后只需在链接和浏览器地址栏中指向 servlet URL 而不是 JSP URL.

You can use the doGet() method of the servlet to preprocess a request and forward the request to the JSP. Then just point the servlet URL instead of JSP URL in links and browser address bar.

例如

@WebServlet("/products")
public class ProductsServlet extends HttpServlet {

    @EJB
    private ProductService productService;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> products = productService.list();
        request.setAttribute("products", products);
        request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
    }

}

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
        </tr>
    </c:forEach>
</table>

注意JSP文件放在/WEB-INF文件夹内,防止用户不调用servlet直接访问.

Note that the JSP file is placed inside /WEB-INF folder to prevent users from accessing it directly without calling the servlet.

另请注意,@WebServlet 仅在 Servlet 3.0(Tomcat 7 等)后可用,另请参见 @WebServlet 与 Tomcat 7 的注解.如果您无法升级,或者由于某种原因需要使用与 Servlet 3.0 不兼容的 web.xml,那么您需要以老式的方式手动注册 servletweb.xml 如下所示,而不是使用注解:

Also note that @WebServlet is only available since Servlet 3.0 (Tomcat 7, etc), see also @WebServlet annotation with Tomcat 7. If you can't upgrade, or when you for some reason need to use a web.xml which is not compatible with Servlet 3.0, then you'd need to manually register the servlet the old fashioned way in web.xml as below instead of using the annotation:

<servlet>
    <servlet-name>productsServlet</servlet-name>
    <servlet-class>com.example.ProductsServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>productsServlet</servlet-name>
    <url-pattern>/products</url-pattern>
</servlet-mapping>

通过注解或 XML 正确注册 servlet 后,现在您可以通过 http://localhost:8080/context/products 打开它,其中 /context 是 webapp 的部署上下文路径和 /products 是 servlet 的 URL 模式.如果你碰巧有任何 HTML <form> 在里面,那么就让它像这样 POST 到当前的 URL <form method="post">并添加一个 doPost() 到同一个 servlet 以执行后处理作业.继续下面的链接以获得更具体的例子.

Once having properly registered the servlet by annotation or XML, now you can open it by http://localhost:8080/context/products where /context is the webapp's deployed context path and /products is the servlet's URL pattern. If you happen to have any HTML <form> inside it, then just let it POST to the current URL like so <form method="post"> and add a doPost() to the very same servlet to perform the postprocessing job. Continue the below links for more concrete examples on that.

这篇关于在页面加载时从 JSP 文件调用 servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆