-1

I am trying to refresh just the starting page (calling some functions) say "1.jsp" and as soon as the condition is fulfilled, it is redirected to another jsp page,say "2.jsp", but not sure why the 2.jsp is also getting refreshed. Not only that, the function which was called in 1.jsp is also getting called.Below is sample code just for understanding:

1.jsp

<body>
    <h1>Hello World!</h1>
    <% 
    // Here i am trying to read some txt file which is contantly being updated. (by refreshing the page)
    // when txt file is written completely, some character like "### DONE ###" will be present at its last line.
    // once "### DONE ###" is found , it will be redirected to "2.jsp"


    System.out.println("1");
    // if "###DONE###" found
    RequestDispatcher rd=request.getRequestDispatcher("2.jsp");
    rd.forward(request, response);            

    %>
</body>

2.jsp

<body>
    <h1>Hello World! PAGE 2</h1>
</body>

web.xml

<welcome-file-list>
    <welcome-file>1.jsp</welcome-file>
</welcome-file-list>

<filter>
    <filter-name>SampleFilter</filter-name>
    <filter-class>com.SampleFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SampleFilter</filter-name>
    <url-pattern>/2.jsp</url-pattern>
</filter-mapping>

** SampleFilter**

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
    public void setHeader(String name, String value) {
        System.out.println(name+"-------------------------------");
        if (!name.equalsIgnoreCase("Refresh")) {
            System.out.println("inside");
            super.setHeader("Refresh", "2");
        }
    }
});
}
Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104

1 Answers1

1

My guess is that when you perform rd.forward(request, response) you actually forward the response.addHeader("Refresh","2") with it, causing the next page to refresh automatically as well.

I'm not sure why exactly you need the refresh before the forward, but if you must have it - check if you can remove the specific header ("Refresh"), maybe using the solution proposed here: How do delete a HTTP response header?

EDIT:

Also, instead of triggering the refresh via server-side properties like the header, you should consider doing so via an HTML tag: <meta http-equiv="refresh" content="2" />

Community
  • 1
  • 1
Ofer Lando
  • 814
  • 7
  • 12
  • I have edited my code as per my understanding. Can you please guide me further. this thing is really imp for me. :( – Shashank Vivek Jan 29 '15 at 18:15
  • 1
    (1) Remove the response.setHeader part from 2.jsp. (2) Change the url-pattern to "catch" 2.jsp instead of 1.jsp (3) In SampleFilter.java, replace !name.equalsIgnoreCase("tt") with !name.equalsIgnoreCase("Refresh") ---> this should tell the server to trigger the filter when 2.jsp is accessed, and to prevent the "Refresh" attribute from being applied to this 2.jsp page. – Ofer Lando Jan 29 '15 at 18:59
  • Thanks for such quick reply. i have edited my code,once again. The code which you have suggested is not refreshing "1.jsp". I have explained the reason for refreshing 1.jsp in the abv code. Please have a look at it – Shashank Vivek Jan 30 '15 at 08:14
  • i tried creating another filter for "1.jsp" which does "super.setHeader("Refresh", "2");". but still "2.jsp" is getting refreshed – Shashank Vivek Jan 30 '15 at 08:24
  • I'll try to check later the filter/header issue, but did you consider using Good-Old-HTML-Refresh: ? – Ofer Lando Feb 01 '15 at 08:39
  • 1
    Thanx!! above HTMP-Tag worked for me. will add this into my code, if it works will select this as an answer :) – Shashank Vivek Feb 02 '15 at 08:24
  • Cool. I'll add it to the body of my answer, incase people google their way to this question :-) – Ofer Lando Feb 02 '15 at 08:36