Advanced Servlet

Click to rate this post!
[Total: 0 Average: 0]

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
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button