Meaningless Drivel is fun!*
The moose likes Struts and the fly likes need caching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "need caching" Watch "need caching" New topic
Author

need caching

Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
Hi,
I am using struts 1.1

In my jsp page i am populating states according to

country selected.But problem is that after populating states in

select, as the page refreshes the data entered before selecting

the country vanishes but i need to keep the data there.I am not

using any of the scripts
--------------------code-------------

response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 3);

---------------------------------------
Any code is there to maintain cache explicitly in JSP.
Please give any solution
It is caching when i using simple html page on refreshing...
i am using tomcat 5.0.19 server
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
please ......... anybody....??? it's urgent.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you post some code from your JSP to give us an idea of how your drop-down is related to the rest of the form?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
By the way, I don't believe your problem has anything to do with caching so I wouldn't spend too much time on thinking about the HTTP headers your webapp is sending to the client...
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
yes Koskela ,


If you have time go through this code and please give me your valuable suggestions..
if the code is confusing sorry.....
on change function of select tag i am calling one method redirect(x).
here x is selected index of country.

function redirect(x){

document.forms[0].action="index.do?countryId=" + x;
document.forms[0].submit();

}


Long country = new Long(0);
String cCountry = request.getParameter("countryId");
if ((cCountry == null) || ("".equals(cCountry))) {

cCountry = "0";

}
else {
country = new Long(cCountry);

}
%>


index.do gives the same page that is on which page we are selecting country
but populated states according to that...





this is the code for state select...
<html:select property="stateId">
<state:statelist name="states" countryId="<%=country%>"/>
</html:select>




I created one tag library
like this..
-------------------------------------code----------------------------------


public int doStartTag() {
availOptions = new StringBuffer();

/** Queries to be executed */
String queryState =
"SELECT STATE_ID, STATE_NAME FROM STATE WHERE COUNTRY_ID = ?";

try {
/** Open the connection, perform the query and store the results */
Class.forName(driver);
conn = DriverManager.getConnection(hostName, user, password);
conn.setAutoCommit(false);

if (conn != null) {
/** Get the list of states */
prepStmt = conn.prepareStatement(queryState);
prepStmt.setLong(1, this.countryId.longValue());
rsState = prepStmt.executeQuery();

availOptions.append(
"<option value=\"\"<span style=\"font-size: 10.0pt; font-family: Helvetica; color: black\">");
availOptions.append("Select State </span></option>\r\n");
/** Populate with the values of states */
while (rsState.next()) {
String cState = rsState.getString("STATE_NAME");
String cStateId = rsState.getString("STATE_ID");
addOption(availOptions, cState, cStateId,cStateId.equalsIgnoreCase(this.getStateId()));
}

ResponseUtils.write(pageContext, availOptions.toString());

return 0;
}

return SKIP_BODY;
}
catch (Exception ex) {
/** If the database can't be opened, skip everything */
return SKIP_BODY;
}
}


protected void addOption(StringBuffer availOptions, String label, String value, boolean matched) {
availOptions.append("<option value=\"" + value + "\"");

if (matched) {
availOptions.append(" selected=\"selected\"");
}
availOptions.append(
"<span style=\"font-size: 10.0pt; font-family: Helvetica; color: black\">");
availOptions.append(label);
availOptions.append("</span></option>\r\n");
}
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you edit your post to include the CODE UBB tags? I have the feeling that your post was supposed to include more than what I can see.

Anyway, could you tell what request.getParameter("countryId"); returns when the JSP is executed after selecting a country?

Also, wouldn't it be easier to rely on <html:select ... value="<%= countryId %>"> for retaining the selection (instead of using your own custom taglib)?
[ August 16, 2004: Message edited by: Lasse Koskela ]
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
thank you for your consideration.....


function redirect(x){

document.forms[0].action="index.do?countryId=" + x;
document.forms[0].submit();

}
since the above code work only when user selected any contry.
so first time when page loads
String cCountry = request.getParameter("countryId");//returns the country
will be null so it is setting to zero.
when selecting the country option it will call thwe redirect(option.value)
and passes that value as queryString.

countryand passses to the tag
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Oh, so the vanishing data is something else than the country selection?

In that case, I guess you need to submit the whole form instead of just the countryId request parameter. I've never done that but maybe someone else has and would be able to help out.
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
yes
Koskela, second time i need to submit the whole data after filling.
like hotmail sign for new account...

https://registernet.passport.net/reg.srf?id=2&lc=16393&sl=1
Thank you....
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
please any body give me the solution...
poornima balagopal
Ranch Hand

Joined: Dec 02, 2003
Posts: 83
Hi manoj,
normally if the the fields in the jsp have get and set method in your actionform class(form bean ). When you enter data into these fields and submit the page , the formbean object will hold the data which you can retrieve in your action class. I think in your case when you reload the page after selecting the country to show the correponding states, the data in the form bean object is getting lost.
I dont know the exact reason.
So try like this. When you submit your page for selection of states according to the country selected, get the form bean object from the form bean parameter of the actionForward method of the action class and again set to the request.
You have to do some changes in your code
in your jsp suppose you have a field called address
So you have to give as
<html:text property="address" name="formObj" />
In your action class in the method which you call while submitting the page for the selection of states
you have to write as
yourFormBean classname formBeanObj = (form)formBeanclassname();
request.setAttribute("formObj",formBeanObj );
When you do like this , if the formbean holds any value , it will be displayed in the jsp according the way you have given(check the above text box i have given for displaying address).
This is the way i usually do. I dont think you have to use your own tag library to solve this problem.This is very simple
Try like this ok
all the best
Poornima
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
thank you poornima,
I got the solution only i added scope to request in jsp form tag all other done by the form class.bc,z in action class i am stting it.
I am sorry to post to the soln when i got it.
 
 
subject: need caching
 
Similar Threads
Problem in opening multiple browser
Clearing of JSP Page and form data from cache
Setting response headers conditionally
Tiles and page caching
remove the data from cache