Eric Tseng
Eric Tseng

Reputation: 563

JSP refresh page through servlet

I'd like to using servlet code to refresh jsp page.

The "my_list.jsp" is a page with a list of items The "my_add.jsp" is a page that user can input something to add items to the list in "my_list.jsp"

The following figure is the current design.

enter image description here

User click "add items" in "my_list.jsp" and there will be a pop-up "my_add.jsp". After user finishes inputing and click "Add button" to add the item to the list, it will trigger "AddServlet.java" to add the item.

AddServlet.java

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {

    AddItem();
    resp.sendRedirect("/my_add.jsp");
    resp.setHeader("REFRESH", "0");
}

Because the user may add more items, so I don't want "my_add.jsp" page close after adding an item. So I write the following in "AddServlet.java"

resp.sendRedirect("/my_add.jsp");
resp.setHeader("REFRESH", "0");

But now, after user clicks "Add button" in "my_add.jsp" page, "my_list.jsp" will not update. "my_list.jsp" will update only if I click "Close button" in "my_add.jsp" or press "Refresh" in "my_list.jsp".

I would like to know if there's any way to refresh "my_list.jsp" page after clicking "Add button" in "my_add.jsp" ?

Thanks in advance.

Eric

Upvotes: 1

Views: 5362

Answers (1)

Santhosh
Santhosh

Reputation: 8187

Instead of having two pages such as "my_list.jsp" and "my_add.jsp" . use a jquery dialog from my_list.jsp . So it will trigger the dialog as you have shown in the my_add.jsp.

Now just make an ajax call from the my_list.jsp after the jquery confirmation.So that you can print the success value in the my_list.jsp without refreshing it .

Hope this helps !!

Upvotes: 0

Related Questions