Skip to main content

Session tracking using Hidden form fields in servlet


Session tracking using Hidden form fields in servlet

 Http Stateless behaviour

========================

The protocol http creates seperate connection with the web server for browser for every request

due to this one connection data can not be used in another connection i.e one request data

can not be used in another request processing.


in order to get stateful behaviour in web application though protocol http is stateless

we need to use following session tracking techniques


a)Hidden form fields


advantages

==========

1)Basic html knowledge

2)can be used with diff servers


2)Disadvantages

a)we can see the data (no secrecy)


b)hidden box values travel across the multiple requests i.e they increase network traffic 

b/w browser and server


c)Hidden boxes can only hold the text data/String values.. i.e they can not hold the java objects


d)if multiple forms are there then adding hidden box values is quite heavy job

Directory

=======




web.xml

========

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">

  <display-name>HiiddenFormFields</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

  </welcome-file-list>

</web-app>


index.html

========


<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

<style type="text/css">

div {

width: 500px;

margin: auto;

margin-top: 100px;

}

</style>

</head>

<body>

<div>

<form action="firsturl" method="post">

Name: <br> <input type="text" name="name"> <br>

Choose any one: <br> <select name="dish">

<option value="sweet">Sweet</option>

<option value="spicy">Spicy</option>

</select> <br> <input type="submit" value="submit">

</form>

</div>

</body>

</html>


FirstServlet.java

===========

import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


@WebServlet("/firsturl")

public class FirstServlet extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

//get PrintWriter

PrintWriter pw = res.getWriter();

//set content type

res.setContentType("text/html");

//get the values

String name = req.getParameter("name");

String dish = req.getParameter("dish");

if(dish.equalsIgnoreCase("sweet")) {

pw.println("<h1>Choose One</h1>");

pw.println("<form action='secondurl' method='post'>");

pw.println("<input type='hidden' name='name' value='"+name+"'>");

pw.println("<select name='dish'>");

pw.println("<option value='gulab jamun'>gulab jamun</option>");

pw.println("<option value='jalebi'>jalebi</option>");

pw.println("</select>");

pw.println("<br>");

pw.println("<input type='submit' value='submit'>");

pw.println("</form>");

}else {

pw.println("<h1>Choose One</h1>");

pw.println("<form action='secondurl' method='post'>");

pw.println("<input type='hidden' name='name' value='"+name+"'>");

pw.println("<select name='dish'>");

pw.println("<option value='samosa'>samosa</option>");

pw.println("<option value='paneer'>paneer</option>");

pw.println("</select>");

pw.println("<br>");

pw.println("<input type='submit' value='submit'>");

pw.println("</form>");

}

//close the stream

pw.close();

}

}

SecondServlet.java
===============
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/secondurl")
public class SecondServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//get the values
String name = req.getParameter("name");
String dish = req.getParameter("dish");
pw.println("<h2>Thanks you "+name+" your "+dish+" will be placed soon</h2>");
//close the Stream
pw.close();
}
}


Comments

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }