Wednesday, 1 October 2014

Hidden Form Field

As I explain in previous chapter that cookie has drawback that if browser has disabled cookie then cookie will not work. So, work around of previous approach is hidden form field.

In this approach we use hidden form field to store user states so that this user states can be circulated among servlets.

<input type="hidden" name="Language" value="English">

e.g
Login page where user will submit username and password and this username will be stored in hidden field of another page and it will be showed in top of home page.

1. Create  the login page-

<form action="loginaction"
    Name:<input type="text" name="userName"/><br/> 
    Password:<input type="password" name="password"/><br/> 
   <input type="submit" value="submit"/> 
 </form> 

2. Create the servlet which will handle login action-
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5. public class LoginServlet extends HttpServlet {  
  6. public void doGet(HttpServletRequest request, HttpServletResponse response){  
  7.         try{  
  8.         String userName= request.getParameter("userName");
  9.         String password = request.getParameter("password");  
  10.         if( userName!= null &&password != null && isAuthUser(userName,password )){
  11.             response.setContentType("text/html");  
  12.             PrintWriter out = response.getWriter();  
  13.             out.print("Welcome "+userName);  
  14.             out.print("<form action='homeServlet'>");  
  15.             out.print("<input type='hidden' name='username' value='"+userName+"'>");  
  16.             out.print("<input type='submit' value='home page'>");  
  17.             out.print("</form>");  
  18.             out.close(); 
  19.         }
  20.        }catch(Exception e){System.out.println(e);}  
  21.     }  
  22.   
3. Create the HomeServlet which will generate page by using hidden field value. 
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5. public class HomeServlet extends HttpServlet {  
  6. public void doGet(HttpServletRequest request, HttpServletResponse response){  
  7.         try{  
  8.         String userName= request.getParameter("username");      
  9.         if( userName!= null ){
  10.             response.setContentType("text/html");  
  11.             PrintWriter out = response.getWriter();  
  12.             out.print("User: "+userName);  
  13.            //other home page creation logic
  14.             out.close(); 
  15.         }
  16.        }catch(Exception e){System.out.println(e);}  
  17.     }  
  18.   
  19. }

4. Create the web.xml file and put servlet mapping. 

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>login-servlet</servlet-name>  
  5. <servlet-class>LoginServlet</servlet-class>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9. <servlet-name>login-servlet</servlet-name>  
  10. <url-pattern>/loginaction</url-pattern>  
  11. </servlet-mapping>  
  12.   
  13. <servlet>  
  14. <servlet-name>home-servlet</servlet-name>  
  15. <servlet-class>HomeServlet</servlet-class>  
  16. </servlet>  
  17.   
  18. <servlet-mapping>  
  19. <servlet-name>home-servlet</servlet-name>  
  20. <url-pattern>/homeServlet</url-pattern>  
  21. </servlet-mapping>  
  22.   
  23. </web-app> 

In above example you can see username which is entered in login page and validated in LoginServlet later it stored in hidden input box and this value is used in HomeServlet. So, you can see username is shared among two servlet.

No comments:

Post a Comment