Validation on TextField using JavaScript and Print command using servlet on NetBeans

Here in this Tutorial we are using JavaScript with Regular Expreesion to perform validation on TextField and Invoking Print a page command on Servlet using netbeans.

Project View on Project Explorer on Completion of This Tutorial
index.jsp
In index.jsp page in form tag printing is url pattern of printing.java page which we are passing in Action property which redirects index.jsp to printing.java
 <%@page contentType="text/html" pageEncoding="UTF-8"%>  
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
   "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
   <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     <title>Registration Page</title>  
     <script type="text/javascript">  
       f1=1,f2=1;  
       function checkfname()  
       {  
         var fname=document.getElementById("fname").value;  
         var element=document.getElementById("label1");  
          if(fname=="")  
           {element.innerHTML="First Name Required!";  
           element.style.color="red";  
           f1=1;  
         }  
         else if(fname.length<4||fname.length>10)  
           { element.innerHTML="First Name must be 4-10 characters long";  
           element.style.color="red";  
           f1=1;  
         }  
         else  
           {  
             if(fname.search(/^([A-Za-z]){1,}$/)==-1)  
             {  element.innerHTML="Not a valid First Name";  
               element.style.color="red";  
               f1=1;  
              }  
           else {  
             element.innerHTML="Correct";  
             element.style.color="green";  
             f1=0;  
             }  
           }  
       }  
       function checklname()  
       {  
         var lname=document.getElementById("lname").value;  
         var element=document.getElementById("label2");  
          if(lname=="")  
           {element.innerHTML="Last Name Required!";  
           element.style.color="red";  
           f2=1;  
         }  
         else if(lname.length<4||lname.length>10)  
           { element.innerHTML="Last Name must be 4-10 characters long";  
           element.style.color="red";  
           f2=1;  
         }  
         else  
           {  
             if(lname.search(/^([A-Za-z]){1,}$/)==-1)  
             {  element.innerHTML="Not a valid Last Name";  
               element.style.color="red";  
               f2=1;  
              }  
           else {  
             element.innerHTML="Correct";  
             element.style.color="green";  
             f2=0;  
             }  
           }  
       }  
       function validate()  
       {  
         if(f1==1||f2==1)  
           {  
             alert("Entry not valid!!");  
             return false;  
           }  
           else  
             {  
               return true;  
             }  
       }  
     </script>  
   </head>  
   <body>  
     <h1>Registration</h1>  
     <form action="printing">  
     <table><tr><td>First Name:<input type="text" id="fname" name="t1" onblur="checkfname();"></td><td><label id="label1"/></td></tr>  
     <tr><td>Last Name:<input type="text" id="lname" name="t2" onblur="checklname();"></td><td><label id="label2"/></td></tr>  
     <tr><td><input type="submit" value="submit" onclick="return validate();" ></td></tr></table>  
     </form>  
   </body>  
 </html>  

printing.java
Here we are using window.print() command on print button click to print output page.
 import java.io.*;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.*;  
 public class printing extends HttpServlet {  
   protected void doGet(HttpServletRequest request, HttpServletResponse response)  
   throws ServletException, IOException {  
     response.setContentType("text/html;charset=UTF-8");  
     PrintWriter out = response.getWriter();  
     String s1=request.getParameter("t1");  
     String s2=request.getParameter("t2");  
     try {  
       out.println("First Name:"+s1+"<br>");  
       out.println("Last Name:"+s2+"<br>");  
       out.println("<input type=submit value=print onclick=window.print()>");  
     } finally {   
       out.close();  
     }  
   }   
  }  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
   <servlet>  
     <servlet-name>printing</servlet-name>  
     <servlet-class>printing</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>printing</servlet-name>  
     <url-pattern>/printing</url-pattern>  
   </servlet-mapping>  
   <session-config>  
     <session-timeout>  
       30  
     </session-timeout>  
   </session-config>  
   <welcome-file-list>  
     <welcome-file>index.jsp</welcome-file>  
   </welcome-file-list>  
 </web-app>  

Download Code Link 1
Download Code Link 2

output:








More Servlet Topics :

No comments: