<% int n1, n2; n1 = Integer.parseInt( request.getParameter("num1")); n2 = Integer.parseInt( request.getParameter("num2")); out.println(n1 + n2); %>
<html> <script language="javascript" src="jquery-1.2.6.js"></script> <script language="javascript"> function addNumbers(){ // use get method to make an AJAX call to addnumbers.jsp $.get( "addnumbers.jsp",{num1 : $("#num1").val(), num2 : $("#num2").val()},doUpdate); } function doUpdate(response) { if (response) { $("#result").val(response); } } </script> <body bgcolor="#ffff66"> <form id="form1" > <h2>Using AJAX to Add Numbers with JQUERY</h2> <table> <tr><td> First Number : </td> <td><input type=text id="num1"></td></tr> <tr><td> Second Number :</td><td> <input type=text id="num2" ></td></tr> <tr><td> Result:</td><td> <input type=text readonly id="result" ></td></tr> </table> <br /> <input type=button ID="Button1" runat="server" onclick="addNumbers()" value="Add Numbers" /> </form> </body> </html>
<%@ page import="java.sql.*" contentType="text/xml"%> <% String empid = request.getParameter("empid"); // connect to oracle using thin driver Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:XE","hr","hr"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select first_name, salary from employees where employee_id = " + empid); if ( rs.next()) { // found out.println("<employee><name>"); out.println(rs.getString(1)); out.println("</name><salary>"); out.println( rs.getInt(2)); out.println("</salary></employee>"); } else { out.println("<error>Employee ID Not Found </error>"); } rs.close(); st.close(); con.close(); %>
<html> <head> <title>AJAX and XML with JQUERY </title> <script language="javascript" src="jquery-1.2.6.js"></script> <script language="javascript"> function getEmployeeDetails(){ // make clock image visible $("#clock").css("visibility","visible") $.get( "empdetails.jsp",{empid : $("#empid").val()}, displayResult); } function displayResult(data) { // hide clock image $("#clock").css("visibility","hidden") if ( $("error",data).text() != "" ) { // clear fields $("#empname").val("") $("#empsal").val("") alert( "Error Message : " + $(data).find("error").text()); } else { empname = $("name",data).text(); empsal = $("salary",data).text(); $("#empname").val( empname ); $("#empsal").val( empsal ) } } </script> </head> <body> <form id="form1"> <h2>Employee Details</h2> <table> <tr> <td>Employee ID : </td> <td><input type="text" id="empid" size="10"/> <input type="button" value="Get Details" onclick="getEmployeeDetails()" /> </td> </tr> <tr> <td>Employe Name : </td> <td><input type="text" id="empname" readonly size="30"/></td> </tr> <tr> <td>Salary : </td> <td><input type="text" id="empsal" readonly size="30"/></td> </tr> </table> <p/> <img id="clock" src="clock.gif" style="visibility:hidden" /> </form> </body> </html>
<%@ page import="java.sql.*" contentType="text/json"%> <% String empid = request.getParameter("empid"); // connect to oracle using thin driver Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:XE","hr","hr"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select first_name, salary from employees where employee_id = " + empid); if (rs.next()) { // if found, send JSON string with employee details to client out.println("{\"first_name\":"); out.println("\"" + rs.getString(1) + "\""); out.println(",\"salary\":"); out.println(rs.getInt(2)); out.println("}"); } else { // if not found, send JSON string with error message out.println("{\"error\":\"Employee id not found!\"}"); } rs.close(); st.close(); con.close(); %>
<html> <head> <title>AJAX and JSON with JQUERY </title> <script language="javascript" src="jquery-1.2.6.js"></script> <script language="javascript"> function getEmployeeDetails(){ $.getJSON( "empdetails_json.jsp",{empid : $("#empid").val()}, displayResult); } function displayResult(data) { if ( data.error) // emp not found { $("#empname").val("") // clear fields $("#empsal").val("") alert( data.error); } else // Found employee. Display details { $("#empname").val( data.first_name); $("#empsal").val( data.salary); } } </script> </head> <body> <form id="form1"> <h2>Employee Details</h2> <table> <tr> <td>Employee ID : </td> <td><input type="text" id="empid" size="10"/> <input type="button" value="Get Details" onclick="getEmployeeDetails()" /> </td> </tr> <tr> <td>Employe Name : </td> <td><input type="text" id="empname" readonly size="30"/></td> </tr> <tr> <td>Salary : </td> <td><input type="text" id="empsal" readonly size="30"/></td> </tr> </table> </form> </body> </html>