Solved

Retain the value of Drop down box in jsp

Posted on 2011-09-26
6
3,619 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 27

Accepted Solution

by:
rrz earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stuck in Bootstrap WysiHtml Editor 3 37
servlet concurrency 13 92
maven j2ee examles 2 49
java operators 3 110
Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now