Sunday 12 August 2012

How To Execute Javascript in .jsp Page

How To Execute Javascript in .jsp Page

Steps :-

  1. You can execute javascript by writing it internally in .jsp page, code is as follow....
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5.     <head>
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7.         <title>JSP Page</title>
  8.         <script>    // Starting tag of java script should be declared in head portion of .jsp page..
  9. function fun()
  10. {
  11.     if(document.f1.t1.value=="")
  12.     {
  13.       alert("PLEASE ENTER NAME");
  14.    
  15.     }
  16.     else if(document.f1.t2.value=="")
  17.     {
  18.       alert("PLZ FILL PASSWORD FIELD ");
  19.       
  20.     }
  21.     else
  22.         {
  23.             alert("yuppy Suusesfully done ");
  24.             
  25.         }
  26.         
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <form name="f1" method="get" action="" onSubmit="return fun()">  // onSubmit attribute of form is compulsorily writtern .....with value (return keyword & name of function )
  32.             <table border="1" bgcolor="silver" ALIGN="CENTER">
  33.             <tr>
  34.                 <td>NAME:</td>
  35.                 <td><input type="text" name="t1"/></td>
  36.                 
  37.             </tr>
  38.             <tr>
  39.                 <td>PASSWORD:</td>
  40.                 <td><input type="password" name="t2"/></td>
  41.                 
  42.             </tr>
  43.             <tr>
  44.                 
  45.                 <td  align="center"><input type="submit" name="btnsub" value="Submt"/></td>
  46.                 <td><input type="reset" value="Reset">
  47.                 
  48.             </tr>
  49.             
  50.             </table>
  51.         </form>
  52.     </body>
</html>
                                                                                                        Note: for executing the .jsp code you must have to set the server .....and default browser in netbeans 7.1.2 IDE thnx ......