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:
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!!!
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;
[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!!!
Comment