SESSION Tutorials in Java Web Application,What is Session,Session example in JSP

0

SESSION in Java Web Application:-  It is used to provide server-side state management option using Session object we can store page content( data) after response means session provides a persistent object which can retain data in browser cookie or database then we can use it into different web pages.
The session is mandatory to create login forms in a web application because session will provide dynamic objects for multiple users to contain information.
for example if five different users login to the system then the session object provides a separate identity to all five users.
the session always will be created after login and destroy after logout. without a session, we can not handle login and logout operation.
Syntax to create in Servlet:-
HttpSession ref = request.getSession();
ref.setAttribute("key","value");
--------------------------------------------------------------------------------------------------
Syntax to destroy session
HttpSession ref = request.getSession();
ref.removeAttribute("key");
ref.invalidate();
Syntax to create session in JSP:-
session.setAttribute("key","value");
Syntax to remove session in JSP
ref.removeAttribute("key");
ref.invalidate();
The session is used to provide security if we want to restrict users to access the internal page without login then using the session object we can proceed to user's in the login page.we should always check session data that it is set or not.
<%
    if(session.getAttribute("uid")==null){
     response.sendRedirect("login.jsp");
    }
 %> 

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)