?
Solved

Retain the value of Drop down box in jsp

Posted on 2011-09-26
6
Medium Priority
?
4,065 Views
Last Modified: 2012-08-13
Hi,

I have a JSP with several drop down boxes.  When the user clicks on submit button the page is submitted and again redirected to the same page with some results added in the bottom of the page.  In this process, the values which are selected in the drop down box are also refreshed (expected).  Now i want to retain the selected value in the drop down box, which am not finding a way to do it through jsp or javascript.  Here is my code snippet:

<form action="tremorEntry.do" method="post" id="teForm">
      <div id="dataEntryPoint" style=" background-color: activeborder; table-layout: auto; margin: 2px; margin-top:10px; margin-left:10px; width:1200px; height:80px; border: thin; border-color: black; border-style: dotted; ">
            <table>
                  <tr>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="startDateLbl" class="setFont" >Start Date/Time : </label>
                        </td>
                        <td>
                              <input id="startDate" class="setFont" name="startDate" title="Start Date/Time" type="text" value="<% out.write(request.getParameter("startDate")); %>"/>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="endDateLbl" class="setFont" >End Date/Time : </label>
                        </td>
                        
                        <td>
                              <input id="endDate" name="endDate" class="setFont" title="End Date/Time" type="text" value="<% out.write(request.getParameter("endDate")); %>"/>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="view" class="setFont" >View : </label>
                        </td>
                        <td>
                              <select id="view" name="view" class="setFont" style="width: auto;" tabindex="0">
                                    <option selected="selected">Choose One</option>
                                    <option>Vertical</option>
                                    <option>Component</option>
                                    <option>Widget</option>
                                    <option>User Guid</option>
                                    <option>Session Id</option>
                                    <option>Root Guid</option>
                              </select>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="applicationLbl" class="setFont" >Application : </label>
                        </td>
                        <td>
                              <select id="application" name="application" class="setFont" style="width: auto;" tabindex="0">
                                    <option>Choose One</option>
                                    <option>Build Reports</option>
                                    <option>Clear App</option>
                                    <option selected="selected">Security</option>
                                    <option>Document</option>
                                    <option>Clear EDD</option>
                                    <option>Unknown</option>
                              </select>
                        </td>
                  </tr>      
                  <tr style="height: 20px;">
                  </tr>
                  <tr>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="timeRangeLbl" class="setFont" >Time Range: </label>
                        </td>
                        <td>
                              <select id="timeRangeSel" name="timeRangeSel" class="setFont" style="width: auto;" tabindex="0">
                                    <option selected="selected">15 Minute</option>
                                    <option>Minute</option>
                                    <option>Day</option>
                                    <option>Month</option>
                              </select>
                        </td>                        
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="middleTierLbl" class="setFont" >Middle Tier : </label>
                        </td>
                        <td>
                              <select id="middleTierSel" name="middleTierSel" class="setFont" style="width: auto;" tabindex="0">
                                    <option selected="selected">Client</option>
                                    <option>Production</option>
                              </select>
                        </td>
                        <td style="width: 80px;">
                        </td>
                        <td>
                              <span id="submitSpan">
                                          <input id="submitBtn" value="Submit" type="submit"/>
                                          <input id="hiddenBtn" type="hidden"/>
                              </span>
                        </td>
                  </tr>
            </table>
      </div>
</form>

Thanks,
Murali.
0
Comment
Question by:lakkarajumurali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36599675
try with the below code..
 
