0

I have a jsp page which hyperlinks to another jsp page. If we click on "Update Preferences" from below shown image, then it opens the 2nd image:

image1

2nd image

Below is the code for the jsp shown in the 2nd image:

 <body>
  <center>      
             <div style="font-style:bold;font-size:30;color:black"> Data Platform - Monitor </div>
  </center>
   <BR><BR> 
      <%! 
                 VoltDAOImpl voltDao = new VoltDAOImpl();

      %>




       <form action="dp_monitor.jsp" method="get"  >



       <table width="350" border="0">

           <tr valign="Left">
             <td><b>Client Acronym:   </b> </td><td>      <b> <input type="text" name="client_acronym" value="PEAKM"></b></td>
           </tr>

           <tr valign="Left">
             <td><b>Sort By :</b></td>
             <td>
             <select name="sort_by">
            <option selected="true" value="notional">notional</option>
        <option value="arr_mid_slp">arr mid slippage</option>
        <option  value="order_qty">order qty</option>
        <option value="liq_consmption">liq consmption</option>
        <option value="arr_last_slp">arr last slippage</option>
        <option value="ivwap_slp">ivwap slppage</option>
        <option value="exec_qty">exec qty</option>
        <option value="leaves_qty">leaves qty</option>
        <option value="limit_px">limit px</option>
        <option value="avg_px">avg px</option>
        <option value="last_px">last px</option>
        <option value="last_qty">last qty</option>
        <option value="transact_time">transact time</option>
        <option value="arr_mid_px">arr mid px</option>
        <option value="ivwap_volume">ivwap volume</option>
        <option value="ivap_px">ivap px</option>
        <option value="arr_last_px">arr last px</option>
        <option value="end_last_px">end last px</option>
        </select>

             </td>
           </tr>


           <tr valign="Left">
             <td><b>Algo: </b>    </td>
             <td>
             <select name="algo">
           <option selected="true" value="">ALL</option>

               <%
               try{

                  List<String> algo_list=    voltDao.getDistinctAlgos("PEAKM",voltDao.client);
              for(String algo : algo_list )
              {
                out.println("<option value=\""+algo+"\">"+algo+"</option>");
              }


            } catch(Exception e){System.out.println( " error in getting algos"   ); }  
           %>

        </select>
           </tr>

           <tr valign="Left">
             <td><b>ListId: </b>   </td>
             <td><select name="listid">
           <option selected="true" value="">ALL</option>

               <%

               try {
                  List<String> list_id__list=    voltDao.getDistinctListID("PEAKM",voltDao.client);
              for(String list_id : list_id__list )
              {
                out.println("<option value=\""+list_id+"\">"+list_id+"</option>");
              }

              } catch(Exception e){System.out.println( " error in getting listids"   ); }  
           %>
        </select>
        </td>
           </tr>

           <tr valign="Left">
             <td><b>Refresh : </b>   </td>
             <td>
             <select name="refresh">
             <option  value="1">1 sec</option>
           <option  value="3">3 secs</option>
           <option  value="5">5 secs</option>
           <option value="10">10 secs</option>
           <option  value="15">15 secs</option>
           <option  value="30">30 secs</option>
           <option  value="45">45 secs</option>
           <option  value="60">1 min</option>
           <option  value="120">2 min</option>
           <option  value="180">3 min</option>
           <option  value="240">4 min</option>
           <option  value="300" selected>5 min</option>
         </select>
         </td>  </tr>
             <tr><td></td>  </tr>
             <tr><td></td>  </tr>
             <tr><td></td>  </tr>
              <tr>  <td><button type="submit">Update</button></td>
                 <td><button type="reset">Clear</button></td>
          </tr>
        </table>




         </form>






  </body>

So, everytime I select a preference, say for "Refresh" from the dropdown, I select 5 secs, then click on Update, so it goes back to the first jsp (the first image shown above). Now when I again click on "Update Preferences" then it shows the default selected value of 5 mins and not 5 secs. How do I get the preferences that were selected before to show everytime I click on Update Preferences?

UPDATE: I added this to my 2nd jsp:

<%
    String refreshValue = request.getParameter("refresh");

    if(refreshValue==null){
        refreshValue = "60";
    }

%>

             <td><b>Refresh : </b>   </td>
             <td>
             <select name="refresh">
<option value="1" <% if(refreshValue.equals("1")) { %> selected <% } %>>1 sec</option>
<option value="3" <% if(refreshValue.equals("3")) { %> selected <% } %>>3 secs</option>
<option value="5" <% if(refreshValue.equals("5")) { %> selected <% } %>>5 secs</option>
<option value="10" <% if(refreshValue.equals("10")) { %> selected <% } %>>10 secs</option>
<option value="15" <% if(refreshValue.equals("15")) { %> selected <% } %>>15 secs</option>
<option value="30" <% if(refreshValue.equals("30")) { %> selected <% } %>>30 secs</option>
<option value="45" <% if(refreshValue.equals("45")) { %> selected <% } %>>45 secs</option>
<option value="60" <% if(refreshValue.equals("60")) { %> selected <% } %>>1 min</option>
<option value="120" <% if(refreshValue.equals("120")) { %> selected <% } %>>2 min</option>
<option value="180" <% if(refreshValue.equals("180")) { %> selected <% } %>>3 min</option>
<option value="240" <% if(refreshValue.equals("240")) { %> selected <% } %>>4 min</option>
<option value="300" <% if(refreshValue.equals("300")) { %> selected <% } %>>5 min</option>
</select>

