1:37 pm - Saturday March 25, 2017

Hidden form fields in Servlet

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Hidden Form fields: 

Hidden form is a form of session tracking which saves the information in client browser itself.

We uses hidden box in html to hide the session information from client.

We cannot use this type of session for secure data because  when a client views the source code of the html page the session information can be view by all.

<input type =”hidden” name =”session” value=”sessionid”/>/

In servlet to get session information:

 String value = request.getParameter(“session”);

One way to support anonymous session tracking is to use hidden form fields. As the name implies, these are fields added to an HTML form that are not displayed in the client’s browser. They are sent back to the server when the form that contains them is submitted.

In case of hidden form field an invisible text field is used for maintaining state of an user.

  Project Structure:

 

 

 

 

 

 

 

 

 

 

 

Hidden.java:


package com.candid;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Hidden
*/
public class Hidden extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public Hidden() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<body bgcolor=pink>");
out.println("<font color=blue size=6>");
String str1 = req.getParameter("loginId");
String str2 = req.getParameter("company");
out.println("<form action=Hidden>");
out.println("your login name is " + str1);
out.println("<br>your Company is " + str2);
out
.println("<p>The value of Login name is stored in the Hidden box</p> ");
out.println("<input type=hidden name=loginId value=" + str1 + ">");
out.println("<input type=submit value=press>");
out.println("</form>");
out.println("</body>");

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/

}

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">
<display-name>Hidden</display-name>
<servlet>
<description>
</description>
<display-name>Hidden</display-name>
<servlet-name>Hidden</servlet-name>
<servlet-class>com.candid.Hidden</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hidden</servlet-name>
<url-pattern>/Hidden</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

Hidden.jsp:


<!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>Insert title here</title>
</head>
<body bgcolor="pink">
<form action="Hidden" method="get"><font color="blue" size="6">
This method is to Explain the Use of Hidden </br>
</br>
Enter your Login Name <input type=text name=loginId></br>
Enter your Company name <input type=text name=company></br>
<br>
<input type=submit value=click> </font></form>
</body>
</html>

OUTPUT:

DOWNLOAD+SOURCE CODE                                                                                                                                                   BACK

Filed in: Z-A Servlet codes

No comments yet.

Leave a Reply