<select id="view" name="view" class="setFont" style="width: auto;" tabindex="0">
  <option value="" selected="selected">Choose One</option>
  <option value="Vertical" <% if(request.getParameter("view").equals("Vertical") { out.println("selected='selected'); }  %>>Vertical</option>
  <option value="Component" <% if(request.getParameter("view").equals("Component") { out.println("selected='selected'); }  %>>Component</option>
  <option value="Widget" <% if(request.getParameter("view").equals("Widget") { out.println("selected='selected'); }  %>>Widget</option>
  <option value="User Guid" <% if(request.getParameter("view").equals("User Guid") { out.println("selected='selected'); }  %>>User Guid</option>
  <option value="Session Id" <% if(request.getParameter("view").equals("Session Id") { out.println("selected='selected'); }  %>>Session Id</option>
  <option value="Root Guid" <% if(request.getParameter("view").equals("Root Guid") { out.println("selected='selected'); }  %>>Root Guid</option>
</select>

Open in new window


BR-Sudhindra-www.clicksperday.com
0
 

Author Comment

by:lakkarajumurali
ID: 36599747
it says selected cannot be resolved.  here is how it is now.

<option value="<%if(request.getParameter("view").equals("Vertical")) {out.println(selected="selected"); } %>">Vertical</option>
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36599809
sorry change it to...

<option value="<%if(request.getParameter("view").equals("Vertical")) {out.println('selected="selected"'); } %>">Vertical</option>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lakkarajumurali
ID: 36600083
am getting the following exception:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/VtqForm.jsp at line 217

214:                         <td>
215:                               <select id="view" name="view" class="setFont" style="width: auto;" >
216:                                     <option value="" selected="selected">Choose One</option>
217:                                     <option value="<%if(request.getParameter("view").equals("Vertical")) {out.println("selected='selected'"); } %>">Vertical</option>
218:                                     <option value="<%if(request.getParameter("view").equals("Component")) {out.println("selected='selected'"); } %>">Component</option>
219:                                     <option value="<%if(request.getParameter("view").equals("Widget")) {out.println("selected='selected'"); } %>">Widget</option>
220:                                     <option value="<%if(request.getParameter("view").equals("User Guid")) {out.println("selected='selected'"); } %>">User Guid</option>


Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.NullPointerException
      org.apache.jsp.WEB_002dINF.jsp.VtqForm_jsp._jspService(VtqForm_jsp.java:350)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 36600185
JSP and JavaScript can work together. In the following code, I retain the values in the first two selects. You can do the same for the third and fourth.
<%
   String viewValue = request.getParameter("view");
   if(viewValue == null)viewValue = "Choose One";
   String applicationValue = request.getParameter("application");
   if(applicationValue == null)applicationValue = "Security";

%>
<html>
<head>
<script>
         function retainValue(){
              var element1 = document.forms[0].view;
              for(x = 0; x < element1.length; x++){
                      if(element1.options[x].value == "<%=viewValue%>"){
                            element1.options[x].selected = true;
                            break;
                      } 
              }
              var element2 = document.forms[0].application;
              for(x = 0; x < element2.length; x++){
                      if(element2.options[x].value == "<%=applicationValue%>"){
                            element2.options[x].selected = true;
                            break;
                      } 
              }
         }
</script>
</head>
<body onload="retainValue();">
<form method="post" id="teForm">
      <div id="dataEntryPoint" style=" background-color: activeborder; table-layout: auto; margin: 2px; margin-top:10px; margin-left:10px; width:1200px; height:80px; border: thin; border-color: black; border-style: dotted; ">
            <table>
                  <tr>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="startDateLbl" class="setFont" >Start Date/Time : </label>
                        </td>
                        <td>
                              <input id="startDate" class="setFont" name="startDate" title="Start Date/Time" type="text" value="<% out.write(request.getParameter("startDate")); %>"/>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="endDateLbl" class="setFont" >End Date/Time : </label>
                        </td>
                        
                        <td>
                              <input id="endDate" name="endDate" class="setFont" title="End Date/Time" type="text" value="<% out.write(request.getParameter("endDate")); %>"/>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="view" class="setFont" >View : </label>
                        </td>
                        <td>
                              <select id="view" name="view" class="setFont" style="width: auto;" tabindex="0">
                                    <option value="Choose One">Choose One</option>
                                    <option value="Vertical">Vertical</option>
                                    <option value="Component">Component</option>
                                    <option value="Widget">Widget</option>
                                    <option value="user Guid">User Guid</option>
                                    <option value="Session Id">Session Id</option>
                                    <option value="Root Guid">Root Guid</option>
                              </select>
                        </td>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="applicationLbl" class="setFont" >Application : </label>
                        </td>
                        <td>
                              <select id="application" name="application" class="setFont" style="width: auto;" tabindex="0">
                                    <option value="Choose One">Choose One</option>
                                    <option value="Build Reports">Build Reports</option>
                                    <option value="Clear App">Clear App</option>
                                    <option value="Security">Security</option>
                                    <option value="Document">Document</option>
                                    <option value="Clear EDD">Clear EDD</option>
                                    <option value="Unknown">Unknown</option>
                              </select>
                        </td>
                  </tr>      
                  <tr style="height: 20px;">
                  </tr>
                  <tr>
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="timeRangeLbl" class="setFont" >Time Range: </label>
                        </td>
                        <td>
                              <select id="timeRangeSel" name="timeRangeSel" class="setFont" style="width: auto;" tabindex="0">
                                    <option selected="selected">15 Minute</option>
                                    <option>Minute</option>
                                    <option>Day</option>
                                    <option>Month</option>
                              </select>
                        </td>                        
                        <td style="width: 2pc"></td>
                        <td>
                              <label id="middleTierLbl" class="setFont" >Middle Tier : </label>
                        </td>
                        <td>
                              <select id="middleTierSel" name="middleTierSel" class="setFont" style="width: auto;" tabindex="0">
                                    <option selected="selected">Client</option>
                                    <option>Production</option>
                              </select>
                        </td>
                        <td style="width: 80px;">
                        </td>
                        <td>
                              <span id="submitSpan">
                                          <input id="submitBtn" value="Submit" type="submit"/>
                                          <input id="hiddenBtn" type="hidden"/>
                              </span>
                        </td>
                  </tr>
            </table>
      </div>
</form>

</body>
</html>

Open in new window

0
 

Author Closing Comment

by:lakkarajumurali
ID: 36600237
thanks it works!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
"Any files you do not have backed up in at least two [other] places are files you do not care about."
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question