0

I have a JSF application where you enter 2 numbers (index.xhtml), and when you click "Add" button those numbers are added and the result is shown in a new page (resultado.xhtml).

What I want to do is that when you insert the 2 numbers, click "Add" button and go to resultado.xhtml, if you go back to index.xhtml, clicking going back button in you browser, the inputText fields must be empty and not containing the previously inserted values. The problem is that it works with Firefox, but not with Chromium and Konqueror browsers. When I use this browser and press going back button the numbers inserted are in the inputText. How could make it work with all the browsers?

I have followed this link to implement a servlet filter to clear cache: Prevent user from seeing previously visited secured page after logout

index.xhtml

    <h:head>
        <h:outputStylesheet library="css" name="styles.css"  />
    </h:head>
    <h:body>
        <h:form>   

            <!--Number 1 -->
            <h:inputText id="num1" label="num1" required="true" size="5" maxlength="5" 
                    styleClass="#{component.valid ? '' : 'validation-failed'}"
                    value="#{sumaManagedBean.number1}"
                    requiredMessage="You must enter a value"/>
            </h:inputText>
            <h:message for="num1" />


            <!--Number 2-->
            <h:inputText id="num2" label="num2" required="true" size="5" maxlength="5"
                     styleClass="#{component.valid ? '' : 'validation-failed'}"
                     value="#{sumaManagedBean.number2}"
                     requiredMessage="You must enter a value">         
            </h:inputText>
            <h:message for="num2" />

                <h:commandButton value="Add" action="#{sumaManagedBean.direccionPagina()}"/>
        </h:form>


</h:body>  

resultado.xhtml

<h:head>
    <h:outputStylesheet library="css" name="styles.css"  />
</h:head>
<h:body>

<h:outputText value="Add correct" rendered="#{sumaManagedBean.insertCorrecto}" styleClass="message"/> 
    Result:
    <h:outputText value="#{sumaManagedBean.calcularResultado()}" /> 
</h:body>

sumaManagedBean.java. My ManagedBean scope is RequestScope.

package controllers;

//Imports

    @ManagedBean
    @RequestScoped
    public class SumaManagedBean implements Serializable
    {

        boolean m_binsertCorrecto; 

        int number1;
        int number2;

        public SumaManagedBean() {      

        }


        //Getters and Setters
        public int getNumber1() {
            return number1;
        }

        public void setNumber1(int number1) {
            this.number1 = number1;
        }

        public int getNumber2() {
            return number2;
        }

        public void setNumber2(int number2) {
            this.number2 = number2;
        }


        //
        public boolean isInsertCorrecto() 
        {
            return m_binsertCorrecto;
        }

        public void setInsertCorrecto(boolean bInsertCorrecto) 
        {
            this.m_binsertCorrecto = bInsertCorrecto;
        } 



        public int calcularResultado()
        {
            int resultado;

            resultado = number1 + number2;

            return resultado;
        }

        public String direccionPagina()
        {
            String direccion = "/resultado";

            setInsertCorrecto(true);

            return direccion;
        }

    }

NoCacheFilter.java

package filters;

import java.io.IOException;
import javax.faces.application.ResourceHandler;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebFilter("/*") //Apply a filter for all URLs
public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        if (!request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            response.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void destroy() {
    }
}
Community
  • 1
  • 1
jose
  • 303
  • 4
  • 17
  • Are you really implying that the webbrowser loads the page from its cache instead of firing a HTTP request? Have you looked at HTTP traffic monitor or are you merely guessing/assuming? I'd imagine that this is "just" again another case of turning off autocomplete. – BalusC Jun 10 '15 at 10:49
  • Thank you for answering, now I have turned on autocomplete and it works well in Firefox and Chromium, but not in Konqueror (when going back, the previously inserted values are still in the inputText). Do you know why it could be? – jose Jun 10 '15 at 13:06
  • your turned ON autocomplete? I think that @BalusC meant to turn it off (if he did not mean that I'm going to eat my hat) – Kukeltje Jun 10 '15 at 13:55
  • Oh, yes, you are right. I turned off. I think that Konqueror does not support autocomplete="off" (maybe I'm wrong) and neither has a Developer-Network option to see if the pages are gotten form the server or the cache. – jose Jun 10 '15 at 14:44
  • I don't think the browser cache also caches the values you typed before. It's just a feature of the browser to keep track of your input history when navigating back which has nothing to do with caching. I don't believe there's a real way to stop browsers from doing this. – ChristopherS Jun 10 '15 at 14:47
  • Konqueror? Ugh. That's not exactly a "decent" browser. It's like IE but then in Linux world. Well, not much to do against it from server side other than conditionally displaying some warning bar in top with a link to browsehappy.com. Is this Q acceptable as dupe? http://stackoverflow.com/questions/18506517/old-values-in-input-fields-by-get-request/18510930#18510930 – BalusC Jun 10 '15 at 19:28
  • Oh, I didn't know it was so bad, last time I used it wasn't bad, but that was years ago. The truth is that I have used today and yes, it is not good. – jose Jun 10 '15 at 20:41

0 Answers0