Home Tutorial Servlet Java Servlet : Hidden Field

 
 

Share on Google+Share on Google+
Java Servlet : Hidden Field
Posted on: October 8, 2012 at 12:00 AM
Advertisement
In this tutorial, we will discuss about Hidden Field. It is one of session tracking technique.

Java Servlet : Hidden Field

In this tutorial, we will discuss about Hidden Field. It is one of session tracking technique.

Hidden Field :

Session tracking is a technique to maintain about the requests of a same user for a specific time period. You can maintain session tracking in three ways - Hidden form field, Cookies, URL rewriting.

Hidden form field is a way of session tracking so that you can save the information in client browser itself. For that you can use hidden field for securing fields.
So at the time of page display, no one can see these fields. When client submit the form, the browser also transfer these hidden value to the server with other fields.

You can secure your field as -

<input type =?hidden? name =?name? value=?value?/>

In servlet you can get hidden form field as -

String name= request.getParameter(?name?);

Example :  In this example, we are mentioning two form field hidden which are name and location in HiddenFieldPage.jsp and accessing these field in to our servlet HiddenFieldServlet.java.

HiddenFieldPage.jsp -

<html>
<head>

<title>Hidden Field Example</title>
</head>
<body>
<center>
<h2>Click on Submit to check hidden field </h2>
<form action="HiddenFieldServlet" method="post"><input
type="hidden" name="name" value="Roseindia"> <input
type="hidden" name="location" value="New Delhi"> <input
type="submit" value="Submit"></form>
</center>
</body>
</html>

HiddenFieldServlet.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 HiddenFieldServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<center>");

String name = request.getParameter("name");
String location = request.getParameter("location");

out.println("<form action=HiddenFieldPage.jsp>");
out.println("<b>Company Name : </b>" + name);
out.println("<br><b>Locaion : </b>" + location);
out.println("<br><br><b>Click on GoBack to go on JSP page </b>");
out.println("<br><input type=submit value='GoBack'>");

out.println("</form>");
out.println("</center>");
out.println("</body>");

}

}

web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SessionTracking</display-name>

<servlet>
<description></description>
<display-name>HiddenFieldServlet</display-name>
<servlet-name>HiddenFieldServlet</servlet-name>
<servlet-class>HiddenFieldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HiddenFieldServlet</servlet-name>
<url-pattern>/HiddenFieldServlet</url-pattern>
</servlet-mapping>
</web-app>

Output :



Advertisement

Related Tags for Java Servlet : Hidden Field :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com