Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:02:44+00:00 2026-05-16T12:02:44+00:00

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

  • 0

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:02:44+00:00Added an answer on May 16, 2026 at 12:02 pm

    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.

    E.g.

    @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>
    

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

    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>
    

    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.

    See also

    • Our Servlets wiki page
    • doGet and doPost in Servlets
    • How to avoid Java code in JSP
    • Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I call a servlet from the form's action without using the web.xml
How can I call a servlet from jsp ? But in this case, I
Using a delegate I can call any function asynchronously. From the documentation I understand
How should I access the ServletContext from a .jsp? For example, how can I
Can you call a servlet with a link? For example <a href=/servletName>link text</a> And
Can we call init() method from service() method in servlet ? I got this
With CreateProcessAsUser I can call an .exe file somewhere on the hard disk: CreateProcessAsUser(IntPtr
From this code I can call bmwCars.CopyToDataTable() as I expected. var bmwCars = from
(Yes I know I can call Java code from Scala; but that is pointless;
I want to call a Servlet which exists in a different war from my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.