Home > Java > javaTutorial > Can I Call a Servlet from a JSP on Page Load Without Using an HTML Form?

Can I Call a Servlet from a JSP on Page Load Without Using an HTML Form?

Barbara Streisand
Release: 2024-12-09 11:54:11
Original
695 people have browsed it

Can I Call a Servlet from a JSP on Page Load Without Using an HTML Form?

Calling a Servlet from JSP File on Page Load

Question:

Can a servlet be invoked from a JSP file without employing an HTML form?

Answer:

Absolutely. To accomplish this, utilize the servlet's doGet() method to preprocess the request and subsequently forward it to the JSP. This can be achieved without specifying the JSP URL in the browser's address bar or links. Instead, point the servlet URL.

Example:

Consider the following code snippet:

Servlet (ProductsServlet.java):

@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);
    }

}
Copy after login

JSP (products.jsp):

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

...

<table border="1">
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
        </tr>
    </c:forEach>
</table>
Copy after login

In this example, the doGet() method of the ProductsServlet retrieves a list of products and sets it as a request attribute. The request is then forwarded to the products.jsp page, which iterates over the list and displays the products in a table.

Note:

Ensure that the JSP file is placed within the /WEB-INF folder to prevent unauthorized direct access. Servlet 3.0 (or later) supports the @WebServlet annotation for servlet registration; however, if you are unable to upgrade or need to use web.xml for compatibility reasons, register the servlet manually in web.xml.

Additional Resources:

  • [Servlets Wiki Page](https://wiki.java.net/HowToWiki/Servlets)
  • [doGet and doPost in Servlets](https://docs.oracle.com/javaee/5/tutorial/servlets/basicservlets004.html)
  • [How to Avoid Java Code in JSP](https://stackoverflow.com/a/9701987/6776571)
  • [Servlet Returns "HTTP Status 404 The Requested Resource (/servlet) Is Not Available"](https://stackoverflow.com/a/4288938/6776571)

The above is the detailed content of Can I Call a Servlet from a JSP on Page Load Without Using an HTML Form?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template