And this to my 1st jsp (dp_monitor):

<input type="hidden" name="refresh" value="<%=request.getParameter("refresh")%>">
<%String refresh = request.getParameter("refresh");
 if( ("".equals(refresh)||refresh==null )   )
        refresh="4000";



  // Set refresh, autoload time  
  response.setIntHeader("Refresh", Integer.parseInt(refresh));
  // Get current time
  Calendar calendar = new GregorianCalendar();
  String am_pm;
  int hour = calendar.get(Calendar.HOUR);
  int minute = calendar.get(Calendar.MINUTE);
  int second = calendar.get(Calendar.SECOND);
  if(calendar.get(Calendar.AM_PM) == 0)
     am_pm = "AM";
  else
     am_pm = "PM";

  if(hour == 0)
   hour = 12;

  String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
  out.println("Last Refresh Time: " + CT + "\n");%>

But, it's whenever I select some value from "Update Preferences", then click on "Update", and then come back, it is taking refreshValue as null only, and hence showing 1 min only as selected. What to do?

Angad
  • 213
  • 2
  • 5
  • 12
  • where do you store this preferences ? Because for now, you select `5 min` in the option ``. You need to get the saved value (file, db, cache) and use some JSTL to add the `selected` to the corresponding – AxelH Oct 07 '16 at 09:31

1 Answers1

0

I believe dp_monitor.jsp is your first jsp.

You need to read, preserve and transmit data between jsps.

To read the data you've to use request.getParameter("refresh")

To preserve the data you can use a hidden element inside the form element of the dp_monitor.jsp page

<input type="hidden" name="refresh" value="<%=request.getParameter("refresh")%>">

For transmitting the data form submission will do that trick.

In the second jsp you've to first check for the refresh parameter, if the value doesn't available then you've to assign the default value

<%
    String refreshValue = request.getParameter("refresh");

    if(refreshValue==null){
        refreshValue = "300;
    }

%>

then by using the refreshValue you can select the required option element.

Another approach is to store the values in session but I think the first approach is more suitable for your requirement

You can use the below code snippet to select the value. There are multiple ways available this is one of them

<select name="refresh">
<option value="1" <% if(refreshValue.equals("1")) { %> selected <% } %>>1 sec</option>
<option value="3" <% if(refreshValue.equals("3")) { %> selected <% } %>>3 secs</option>
<option value="5" <% if(refreshValue.equals("5")) { %> selected <% } %>>5 secs</option>
<option value="10" <% if(refreshValue.equals("10")) { %> selected <% } %>>10 secs</option>
<option value="15" <% if(refreshValue.equals("15")) { %> selected <% } %>>15 secs</option>
<option value="30" <% if(refreshValue.equals("30")) { %> selected <% } %>>30 secs</option>
<option value="45" <% if(refreshValue.equals("45")) { %> selected <% } %>>45 secs</option>
<option value="60" <% if(refreshValue.equals("60")) { %> selected <% } %>>1 min</option>
<option value="120" <% if(refreshValue.equals("120")) { %> selected <% } %>>2 min</option>
<option value="180" <% if(refreshValue.equals("180")) { %> selected <% } %>>3 min</option>
<option value="240" <% if(refreshValue.equals("240")) { %> selected <% } %>>4 min</option>
<option value="300" <% if(refreshValue.equals("300")) { %> selected <% } %>>5 min</option>
</select>

Another way is to set the option using jstl, you can refer this link

Community
  • 1
  • 1
vjy
  • 1,184
  • 1
  • 10
  • 24
  • Thank a lot. How do I select the required option element using refreshValue? – Angad Oct 07 '16 at 14:16
  • I added UPDATE to my question to tell you what's happening after doing as you suggested. Please have a look. – Angad Oct 12 '16 at 15:17
  • @Angad in `dp_monitor.jsp` add the `">` inside the `form tag` where the action points to second jsp – vjy Oct 13 '16 at 06:21
  • Please see my updated question. I'm doing that already, and I need to perform some operation with the String refresh also, so can't remove the 2nd line. – Angad Oct 13 '16 at 13:22
  • @Angad the solution I've provided will not work if you've used browser refresh button. If you navigate through the button clicks between the pages it'll surely transmit the data between pages. If you want make it work during refresh also then use cookies to store the values. Refer the link (http://www.w3schools.com/js/js_cookies.asp) for more details – vjy Oct 14 '16 at 05:14