I have created a web application using JSP, CSS and HTML. The page contains six buttons, each of which calls a JavaScript method. For instance, the first button triggers the par()
method.
<html>
<head>
<title>Welcome d To Student University</title>
<link rel="stylesheet" type="text/css" href="../css/stlogin.css">
<link rel="stylesheet" type="text/css" href="../css/background.css">
<link rel="stylesheet" type="text/css" href="../css/back.css">
<script src="StInfo.jsp">
</script>
</head>
<body>
<div class=main>
<div class=blank></div>
<div class=welcome><h1 class=welcome><center>Welcome Student</center></h1>
<div class=logout><a href='logout.jsp?value=st'>Logout</a></div></div>
</div>
<br>
<div class=menu>
<div class=leftgap>.
</div>
<div class=option>
<center>
<button type="button" onclick=par() class="Bt_menu">Check Parsnal Info</button>
</center>
</div>
<div class=option>
<center>
<button type="button" onclick=faculty() class="Bt_menu">All Faculty Details</button>
</center>
</div>
<div class=option>
<center>
<button type="button" onclick=exam() class="Bt_menu">Next Exams Details</button>
</center>
</div>
<div class=option>
<center>
<button type="button" onclick=atten() class="Bt_menu">Attendance Details</button>
</center>
</div>
<div class=option>
<center>
<button type="button" onclick=Result() class="Bt_menu">Exam Result Details</button>
</center>
</div>
<div class=option>
<center>
<button type="button" onclick=Notices() class="Bt_menu">College Notices / Details</button>
</center>
</div>
</div>
<p id=Table></p>
</body>
</html>
In this script tag is used as follows:
<script src="StInfo.jsp">
</script>
Now, let me show you my StInfo.jsp file where the Java script methods are located.
<%@page import="data.*;" %>
<%
ServletConfig con=getServletConfig();
ServletContext ctx=con.getServletContext();
DataRet d;
%>
function par()
{
try
{
// I set ctx.setAttribute("id") to 1 in the previous page, so here the output will be 1.
<%DataRet.setAtt(""+ctx.getAttribute("id"),"stlogin");%>
var id=<%=ctx.getAttribute("id")%>; // When I do this, the value 1 is stored in 'id'.
var id=<%=DataRet.get(2)%>; // However, when I do this, nothing happens and the code doesn't work.
}catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
document.getElementById("Table").innerHTML="<center><table border='10'><th>College Id</th> <th>Name</th><th>Father Name</th><th>Department</th><th>Year</th><th>Semester</th><th>Ph. No.</th><th>Address</th>\
<tr>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
<td>"+id+"</td>\
</tr></table><center>";
}
And here is my DataRet File:
package data;
import java.sql.*;
import connection.connection;
public class DataRet
{
static Connection c;
static ResultSet re;
static Statement s;
static String id;
static
{
try
{
c=connection.getConnect();
System.out.println(c);
s = c.createStatement();
System.out.println("Statement Object Created = "+s);
}catch(Exception e){System.out.println(e);}
}
public static void setAtt(String table)
{
try {
re=s.executeQuery("select * from "+table);
}catch(Exception e){}
}
public static void setAtt(String att,String table)
{
System.out.println("Table Started");
id=att;
int i=0;
try
{
re=s.executeQuery("select * from "+table);
while(re.next() && re.getString(3).equals(att))
{
i++;
break;
}
System.out.println("cursor on "+i);
}catch(Exception e){System.out.println(e);}
}
public static void change()
{
try{
re.next();
}catch(Exception e){}
}
public static String get(int val)
{
System.out.println("value obtained of "+val);
try{
String o=re.getString(val);
System.out.println(o);
return o;
}catch(Exception e){ System.out.println("Problem in Getting Value"+e);}
System.out.println("return null");
return null;
}
}
The issue arises when I call the method *<%=ctx.getAttribute("id")%> * in StInfo.jsp, it prints 1 in every column. However, when I try calling <%=DataRet.get(2)%>, the file fails to work properly...