By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Problem in maintaining the selected value of dropdown list

P: 41
Hi,

i have one jsp in which i select a value from drop down and on change of this a servlet is called that accesses some values and return to the same jsp where these values get populated in another dropdown list . Now the problem is when the jsp loads again (after fetching the values in servlet) the selected value of dropdown is lost. How can i make it happen that when the page reloads thedrop down list show me the previous selected value and below that another values.

Thanks
Oct 29 '07 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
Hi,

i have one jsp in which i select a value from drop down and on change of this a servlet is called that accesses some values and return to the same jsp where these values get populated in another dropdown list . Now the problem is when the jsp loads again (after fetching the values in servlet) the selected value of dropdown is lost. How can i make it happen that when the page reloads thedrop down list show me the previous selected value and below that another values.

Thanks
You pass the previosly selected value back to the JSP and use a simple if condition

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if(value.equals("previousValue")) {
  3. %> 
  4. <OPTION value ="<%=value%>" selected><%=value%>  
Oct 29 '07 #2

P: 41
You pass the previosly selected value back to the JSP and use a simple if condition

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if(value.equals("previousValue")) {
  3. %> 
  4. <OPTION value ="<%=value%>" selected><%=value%>  
Hi,

Thanks for ur reply.

But in this case i will get duplicate items in same dropdown list i.e. One due to if condition and other due to static options.
Oct 29 '07 #3

10K+
P: 13,264
Hi,

Thanks for ur reply.

But in this case i will get duplicate items in same dropdown list i.e. One due to if condition and other due to static options.
You should use a loop to populate the other "static" options. Put them in an array and populate them from the array.
Oct 29 '07 #4

P: 41
Hi,

Thanks for your valuable suggestion. I think this will get solved now.

I want to ask one more thing.
Is it possible that we use
Expand|Select|Wrap|Line Numbers
  1. HttpSession session=request.getSession(true)
  2. session.setAttribute("str",str);
and
Expand|Select|Wrap|Line Numbers
  1. request.setAttribute("str1",str1);
and then use RequestDispatcher and forward to pass the request in same servlet.

Thnax
Oct 29 '07 #5

10K+
P: 13,264
Hi,

Thanks for your valuable suggestion. I think this will get solved now.

I want to ask one more thing.
Is it possible that we use
Expand|Select|Wrap|Line Numbers
  1. HttpSession session=request.getSession(true)
  2. session.setAttribute("str",str);
and
Expand|Select|Wrap|Line Numbers
  1. request.setAttribute("str1",str1);
and then use RequestDispatcher and forward to pass the request in same servlet.

Thnax
Should work but normally you want to use one and not both methods.
Use the session to store attributes that belong to a session and use request.setAttribute when you want to pass a value from a servlet to JSP. You'd then use requestDispatcher with include(request, response)
Oct 29 '07 #6

P: 2
session["value"]!=null
{
//bind ur code
}
Jul 15 '15 #7

Post your reply

Sign in to post your reply or Sign up for a free account.