Hidden field:
Hidden field is an input text with hidden type. This field will not be visible to the user.
Syntax:
input name="fieldName" value="fieldValue" type="hidden"
How to get hidden field value in servlet?
HttpServletRequest interface’s getParameter() method is used to get hidden field value in servlet.
Syntax:
String value = request.getParameter(“fieldName”);
Note: Hidden field only works in case of form submission so they will not work in case of anchor tag as no form submission is there.
Advantages of hidden field:
1. All browsers support hidden fields.
2. Simple to use.
Disadvantages of hidden fields:
1. Not secure.
2. Only work in case of form submission.
Session management example using hidden field:
SetHiddenFieldServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This class is used to set values in hidden fields. * @author w3spoint */ public class SetHiddenFieldServlet extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor. public SetHiddenFieldServlet() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //get parameters from request object. String userName = request.getParameter("userName").trim(); String password = request.getParameter("password").trim(); //check for null and empty values. if(userName == null || userName.equals("") || password == null || password.equals("")){ out.print("Please enter both username " + "and password. <br/><br/>"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html"); requestDispatcher.include(request, response); }//Check for valid username and password. else if(userName.equals("jai") && password.equals("1234")){ out.println("Logged in successfully.<br/>"); out.println("Click on the below button to see " + "the values of Username and Password.<br/>"); out.print("<form action='GetHiddenFieldServlet'" + " method='POST'>"); out.print("<input type='hidden' name='userName'" + " value='" + userName + "'>"); out.print("<input type='hidden' name='password'" + " value='" + password + "'>"); out.print("<input type='submit' value='See Values'>"); out.print("</form>"); out.close(); }else{ out.print("Wrong username or password. <br/><br/>"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html"); requestDispatcher.include(request, response); } } } |
GetHiddenFieldServlet.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; /** * This class is used to get hidden fields values. * @author w3spoint */ public class GetHiddenFieldServlet extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor. public GetHiddenFieldServlet() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //get parameters from request object. String userName = request.getParameter("userName").trim(); String password = request.getParameter("password").trim(); out.println("Username: " + userName + "<br/><br/>"); out.println("Password: " + password); out.close(); } } |
login.html
<!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=ISO-8859-1"> <title>Login</title> </head> <body> <form action="SetHiddenFieldServlet" method="post"> Username:<input type="text" name="userName"/> <br/><br/> Password:<input type="password" name="password"/> <br/><br/> <input type="submit" value="login"/> </form> </body> </html> |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>SetHiddenFieldServlet</servlet-name> <servlet-class> com.w3spoint.business.SetHiddenFieldServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>SetHiddenFieldServlet</servlet-name> <url-pattern>/SetHiddenFieldServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>GetHiddenFieldServlet</servlet-name> <servlet-class> com.w3spoint.business.GetHiddenFieldServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>GetHiddenFieldServlet</servlet-name> <url-pattern>/GetHiddenFieldServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app> |
Output:
Enter username: jai and password: 1234
Click on login button.
Click on See Values.
Download this example.
Next Topic: URL rewriting in servlet with example.
Previous Topic: Cookie in servlet with example.