session – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 05 Oct 2020 16:43:36 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg session – Programmerbay https://programmerbay.com 32 32 Difference between Cookies and Session in Tabular form https://programmerbay.com/difference-between-cookies-and-sessions/ https://programmerbay.com/difference-between-cookies-and-sessions/#respond Mon, 09 Mar 2020 19:31:22 +0000 https://www.programmerbay.com/?p=2789 Cookies and Session are used to make a stateful communication between client’s system and server, or it can be between computers.

Difference between Cookies and Sessions

When a client sends a request to a server, an HTTP request is created with a header consisting of information about the user and requested page.

In a similar way, when the server provides the appropriate page back to the client, an HTTP response is generated with a header having information about the document being sent.

In an all, HTTP protocol is used for communicating on the web.

This HTTP protocol is stateless and to make it stateful, we use Cookies and Session.

Difference between Cookies and Session in Tabular form

CookiesSession
It is a small piece of information which is sent by the server on the client's machine to identify a user uniquelyIt is a set of data that is stored on a server
It expires after a specific length of timeIt expires as and when the browser is closed
It is not secured as one can read values and may alter them directly by accessing and reading it from browserIt is more secure as it is managed and stored at server-side
The size of the cookie is limited to 4 kbThe storage capacity of Session is comparatively more than cookies
A user can disable cookiesA user can't disable session

Session

A session is used to provide stateful communication between client and server. It maintains the state by storing the information at the server side.

It assigns a unique session ID to every user at first visit on the website and the same session id is saved at the client’s machine in the cookies.

Cookie

At the client level, Cookies are the small piece of information which is sent by the server to identify the user and its activities uniquely.

In this method, the information is stored locally on the user’s browser and later on, this information is then being sent to the server whenever the information is needed ( when the user again visits that site).

How do cookies and session work?

When a client’s computer requests a webpage from a server ( a Website), in return, the server answers back with the requested webpage. This is how client/server approach works.

However, it is crucial to keep information about users in order to monitor the behavior carried out by them on various webpages. This can be done by maintaining state.

As it helps in the further development of complex interactive web-based applications.

If a server isn’t capable of maintaining the state then it would forget all the information about a visitor and the page on which he landed on, as and when it responded.

To overcome this problem, this type of information is stored on the server itself or the user’s browser locally. One of the way is cookie and session.

 

]]>
https://programmerbay.com/difference-between-cookies-and-sessions/feed/ 0
Create an application to show how to call a servlet from another servlet using sendRedirect method in apache tomcat server https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/#respond Sat, 07 Dec 2019 15:20:38 +0000 https://programmerbay.com/?p=5664 There is another way to call a servlet from another servlet using sendRedirect method.
1) Create Index.html file by clicking on ‘new’ tab and select HTML.

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="first_servlet">
WRITE <input type="text" name="word" value="I am calling first servlet"/>
<button type="submit" name="calculate">Call First Servlet </button><br/>
</form>
</body>
</html>

2) Create a servlet that will redirect to another servlet. In order to send data, one requires to maintain a session.

First_Servlet.java

package com.programmerbay;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class First_Servlet extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
  {
  String word = request.getParameter("word")	;
  PrintWriter output = response.getWriter();
  
String word1 = word+".\n Now, It is calling second servlet";
  // redirecting it to second servlet
HttpSession session = request.getSession();
session.setAttribute("word", word1);
  response.sendRedirect("second_servlet");
  }

}

3) Write another servlet that will handle the data sent from the first servlet and print the message.

Second_Servlet.java

package com.programmerbay;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Second_Servlet extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
  {
    HttpSession session = request.getSession();
    String word = (String) session.getAttribute("word");
    PrintWriter output = response.getWriter();
    output.println(word+" \n usiing session");
    
  }
}

4) Modify the Web.xml file to map servlets.

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_3_1.xsd" id="WebApp_ID" version="3.1">
  


 <servlet>
 <servlet-name>Invoke_First</servlet-name>
 <servlet-class>com.programmerbay.First_Servlet</servlet-class>
 
 </servlet>
 
 <servlet-mapping>
 
 <servlet-name>Invoke_First</servlet-name>
 <url-pattern>/first_servlet</url-pattern>
 
 </servlet-mapping>
 
 <servlet>
 <servlet-name>Invoke_Second</servlet-name>
 <servlet-class>com.programmerbay.Second_Servlet</servlet-class>
 
 </servlet>
 
 <servlet-mapping>
 
 <servlet-name>Invoke_Second</servlet-name>
 <url-pattern>/second_servlet</url-pattern>
 
 </servlet-mapping>
 
 
</web-app>

Output:

call one servlet from another

]]>
https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/feed/ 0