Advanced Servlet

Session Tracking in Servlets :

Session essentially implies a specific interim of time.Session Tracking is an approach to look after state (information) of a client. It is otherwise called session administration in servlet.

  1. Cookies
  2. Hidden Form Field
  3. URL Rewriting
  4. HttpSession

Cookies in Servlet:

A treat is a little snippet of data that is endured between the different customer demands. A treat has a name, a solitary esteem, and discretionary characteristics, for example, a remark, way and space qualifiers, a greatest age, and a rendition number.

Advanced Servlet

Types of Cookie

  1. Non-persistent cookie
  2. Persistent cookie

Non-persistent cookie:’

It is legitimate for single session as it were. It is evacuated each time when client shuts the program.

Persistent Cookie:

It is legitimate for various session . It is not expelled each time when client shuts the program. It is evacuated just if client logout or signout.

Advantages:

Least complex method of keeping up the state.

Treats are kept up at customer side.

Disadvantage of Cookies :

It won’t work if treat is handicapped from the program.

Just literary data can be set in Cookie protest.

Hidden Form Field:

In the event of Hidden Form Field a covered up (undetectable) textfield is utilized for keeping up the condition of a client.

In such case, we store the data in the concealed field and get it from another servlet. This approach is better on the off chance that we need to submit frame in every one of the pages and we would prefer not to rely upon the program.

How about we see the code to store an incentive in concealed field.

<input type=”hidden” name=”uname” value=”Varun”>

 Advantages:

It will dependably work whether treat is debilitated or not.

Hindrance of Hidden Form Field:

  • It is kept up at server side.
  • Additional shape accommodation is required on each pages.
  • Just printed data can be utilized.

3)URL Rewriting :

In URL revamping, we add a token or identifier to the URL of the following Servlet or the following asset. We can send parameter name/esteem sets utilizing the accompanying organization.

Flow chart

Advantages:

It will dependably work whether treat is impaired or not (program autonomous).

Additional frame accommodation is not required on each pages.

Inconvenience of URL Rewriting

It will work just with joins.

It can send Only printed data.

 index.html

<form action=”servlet1″>

Name:<input type=”text” name=”userName”/><br/>

<input type=”submit” value=”go”/>

</form>

FirstServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

String n=request.getParameter(“userName”);

out.print(“Welcome “+n);

//appending the username in the query string

out.print(“<a href=’servlet2?uname=”+n+”‘>visit</a>”);

out.close();

}catch(Exception e){System.out.println(e);}

}

}

SecondServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SecondServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

try{

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

//getting value from the query string

String n=request.getParameter(“uname”);

out.print(“Hello “+n);

out.close();

}catch(Exception e){System.out.println(e);}

}

}

web.xml

<web-app>

<servlet>

<servlet-name>s1</servlet-name>

<servlet-class>FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s1</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>s2</servlet-name>

<servlet-class>SecondServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s2</servlet-name>

<url-pattern>/servlet2</url-pattern>

</servlet-mapping>

</web-app>

4) HttpSession interface:

In such case, compartment makes a session id for each user.The holder utilizes this id to recognize the specific user.An question of HttpSession can be utilized to perform two errands:

tie objects see and control data about a session, for example, the session identifier, creation time, and last got to time.

Commonly used methods of HttpSession interface

  1. public String getId()
  2. public long getCreationTime()
  3. public long getLastAccessedTime()
  4. public void invalidate()

index.html

<form action=”servlet1″>

Name:<input type=”text” name=”userName”/><br/>

<input type=”submit” value=”go”/>

</form>

FirstServlet.java :

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

String n=request.getParameter(“userName”);

ut.print(“Welcome “+n);

HttpSession session=request.getSession();

session.setAttribute(“uname”,n);

out.print(“<a href=’servlet2′>visit</a>”);

out.close();   }catch(Exception e){System.out.println(e);}

}

}

SecondServlet.java:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SecondServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

try{

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

HttpSession session=request.getSession(false);

String n=(String)session.getAttribute(“uname”);

out.print(“Hello “+n);

out.close();

}catch(Exception e){System.out.println(e);}

}

}

web.xml

<web-app>

<servlet>

servlet-name>s1</servlet-name>

<servlet-class>FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s1</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>s2</servlet-name>

<servlet-class>SecondServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s2</servlet-name>

<url-pattern>/servlet2</url-pattern>

</servlet-mapping>

</web-app>

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.