473,695 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting random values on refresh in JSP page

7 New Member
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
1 4920
jhardman
3,406 Recognized Expert Specialist
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

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

Similar topics

2
2451
by: JDJones | last post by:
I'm using the following to generate a random quote on a PHP page and it works fine: <?php $quotes = 'quote #1'; $quotes = 'quote #2'; $quotes = 'quote #3'; $quotes = 'etc...'; srand ((double) microtime() * 1000000);
5
4233
by: Phil Powell | last post by:
I created a page that will be doing image resizing and manipulation, which seems to work (using GD library). However, upon returning to the page where the image has been changed, I still see the old image, until I refresh my browser and then zappo, there it is! All changed! I have done everything I can think of to force caching including this: // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
3
8822
by: Jennie Friesen | last post by:
Hello-- I would like to display a different line of text (32 different messages) on refresh, BUT with no repeats. The script I am currently using is: <script language=javascript type=text/javascript> <!-- a = Math.floor(Math.random() * 32);
1
3227
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script that pulls images randomly from a directory? Thus I would need only add (or delete) images from that directory and they would display on the web page respectively. All the images are the same size (90x90) but there are hundreds so hard coding...
3
5025
by: TaTonka | last post by:
hi! how can i manage it (html or jscript with css) that everytime a user loads or refreshes a page, the page has a new bgcolor. i want to put it in a single file, so that all my pages have the same color, but after every refresh a new random one. thanx 4 your support
1
1678
by: robbiehenry | last post by:
There is this javascript reference site that I often use and I was looking for some script that would randomly show one of three images on page refresh. Here is the reference site that I use: http://www.chalcedony.com/javascript/scripts/index.html Here is their example: http://www.chalcedony.com/javascript/scripts/index.html
12
5224
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
3
2080
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: ************************************************* Function RandomString(size as integer, lowerCase as boolean) as string Dim builder as StringBuilder = new StringBuilder() Dim random as Random = new Random() Dim i as integer dim ch as char
10
10945
by: Joey_Stacks | last post by:
Does anyone know of a scipt that will rotate random div layers on page refresh? I have a primary content area front and center on my site homepage, and I'd like to rotate various chunks of html (in div layers) each time a user reloads the page. Right now I'm resorting to a random image rotation (those scripts are a dime a dozen) on refresh, but it's far from ideal for what I'm trying to accomplish. Any help on this would be GREATLY...
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.