
Suppose I have two jsp pages Search.jsp and Getdata.jsp. There are two fields in Search.jsp say Serial no,year and a button VIEW DATA. I want, when I click the button(VIEW DATA), Getdata.jsp will be loaded filling up all of its fields with records from database(against Serial no and year).That is, as soon as Getdata.jsp will be loaded, it will show the first record. How can I do this?

1)search.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(){
xmlHttp=GetXmlHttpObject()
var no=document.getElementById("sno").value;
var y=document.getElementById("year").value;
var url="getdata.jsp";
url=url+"?no="+no+"&&y="+y;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("tab").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();
}
catch(e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee" >
<br><br>
<table >
<div id="mydiv"></div>
<tr><td>Serial No:</td><td><input type="text" id="sno"></td></tr>
<tr><td>Year:</td><td><input type="text" id="year"></td></tr>
<tr><td></td><td><input type="button" value="View Data" onclick="showData();"></td></tr>
</table>
<table id="tab">
</table>
</body>
</html>
2)getdata.jsp:
<%@ page import="java.sql.*" %>
<%
int no = Integer.parseInt(request.getParameter("no"));
int y = Integer.parseInt(request.getParameter("y"));
String buffer="<table id='tab' border='1' >";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from student where id="+no+" and year="+y+"");
while(rs.next()){
buffer=buffer+"<tr><td>"+rs.getString("firstname")+"</td><td>"+rs.getString("lastname")+"</td></tr>";
}
buffer=buffer+"</table>";
response.getWriter().println(buffer);
System.out.println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>

view data is not working
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.