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

Getting random values on refresh in JSP page

P: 7
Hi

I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button.
All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button.

The Category name is being fetched from the oracle db along with the corresponding Category order.

In the corresponding input field (text box) the user enters a new category order which gets stored in the db on using the submit button.
The new category order (number) has to be less or equal to the count of the categories . On Submitting the New category order a sql procedure is called which updates the order and then the new order is displayed in the category Order column in the same jsp page.

The procedure looks like this:
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PROCEDURE proc_ordUpdate
  2.  
  3. (oldOrder IN number,newOrder IN number, catname IN varchar2 )
  4. IS
  5. begin
  6. if oldOrder>0 and newOrder>0 and oldOrder!=newOrder then
  7. if oldOrder<newOrder then
  8. update cms_video_info set CATORDER=-1 where CATORDER = oldOrder and CATNAME=catname;
  9. update cms_video_info set CATORDER=CATORDER-1 where CATNAME=catname and CATORDER between oldOrder+1 and newOrder;
  10. update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
  11. else
  12. update cms_video_info set CATORDER=-1 where CATORDER=oldOrder and CATNAME=catname;
  13. update cms_video_info set CATORDER=CATORDER+1 where CATNAME=catname and CATORDER between newOrder and oldOrder-1;
  14. update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
  15. end if;
  16.  
