Question

I've inherited a website developed using Struts 1.2 that I need to make some updates to. For one of the updates, I need to set a session variable based on a value selected from a dynamically created (using struts) dropdown box when the user clicks a button. The way I'm attempting to accomplish this task is by using the 'onclick' property of my button to call a JavaScript function that utilizes AJAX to make an asynchronous call to a struts Action class.

I've successfully implemented this solution, however, I'm running into an issue that only occurs sporadically. For the most part, when I click my button the session variable seems to be set correctly. However, occasionally, the session variable is not being set/reset. The sporadic nature of this issue is what is throwing me off. It looks like the AJAX code block is being called and processed (returning a readyState = 4 and status = 200), however, the Java struts logic is NOT being entered. Code is below.

JavaScript AJAX function:

function createTaskOpp() {
var selectedOpp = document.getElementById("oppId");
var url="setTaskSessionOpportunity.do?opp=" + selectedOpp.value;
var ajaxRequest;

try
{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
    // Internet Explorer Browsers
    try
    {
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e)
        {
            // Something went wrong while trying to create our HTTP object. Resubmit the current page.
            alert("Browser Error. Error creating Request Object: " + e);
            window.location ="taskConfiguration.do";
        }
    }
}
ajaxRequest.open("GET", url, true);
ajaxRequest.onreadystatechange = function() {

if (ajaxRequest.readyState == 4)
{
    if (ajaxRequest.status == 200) 
    {
        window.location ="saveTask.do";
    }

    else  {
        window.location ="taskConfiguration.do";
    }
}
};
ajaxRequest.send(null); }

Struts Action:

public class SetTaskSessionOpportunityAction extends Action {
public ActionForward execute( ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
{
    System.out.println("*****************");
    System.out.println("In SetTaskOpp AJAX -Beginning");
    HttpSession session = request.getSession();
    String taskOpp = request.getParameter("opp");
    session.setAttribute("task_opportunity", taskOpp);
    ActionForward actionForward = new ActionForward("/viewTask.jsp");
    return actionForward;
}

}

Thanks for the help! -Josh

Was it helpful?

Solution

Problem solved. It turns out it was a browser caching issue with AJAX request. IE (my test environment) automatically caches AJAX request. I corrected the issue by adding the following line of code:

response.setHeader("Cache-Control", "no-cache"); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top