Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call servlet on jsp page load [duplicate]

I want to call a servlet latest_products on load of index.jsp page.This servlet has records in List. I want to pass this List<products> to index.jsp. But I don't want to display the name of servlet in url. Is there any way by which I can do this.

like image 621
user3660263 Avatar asked Jun 05 '14 17:06

user3660263


2 Answers

Solution 1

Steps to follow:

  • use jsp:include to call the Servlet from the JSP that will include the response of the Servlet in the JSP at runtime
  • set the attribute in the request in Servlet and then simply read it in JSP

Sample code:

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

EDIT

but i don't want to display the name of servlet in url.

Simply define a different and meaningful url-pattern for the Servlet in the web.xml such as as shown below that look like a JSP page but internally it's a 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>

Solution 2

Steps to follow:

  • first call to the the Servlet
  • process the latest products
  • set the list in the request attribute
  • forward the request to the JSP where it can be accessed easily in JSP using JSTL

Sample code:

Servlet:

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>

hit the URL: scheme://domain:port/latest_products.jsp that will call the Servlet's doGet() method.

like image 103
Braj Avatar answered Sep 18 '22 11:09

Braj


(...) but I don't want to display the name of servlet in url.

You don't need to use the Servlet name at all when accessing to a Servlet. In fact, you can create your servlet to point to the desired URL by defining the right URL pattern:

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

Then, you will have a folder structure like this

- <root folder>
  - WEB-INF
    + index.jsp
    + web.xml

And in WEB-INF/index.jsp:

<!DOCTYPE html>
<html lang="es">
    <body>
        <!--
            Display the data accordingly.
            Basic quick start example
        -->
        <c:forEach items="${productList}" var="product">
            ${product.name} <br />
        </c:forEach>
    </body>
</html>

Note that your clients will access to http://<yourWebServer>:<port>/<yourApplication>/index.jsp and will get the desired content. And you don't need to fire two GET requests to retrieve the content for your specific page.

Note that you can go further with this approach: Since the pattern is now defined in servlet, you may choose to not use index.jsp for your clients, but index.html to access to your page. Just change the URL pattern in the Servlet:

@WebServlet("/index.html")
public class LatestProductServlet extends HttpServlet {
     //implementation...
}

And the clients will access to http://<yourWebServer>:<port>/<yourApplication>/index.html, which will show the results in WEB-INF/index.jsp. Now both your clients and you will be happy: clients will get their data and you won't show that ugly servlet name in your URL.


Additional

If you have index.jsp as your welcome page in web.xml, this approach won't work because application servlet expects that a real file index.jsp exists. To solve this issue, just create an empty file named index.jsp:

- <root folder>
  - index.jsp  <-- fake empty file to trick the application server
  - WEB-INF
    + index.jsp
    + web.xml

And when accessing to http://<yourWebServer>:<port>/<yourApplication>/, will work as shown above.

like image 32
Luiggi Mendoza Avatar answered Sep 18 '22 11:09

Luiggi Mendoza