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.

Http convention is a stateless so we have to keep up state utilizing session following methods. Each time client solicitations to the server, server regards the demand as the new demand. So we have to keep up the condition of a client to perceive to specific client.Session TrackingSession Tracking Techniques:

  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.

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.URL Rewriting

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>

 

 

 

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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