Share on Google+Share on Google+

Calculating square root using JSP & jQuery

Ads
In this tutorial, we will find square root of any number with the help of JSP & jQuery.

Calculating square root using JSP & jQuery

In this tutorial, we will find square root of any number with the help of JSP & jQuery. In this example, we have a JSP file which have jQuery 'javascript' :

In this video tutorail I am going to explains you to calculate the square root using jQuery. Sever side program will be called from a jsp page using jQuery and then server side program will return the calcuated value. The value returned from server will be displayed on the screen.

index.jsp

<html>
<head>
  <title>Simple jQuery and JSP example</title>
  <script src="jquery-1.4.2.js" type="text/javascript"></script>
  <script src="script.js" type="text/javascript"></script>
</head>
<body>
<form id="form" action="calculate.jsp" method="post">
  Enter number:
  <input id="number" type="text" name="number" />
  <input id="submit" type="submit" value="Calculate Square Root" 
 
name="submit"/>
</form>
<p id="result"></p>
</body>
</html>

Here is the video of “Calculating square root using JSP & jQuery?”:

script.js

$(document).ready(function() {
  $('#form').submit(function(){
  var number = $('#number').val(); 
  $.ajax({type:"post",url:"calculate.jsp",data:"number=" 
  +number,success:function(msg){$(
'#result').hide(); 
  $("#result").html("<h3>" + msg + "</h3>").fadeIn("slow"); } });
  return false;
});
});

calculate.jsp

<%page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int number = 0;
if(request.getParameter("number")==null){
  out.println("Enter a number!");
else {
  
  number = Integer.parseInt(request.getParameter("number"));
  out.println("Square root of "+ number + " is " 
 
+ Math.sqrt(number));
}/font>
%%>

OUTPUT :

Download Source Code

Advertisements

Ads

Posted on: July 26, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Advertisement