My jsp page looks like this:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" %>
  2. <%@ page import="java.sql.*" %>
  3. <%@ page import="java.util.*" %>
  4. <%@ page import="java.io.*" %>
  5. <%@ page import="java.security.*" %>
  6. <%@ page import="java.net.URL" %>
  7. <%@ page import="java.net.HttpURLConnection" %>
  8. <%@ page import="com.btsl.*" %>
  9. <%
  10. response.setHeader("Cache-Control","no-cache");
  11. response.setHeader("Pragma","no-cache");
  12. response.setDateHeader ("Expires", 60);
  13. %>
  14.  
  15. <script type="text/javascript">
  16. function FormAction(cost_rad)
  17. {
  18.   document.frm1.submit();
  19. }
  20. </script>
  21.  
  22. <script language="JavaScript">
  23. function update(old_catorder,new_catorder,catname,seq,count)
  24.    if (new_catorder == old_catorder){
  25.     alert("New order and Old order should not be same");
  26.     return false;
  27.   }
  28.   else if (new_catorder > count) {
  29.     alert("New Order should be between 1 and "+count);
  30.     return false;
  31.   }
  32. else{
  33.   var field_pos="new_catorder"+seq;
  34.   //alert(field_pos)
  35.   //alert("new_catorder:"+document.getElementById(field_pos).value);
  36.  
  37.   document.frm1.old_catorder.value=old_catorder;
  38.   document.frm1.new_catorder.value=document.getElementById(field_pos).value;
  39.   document.frm1.catname.value=catname;
  40.  
  41.  alert(document.frm1.catname.name);
  42.  alert(document.frm1.catname.value);
  43.  alert(document.frm1.old_catorder.value);
  44.  alert(document.frm1.new_catorder.value);
  45.  
  46.   document.frm1.submit();
  47.   return true;
  48. }
  49. }
  50. </script>
  51.  
  52.  
  53.  
  54. <%
  55. int old_catorder=0;
  56. int new_catorder=0;
  57. String catname="";
  58. int count=0;
  59. int curr_catorder=0;
  60. String cost_rad="";
  61. String query ="";
  62.  
  63. Connection db_conn = null;
  64. Statement db_st = null;
  65. ResultSet rs =null;
  66. CallableStatement proc=null;
  67.  
  68. Vector<String> catnameVec = new Vector<String>(1,1);
  69. Vector<Integer> old_CatOrderVec  = new Vector<Integer>(1,1);
  70.  
  71. if((String)request.getParameter("cost_rad")!=null)
  72. {
  73.   cost_rad=request.getParameter("cost_rad");
  74. }
  75.  
  76. System.out.println("Selected cost_rad is :: "+cost_rad);
  77.  
  78. try {
  79.   db_conn = OracleConnectionUtil.getConnection();
  80.   db_st=db_conn.createStatement();
  81.  
  82.   if (cost_rad.equals("Free"))
  83.   {
  84.     System.out.println("applying free query");
  85.     query = "select distinct catname,catorder from cms_video_info where cost ='0' order by catorder asc";
  86.   }
  87.   else
  88.   {
  89.     System.out.println("applying paid query");
  90.     query = "select distinct catname,catorder from cms_video_info where cost !='0' order by catorder asc";
  91.   }
  92.  
  93.   try
  94.   {
  95.     rs=db_st.executeQuery(query);
  96.   }
  97.   catch(Exception e)
  98.   {
  99.     System.out.println("ERROR!!! Exception while getting catname");
  100.   }
  101.  
  102. /* if not null print the result of query */
  103.  
  104.   if(rs !=null)
  105.   {
  106.     System.out.println("value of rs:"+rs);
  107.     try
  108.     {
  109.       while(rs.next())
  110.       {
  111.         try
  112.         {
  113.           catnameVec.add((rs.getString(1)).trim());
  114.           old_catorder=rs.getInt(2);
  115.           old_CatOrderVec.add(old_catorder);
  116.         }
  117.         catch(SQLException subE)
  118.         {
  119.           System.out.println("ERROR!!! sql exception while fetching data" +subE);
  120.         }
  121.       }
  122.     } catch(Exception er)
  123.     {
  124.       System.out.println("ERROR!!! problem in result set" +er);
  125.     }
  126.   }
  127. try{
  128.   if((String)request.getParameter("catname")!= null)
  129.   {
  130. System.out.println("catname is" );
  131.     catname=request.getParameter("catname");
  132.     System.out.println("catname is" +catname);
  133.   }
  134.  
  135.   if(Integer.parseInt((String)request.getParameter("new_catorder").trim())!=0)
  136.   {
  137.     System.out.println("new_catorder is" );
  138. new_catorder=Integer.parseInt((String)request.getParameter("new_catorder").trim());
  139.     System.out.println("new catorder is " +new_catorder);
  140.   }
  141. }catch(Exception e)
  142. {
  143. System.out.println("Exception catname old_catorder"+old_catorder);
  144. }
  145.  
  146. if(Integer.parseInt((String)request.getParameter("old_catorder").trim())!=0)
  147.   {
  148. old_catorder=Integer.parseInt((String)request.getParameter("old_catorder").trim());
  149.     System.out.println("old catorder is " +old_catorder);
  150.  
  151.     try
  152.     {
  153.  
  154.      System.out.println("Proc calling");  
  155.       String strQuery = "{call proc_ordUpdate(?,?,?)}";
  156.       proc = db_conn.prepareCall(strQuery);
  157.             //proc.registerOutParameter(3,Types.VARCHAR);
  158.           System.out.println("Execution update values:old_catorder"+old_catorder+"new_catorder"+new_catorder+"catname"+catname);
  159.       proc.setInt(1,old_catorder);
  160.       System.out.println("old_catorder="+old_catorder);
  161.       proc.setInt(2,new_catorder);
  162.       System.out.println("new_catorder="+new_catorder);
  163.       proc.setString(3,catname);
  164.       System.out.println("catname="+catname);
  165.  
  166.       proc.registerOutParameter(3,Types.VARCHAR);
  167.             System.out.println("executing Proc");
  168.             try{
  169.         System.out.println("inside :executing Proc");
  170.       proc.execute();
  171. }catch(Exception e)
  172. {
  173. System.out.println("executing ProcEXECP");
  174. }
  175.             System.out.println("executing completed");
  176.       System.out.println("old catorder updated successfully");
  177.     } catch(Exception ex)
  178.     {
  179.       System.out.println("ERROR!!! failed to update catorder" +ex);
  180.     }
  181.   }
  182.  
  183. }
  184. catch(Exception e)
  185. {
  186.   e.printStackTrace();
  187.   System.out.println("ERROR After Procedure!!! unable to connect to oracle DB");
  188. }
  189. finally
  190. {
  191.   if (proc != null)
  192.   {
  193.     proc.close();
  194.     proc = null;
  195.   }
  196.  
  197.   if(db_conn!=null)
  198.   {
  199.     db_conn.commit();
  200.     rs.close();
  201.     db_st.close();
  202.     db_conn.close();
  203.   }
  204. }
  205. %>
  206.  
  207. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  208. <html xmlns="http://www.w3.org/1999/xhtml">
  209. <head>
  210. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. <script type="text/javascript">
  219. function reloadPage()
  220. { window.location.reload(); }
  221. </script>
  222.  
  223. </head>
  224.  
  225. <form  name=frm1 action="vem_catOrder.jsp" method="Post">
  226.  
  227. <body>
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. <table width="95%"; align="center"; border="2"; cellpadding="0">
  236. <tr height="30">
  237. <th width="38%"; align="center">Category</th>
  238. <th width="32%"; align="center">Current Order</th>
  239. <th width="25%"; align="center">Set Order</th>
  240.  
  241. </tr>
  242. </table>
  243.  
  244. <div style="overflow:auto; width:100%; height:110px" align="center">
  245. <table width="95%"; align="center"; border="1"; cellpadding="0"; cellspacing="2">
  246.  
  247. <%
  248. try {
  249.   for (int i =0; i < catnameVec.size(); i++)
  250.   {
  251.       %>
  252.       <tr>
  253.       <td width="38%"; align="center";>
  254.       <input type=hidden name='<%="catname"+i%>' id=catname"+i+" value='<%=catnameVec.elementAt(i)%>'/><%=catnameVec.elementAt(i)%>
  255.       </td>
  256.       <td width="32%"; align="center";>
  257.       <input type=hidden name=old_catorder"+i+" id=old_catorder"+i+" value='<%=old_CatOrderVec.elementAt(i)%>'/><%=old_CatOrderVec.elementAt(i)%>
  258.       </td>
  259.       <td width="15%"; align="center";>
  260.       <INPUT type="text" name='<%="new_catorder"+i%>' id='<%="new_catorder"+i%>'  style="WIDTH: 80px" maxLength=15 value=""/>
  261.       </td>
  262.  
  263.       <td width="10%" align="center">
  264.       <input type="button" name=button"+i+" id=button"+i+" onClick='return update("<%=old_CatOrderVec.elementAt(i)%>","<%=new_catorder%>","<%=catnameVec.elementAt(i)%>",<%=i%>,<%=catnameVec.size()%>)' value="Submit"/></td>
  265.       </tr>
  266.       <%
  267.   }
  268. }
  269. catch(Exception e) {
  270.   System.out.println("got Exception"+e);
  271. }
  272. %>
  273.  
  274. </table>
  275. </div>
  276.  
  277. <INPUT type=hidden name=old_catorder value="">
  278. <INPUT type=hidden name=new_catorder value="">
  279. <INPUT type=hidden name=catname value="">
  280.  
  281. <br />
  282.  
  283. </form>
  284.  
  285. </body>
  286. </html>
  287.  
  288.  
  289.        else
  290.  
  291.           dbms_output.put_line('please check the new order and old order');
  292.  
  293.      end if;
  294.  
  295. end;
  296.  
My problem is that after i submit the page the previous given value is displayed and on calling refresh i get a random values. I guess the function update gets called everytime i refresh the page.
someone suggested to use a request dispatcher.
I have no clues as to how to go about it.

Please HELP!!!
Aug 27 '08 #1
Share this Question
Share on Google+
1 Reply


jhardman
Expert 2.5K+
P: 3,405
raghuvendra,

You posted way too much code, there is no way I am going to browse through 300 lines to find a problem, but no worries! In the future just show the parts of the code that we are interested in.

The real problem is that I don't understand what the problem is - you need to explain what the page SHOULD DO and then explain what it ACTUALLY DOES. You might have said what it is doing, but you didn't say what it should do. It sounds to me that the real issue might be that the page you are displaying is the result of a form submission, so that when you refresh the page, you have essentially re-submitted the form, but I'm not sure because your explanation is unclear and I'm not going to read through that much code to try to decipher what you meant.

If that is the case, you will need to check the db to see if the data you entered has already been entered, and if so you will need to block it, or at least alert the user "This info is already in the db". Let me know if this helps.

Jared
Aug 29 '08 #2

Post your reply

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