REFRESH Problem IN JSP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghuvendra
    New Member
    • Aug 2008
    • 7

    REFRESH Problem IN JSP

    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:

    Code:
    CREATE OR REPLACE PROCEDURE proc_ordUpdate
    
    (oldOrder IN number,newOrder IN number, catname IN varchar2 )
    IS
    begin
    if oldOrder>0 and newOrder>0 and oldOrder!=newOrder then
    if oldOrder<newOrder then
    update cms_video_info set CATORDER=-1 where CATORDER = oldOrder and CATNAME=catname;
    update cms_video_info set CATORDER=CATORDER-1 where CATNAME=catname and CATORDER between oldOrder+1 and newOrder;
    update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
    else
    update cms_video_info set CATORDER=-1 where CATORDER=oldOrder and CATNAME=catname;
    update cms_video_info set CATORDER=CATORDER+1 where CATNAME=catname and CATORDER between newOrder and oldOrder-1;
    update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
    end if;
    My jsp page looks like this:

    [CODE=java]<%@ page language="java" %>
    <%@ page import="java.sq l.*" %>
    <%@ page import="java.ut il.*" %>
    <%@ page import="java.io .*" %>
    <%@ page import="java.se curity.*" %>
    <%@ page import="java.ne t.URL" %>
    <%@ page import="java.ne t.HttpURLConnec tion" %>
    <%@ page import="com.bts l.*" %>
    <%
    response.setHea der("Cache-Control","no-cache");
    response.setHea der("Pragma","n o-cache");
    response.setDat eHeader ("Expires", 60);
    %>

    <script type="text/javascript">
    function FormAction(cost _rad)
    {
    document.frm1.s ubmit();
    }
    </script>

    <script language="JavaS cript">
    function update(old_cato rder,new_catord er,catname,seq, count )
    {
    if (new_catorder == old_catorder){
    alert("New order and Old order should not be same");
    return false;
    }
    else if (new_catorder > count) {
    alert("New Order should be between 1 and "+count);
    return false;
    }
    else{
    var field_pos="new_ catorder"+seq;
    //alert(field_pos )
    //alert("new_cato rder:"+document .getElementById (field_pos).val ue);

    document.frm1.o ld_catorder.val ue=old_catorder ;
    document.frm1.n ew_catorder.val ue=document.get Eleme ntById(field_po s).value;
    document.frm1.c atname.value=ca tname;

    alert(document. frm1.catname.na me);
    alert(document. frm1.catname.va lue);
    alert(document. frm1.old_catord er.value);
    alert(document. frm1.new_catord er.value);

    document.frm1.s ubmit();
    return true;
    }
    }
    </script>



    <%
    int old_catorder=0;
    int new_catorder=0;
    String catname="";
    int count=0;
    int curr_catorder=0 ;
    String cost_rad="";
    String query ="";

    Connection db_conn = null;
    Statement db_st = null;
    ResultSet rs =null;
    CallableStateme nt proc=null;

    Vector<String> catnameVec = new Vector<String>( 1,1);
    Vector<Integer> old_CatOrderVec = new Vector<Integer> (1,1);

    if((String)requ est.getParamete r("cost_rad")!= null)
    {
    cost_rad=reques t.getParameter( "cost_rad") ;
    }

    System.out.prin tln("Selected cost_rad is :: "+cost_rad) ;

    try {
    db_conn = OracleConnectio nUtil.getConnec tion();
    db_st=db_conn.c reateStatement( );

    if (cost_rad.equal s("Free"))
    {
    System.out.prin tln("applying free query");
    query = "select distinct catname,catorde r from cms_video_info where cost ='0' order by catorder asc";
    }
    else
    {
    System.out.prin tln("applying paid query");
    query = "select distinct catname,catorde r from cms_video_info where cost !='0' order by catorder asc";
    }

    try
    {
    rs=db_st.execut eQuery(query);
    }
    catch(Exception e)
    {
    System.out.prin tln("ERROR!!! Exception while getting catname");
    }

    /* if not null print the result of query */

    if(rs !=null)
    {
    System.out.prin tln("value of rs:"+rs);
    try
    {
    while(rs.next() )
    {
    try
    {
    catnameVec.add( (rs.getString(1 )).trim());
    old_catorder=rs .getInt(2);
    old_CatOrderVec .add(old_catord er);
    }
    catch(SQLExcept ion subE)
    {
    System.out.prin tln("ERROR!!! sql exception while fetching data" +subE);
    }
    }
    } catch(Exception er)
    {
    System.out.prin tln("ERROR!!! problem in result set" +er);
    }
    }
    try{
    if((String)requ est.getParamete r("catname")! = null)
    {
    System.out.prin tln("catname is" );
    catname=request .getParameter(" catname");
    System.out.prin tln("catname is" +catname);
    }

    if(Integer.pars eInt((String)re quest.getParame ter("new_catord er").trim())!=0 )
    {
    System.out.prin tln("new_catord er is" );
    new_catorder=In teger.parseInt( (String)request .getP arameter("new_c atorder").trim( ));
    System.out.prin tln("new catorder is " +new_catorder);
    }
    }catch(Exceptio n e)
    {
    System.out.prin tln("Exception catname old_catorder"+o ld_catorder);
    }

    if(Integer.pars eInt((String)re quest.getParame ter("old_catord er").trim())!=0 )
    {
    old_catorder=In teger.parseInt( (String)request .getP arameter("old_c atorder").trim( ));
    System.out.prin tln("old catorder is " +old_catorder);

    try
    {

    System.out.prin tln("Proc calling");
    String strQuery = "{call proc_ordUpdate( ?,?,?)}";
    proc = db_conn.prepare Call(strQuery);
    //proc.registerOu tParameter(3,Ty pes.VARCHAR);
    System.out.prin tln("Execution update values:old_cato rder"+old_cator der+"new_catord er"+new_catorde r+"catname"+cat name);
    proc.setInt(1,o ld_catorder);
    System.out.prin tln("old_catord er="+old_catord er);
    proc.setInt(2,n ew_catorder);
    System.out.prin tln("new_catord er="+new_catord er);
    proc.setString( 3,catname);
    System.out.prin tln("catname="+ catname);

    proc.registerOu tParameter(3,Ty pes.VARCHAR);
    System.out.prin tln("executing Proc");
    try{
    System.out.prin tln("inside :executing Proc");
    proc.execute();
    }catch(Exceptio n e)
    {
    System.out.prin tln("executing ProcEXECP");
    }
    System.out.prin tln("executing completed");
    System.out.prin tln("old catorder updated successfully");
    } catch(Exception ex)
    {
    System.out.prin tln("ERROR!!! failed to update catorder" +ex);
    }
    }

    }
    catch(Exception e)
    {
    e.printStackTra ce();
    System.out.prin tln("ERROR After Procedure!!! unable to connect to oracle DB");
    }
    finally
    {
    if (proc != null)
    {
    proc.close();
    proc = null;
    }

    if(db_conn!=nul l)
    {
    db_conn.commit( );
    rs.close();
    db_st.close();
    db_conn.close() ;
    }
    }
    %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />







    <script type="text/javascript">
    function reloadPage()
    { window.location .reload(); }
    </script>

    </head>

    <form name=frm1 action="vem_cat Order.jsp" method="Post">

    <body>







    <table width="95%"; align="center"; border="2"; cellpadding="0" >
    <tr height="30">
    <th width="38%"; align="center"> Category</th>
    <th width="32%"; align="center"> Current Order</th>
    <th width="25%"; align="center"> Set Order</th>

    </tr>
    </table>

    <div style="overflow :auto; width:100%; height:110px" align="center">
    <table width="95%"; align="center"; border="1"; cellpadding="0" ; cellspacing="2" >

    <%
    try {
    for (int i =0; i < catnameVec.size (); i++)
    {
    %>
    <tr>
    <td width="38%"; align="center"; >
    <input type=hidden name='<%="catna me"+i%>' id=catname"+i+" value='<%=catna meVec.elementAt (i)%>'/><%=catnameVec. elementAt(i)%>
    </td>
    <td width="32%"; align="center"; >
    <input type=hidden name=old_catord er"+i+" id=old_catorder "+i+" value='<%=old_C atOrderVec.elem entAt(i)%>'/><%=old_CatOrde rVec.elementAt( i)%>
    </td>
    <td width="15%"; align="center"; >
    <INPUT type="text" name='<%="new_c atorder"+i%>' id='<%="new_cat order"+i%>' style="WIDTH: 80px" maxLength=15 value=""/>
    </td>

    <td width="10%" align="center">
    <input type="button" name=button"+i+ " id=button"+i+" onClick='return update("<%=old_ CatOrderVec.ele mentAt(i)%>","< %=new_catorder% >","<%=catnameV ec.elementAt(i) %>",<%=i%>,<%=c atnameVec.size( )%>)' value="Submit"/></td>
    </tr>
    <%
    }
    }
    catch(Exception e) {
    System.out.prin tln("got Exception"+e);
    }
    %>

    </table>
    </div>

    <INPUT type=hidden name=old_catord er value="">
    <INPUT type=hidden name=new_catord er value="">
    <INPUT type=hidden name=catname value="">

    <br />

    </form>

    </body>
    </html>


    else

    dbms_output.put _line('please check the new order and old order');

    end if;

    end;[/CODE]

    ############### ############### ############### ##### ############### #

    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!!!
    Last edited by gits; Aug 27 '08, 10:04 AM. Reason: added code tags
  • raghuvendra
    New Member
    • Aug 2008
    • 7

    #2
    REFRESH Problem IN JSP

    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:

    Code:
    CREATE OR REPLACE PROCEDURE proc_ordUpdate
    
    (oldOrder IN number,newOrder IN number, catname IN varchar2 )
    IS
    begin
    if oldOrder>0 and newOrder>0 and oldOrder!=newOrder then
    if oldOrder<newOrder then
    update cms_video_info set CATORDER=-1 where CATORDER = oldOrder and CATNAME=catname;
    update cms_video_info set CATORDER=CATORDER-1 where CATNAME=catname and CATORDER between oldOrder+1 and newOrder;
    update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
    else
    update cms_video_info set CATORDER=-1 where CATORDER=oldOrder and CATNAME=catname;
    update cms_video_info set CATORDER=CATORDER+1 where CATNAME=catname and CATORDER between newOrder and oldOrder-1;
    update cms_video_info set CATORDER=newOrder where CATORDER=-1 and CATNAME=catname;
    end if;
    My jsp page looks like this:

    Code:
    <%@ page language="java" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.util.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.security.*" %>
    <%@ page import="java.net.URL" %>
    <%@ page import="java.net.HttpURLConnection" %>
    <%@ page import="com.btsl.*" %>
    <%
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 60);
    %>
    
    <script type="text/javascript">
    function FormAction(cost_rad)
    {
    document.frm1.submit();
    }
    </script>
    
    <script language="JavaScript">
    function update(old_catorder,new_catorder,catname,seq,count )
    {
    if (new_catorder == old_catorder){
    alert("New order and Old order should not be same");
    return false;
    }
    else if (new_catorder > count) {
    alert("New Order should be between 1 and "+count);
    return false;
    }
    else{
    var field_pos="new_catorder"+seq;
    //alert(field_pos)
    //alert("new_catorder:"+document.getElementById(field_pos).value);
    
    document.frm1.old_catorder.value=old_catorder;
    document.frm1.new_catorder.value=document.getEleme ntById(field_pos).value;
    document.frm1.catname.value=catname;
    
    alert(document.frm1.catname.name);
    alert(document.frm1.catname.value);
    alert(document.frm1.old_catorder.value);
    alert(document.frm1.new_catorder.value);
    
    document.frm1.submit();
    return true;
    }
    }
    </script>
    
    
    
    <%
    int old_catorder=0;
    int new_catorder=0;
    String catname="";
    int count=0;
    int curr_catorder=0;
    String cost_rad="";
    String query ="";
    
    Connection db_conn = null;
    Statement db_st = null;
    ResultSet rs =null;
    CallableStatement proc=null;
    
    Vector<String> catnameVec = new Vector<String>(1,1);
    Vector<Integer> old_CatOrderVec = new Vector<Integer>(1,1);
    
    if((String)request.getParameter("cost_rad")!=null)
    {
    cost_rad=request.getParameter("cost_rad");
    }
    
    System.out.println("Selected cost_rad is :: "+cost_rad);
    
    try {
    db_conn = OracleConnectionUtil.getConnection();
    db_st=db_conn.createStatement();
    
    if (cost_rad.equals("Free"))
    {
    System.out.println("applying free query");
    query = "select distinct catname,catorder from cms_video_info where cost ='0' order by catorder asc";
    }
    else
    {
    System.out.println("applying paid query");
    query = "select distinct catname,catorder from cms_video_info where cost !='0' order by catorder asc";
    }
    
    try
    {
    rs=db_st.executeQuery(query);
    }
    catch(Exception e)
    {
    System.out.println("ERROR!!! Exception while getting catname");
    }
    
    /* if not null print the result of query */
    
    if(rs !=null)
    {
    System.out.println("value of rs:"+rs);
    try
    {
    while(rs.next())
    {
    try
    {
    catnameVec.add((rs.getString(1)).trim());
    old_catorder=rs.getInt(2);
    old_CatOrderVec.add(old_catorder);
    }
    catch(SQLException subE)
    {
    System.out.println("ERROR!!! sql exception while fetching data" +subE);
    }
    }
    } catch(Exception er)
    {
    System.out.println("ERROR!!! problem in result set" +er);
    }
    }
    try{
    if((String)request.getParameter("catname")!= null)
    {
    System.out.println("catname is" );
    catname=request.getParameter("catname");
    System.out.println("catname is" +catname);
    }
    
    if(Integer.parseInt((String)request.getParameter("new_catorder").trim())!=0)
    {
    System.out.println("new_catorder is" );
    new_catorder=Integer.parseInt((String)request.getP arameter("new_catorder").trim());
    System.out.println("new catorder is " +new_catorder);
    }
    }catch(Exception e)
    {
    System.out.println("Exception catname old_catorder"+old_catorder);
    }
    
    if(Integer.parseInt((String)request.getParameter("old_catorder").trim())!=0)
    {
    old_catorder=Integer.parseInt((String)request.getP arameter("old_catorder").trim());
    System.out.println("old catorder is " +old_catorder);
    
    try
    {
    
    System.out.println("Proc calling");
    String strQuery = "{call proc_ordUpdate(?,?,?)}";
    proc = db_conn.prepareCall(strQuery);
    //proc.registerOutParameter(3,Types.VARCHAR);
    System.out.println("Execution update values:old_catorder"+old_catorder+"new_catorder"+new_catorder+"catname"+catname);
    proc.setInt(1,old_catorder);
    System.out.println("old_catorder="+old_catorder);
    proc.setInt(2,new_catorder);
    System.out.println("new_catorder="+new_catorder);
    proc.setString(3,catname);
    System.out.println("catname="+catname);
    
    proc.registerOutParameter(3,Types.VARCHAR);
    System.out.println("executing Proc");
    try{
    System.out.println("inside :executing Proc");
    proc.execute();
    }catch(Exception e)
    {
    System.out.println("executing ProcEXECP");
    }
    System.out.println("executing completed");
    System.out.println("old catorder updated successfully");
    } catch(Exception ex)
    {
    System.out.println("ERROR!!! failed to update catorder" +ex);
    }
    }
    
    }
    catch(Exception e)
    {
    e.printStackTrace();
    System.out.println("ERROR After Procedure!!! unable to connect to oracle DB");
    }
    finally
    {
    if (proc != null)
    {
    proc.close();
    proc = null;
    }
    
    if(db_conn!=null)
    {
    db_conn.commit();
    rs.close();
    db_st.close();
    db_conn.close();
    }
    }
    %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    
    
    
    
    
    
    <script type="text/javascript">
    function reloadPage()
    { window.location.reload(); }
    </script>
    
    </head>
    
    <form name=frm1 action="vem_catOrder.jsp" method="Post">
    
    <body>
    
    
    
    
    
    
    
    <table width="95%"; align="center"; border="2"; cellpadding="0">
    <tr height="30">
    <th width="38%"; align="center">Category</th>
    <th width="32%"; align="center">Current Order</th>
    <th width="25%"; align="center">Set Order</th>
    
    </tr>
    </table>
    
    <div style="overflow:auto; width:100%; height:110px" align="center">
    <table width="95%"; align="center"; border="1"; cellpadding="0"; cellspacing="2">
    
    <%
    try {
    for (int i =0; i < catnameVec.size(); i++)
    {
    %>
    <tr>
    <td width="38%"; align="center";>
    <input type=hidden name='<%="catname"+i%>' id=catname"+i+" value='<%=catnameVec.elementAt(i)%>'/><%=catnameVec.elementAt(i)%>
    </td>
    <td width="32%"; align="center";>
    <input type=hidden name=old_catorder"+i+" id=old_catorder"+i+" value='<%=old_CatOrderVec.elementAt(i)%>'/><%=old_CatOrderVec.elementAt(i)%>
    </td>
    <td width="15%"; align="center";>
    <INPUT type="text" name='<%="new_catorder"+i%>' id='<%="new_catorder"+i%>' style="WIDTH: 80px" maxLength=15 value=""/>
    </td>
    
    <td width="10%" align="center">
    <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>
    </tr>
    <%
    }
    }
    catch(Exception e) {
    System.out.println("got Exception"+e);
    }
    %>
    
    </table>
    </div>
    
    <INPUT type=hidden name=old_catorder value="">
    <INPUT type=hidden name=new_catorder value="">
    <INPUT type=hidden name=catname value="">
    
    <br />
    
    </form>
    
    </body>
    </html>
    
    
    else
    
    dbms_output.put_line('please check the new order and old order');
    
    end if;
    
    end;
    ############### ############### ############### ##### ############### #

    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!!!
    Last edited by acoder; Aug 27 '08, 11:01 AM. Reason: Added [code] tags

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Yup....
      If you are trying to handle that transaction in just a page, and when you attempt to click the refresh button(from browser), a null value will be received from all request.getPara meter("form member name") method....

      That may lead to:
      -Unexpected result
      -Allocate most of the time in debugging(hard to debug)
      -More lines of unnecessary scriptlets
      -More experiments to test the code....

      Why don't you add another jsp document that handles all values and then process.... starts from database(better to use bean) until input handling.... and redirect back into the main page(needs a bean, yet not prone to error if tested)?

      I think you're not using javabean here.....(use to separate transaction from presentation).. ..

      regards,
      sukatoa

      Comment

      • ajos
        Contributor
        • Aug 2007
        • 283

        #4
        Originally posted by raghuvendra
        Hi

        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!!!
        Im not sure what the actual problem here is, not gone through your code completely yet.

        My problem is that after i submit the page the previous given value is displayed and on calling refresh i get a random values
        But by your above statement i think you have a caching problem here.

        Try putting this in right after your <body> tag and see if this helps.
        Code:
        <% response.setHeader("Cache-Control","no-cache"); //HTTP 1.1 response.setHeader("Pragma","no-cache"); //HTTP 1.0 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server response.setHeader("Cache-Control","no-store"); //HTTP 1.1 %>
        I feel you can do better by putting the scriptlet code from the jsp page to a servlet class which will help you in the long run trying to maintain and debug the code. Its a beast trying to debug this code if you are not sure where the problem is, also the next time when you post your code try posting your code inside the code tags which is the hash(#) button on the tool bar above.

        Edit:- Too late it seems ;).
        Edit2:- You seem to put the noCache statements, will have to give your code a closer look it seems.

        regards,

        ajos
        Last edited by ajos; Aug 27 '08, 09:49 AM. Reason: too late in replying

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          raghuvendra, JavaScript does not equal JSP. There are two very different languages. I've merged your threads after moving one.

          Please also remember to use code tags when posting code. See How to Ask a Question. Thanks!

          Comment

          Working...