1

I am setting data in session attribute after fetching from DB once user is logged in. But when user reloads the page, I want the session attribute to be updated with the latest data from DB so that updated data is displayed on the screen using

<c:forEach items= "${sessionScope.allPosts}" var="post">
... 
</c:forEach>.

It is basically just 2 lines of code in Servlet where PostsDao.getPosts() will fetch the latest data:

List<Post> allPostsList = PostsDao.getPosts("ALL", userId);
session.setAttribute("allPosts", allPostsList);

How do I make this call every time when user click on page refresh?

k92
  • 375
  • 3
  • 15
  • i was just going to post answer already typed but this question got closed :( – Swati Jun 15 '20 at 12:16
  • you can post in the comment! :-) – k92 Jun 15 '20 at 12:18
  • You can write a `function` when page gets reloaded everytime you can call this function which will redirect you to the `servlet` where your code has written and again use `request.getRequestDispatcher("Yourjsppage")..` to come back to same jsp page . – Swati Jun 15 '20 at 12:20
  • okay. So if I am using `getRequestDispatcher`, then will this process without going to the other page? Like I don't want to be redirected and the come back. – k92 Jun 15 '20 at 12:22
  • use `window.location = "YourServletUrl";//redirect to servlet` and then come back to same page using `request.getRequestDispatcher("Yourjsppage")..` your code should be in `doGet()` method of servlet.Also the link provided as duplicate is also helpful refer that as well – Swati Jun 15 '20 at 12:24
  • Okay will try this. Thanks a lot @Swati!! – k92 Jun 15 '20 at 12:26

0 Answers0