jay
jay

Reputation: 65

dynamic content at the load time on jsp page using servlet

this is my question:i want to get dynamic information on the jsp page when i redirect on it but not using scriptlet ,using servlet so ultimately i want to call servlet when my jsp page is loaded so without any form action i have to do this

thanks in advance.

Upvotes: 1

Views: 2672

Answers (2)

BalusC
BalusC

Reputation: 1108642

Just call the servlet instead and let servlet forward the request to the JSP after preprocssing.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Do your preprocessing job here. E.g. retrieving list of products to display in JSP.
    List<Product> products = productDAO.list();
    request.setAttribute("products", products); // It'll be available as ${products} in JSP.

    // Finally forward request to JSP.
    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
}

Map this servlet on an url-pattern of for example /page and call it by http://example.com/context/page. Placing the JSP in /WEB-INF folder effectively hides it from direct access so that the ensuder cannot open it without a servlet.

See also

Upvotes: 2

djna
djna

Reputation: 55897

You need to think about where the code is running at different times. Your JSP is a servlet, it runs on the server and sends HTML to browser. Once the browser has rendered the HTML page your JSP is no longer running, you've just got HTML in the browser.

That page may have links, or Form submit buttons which will invoke a new resource such as a servlet or JSP on the server and get a whole new page back to the browser. I don't fully understand your question, I think you don't want to POST a form, but you could just allow the user to click on a link.

However, I suspect that what you want to do is update part of the page without redrawing the whole thing. This is sometimes called AJAX programming. Some JavaScript in your HTML page runs, makes calls to the server and then modifies the HTML, hence displaying new data.

AJAX is a big topic, needs JavaScript skills and greatly benefits from a framework such as Dojo or JQuery. I suggest that you google your way around these subjects a bit and then come back with specific questions.

Upvotes: 0

Related Questions