Session Tracking using Hidden Forms Field

Project View :

Hidden fields are not visible to user on browser so we can hide data that we invoke from previous page using getParameter() method. Hidden Form Fields used in a proper sequence.

Hidden Form Field Example:

In index.jsp page on submit button action is page1 and method is post which redirects index.jsp to page1.java's doPost() method. 

In page1.java using hidden form field we hide previous page data and on submit button click redirect page1.java to page2.java and print all data on page2.java

index.jsp
 <html>  
   <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     </head>  
   <body>  
     <h1>Register Yourself</h1>  
     <form action="page1" method="post">  
     UserName:<input type="text" name="t1">  
     Password:<input type="password" name="t2"><br>  
     <input type="submit" value="Next">  
     </form>  
   </body>  
 </html>  

page1.java
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 public class page1 extends HttpServlet {  
   protected void doPost(HttpServletRequest request, HttpServletResponse response)  
   throws ServletException, IOException {  
     PrintWriter out=response.getWriter();  
     String s1=request.getParameter("t1");  
     String s2=request.getParameter("t2");  
     out.println("<form action=page2 method=post>");  
     out.println("<input type=hidden name=t1 value='"+s1+"'>");  
     out.println("<input type=hidden name=t2 value='"+s2+"'>");  
     out.println("FirstName:<input type=text name=t3 >");  
     out.println("LastName:<input type=text name=t4 ><br>");  
     out.println("<input type=submit value=Finish>");  
     out.println("</form>");  
   }   
 }  

page2.java
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 public class page2 extends HttpServlet {  
   protected void doPost(HttpServletRequest request, HttpServletResponse response)  
   throws ServletException, IOException {  
     PrintWriter out=response.getWriter();  
     String s1=request.getParameter("t1");  
     String s2=request.getParameter("t2");  
     String s3=request.getParameter("t3");  
     String s4=request.getParameter("t4");  
     out.print("<h1>Registration</h1>");  
     out.print("<h4>UserName:"+s1+"</h4>");  
     out.print("<h4>Password:"+s2+"</h4>");  
     out.print("<h4>FirstName:"+s3+"</h4>");  
     out.print("<h4>LastName:"+s4+"</h4>");  
   }  
 }  


Download Code Link 1
Download Code Link 2

Output:






More Servlet Topics :

No comments: