Java Topics
HubJava Collections FrameworkCollections Practice ProgramsComparable vs ComparatorGenericsIteratorArrayListLinkedListStackVectorHashMapHashtableLinkedHashMapTreeMapArrayDequeDeque Double-Ended QueuePriorityQueueHashSetLinkedHashSetTreeSetAdapter PatternBuilder PatternFactory PatternMVC PatternObserver PatternSingleton PatternDynamic ProgrammingGraphHashingHeapLinked List DSAQueue DSARecursionSearching AlgorithmsSorting AlgorithmsStack DSATime ComplexityTreesException Handling Best PracticesCustom ExceptionException Hierarchyfinally BlockException Handlingthrow aur throwstry-catchBuffered StreamsByte StreamsCharacter StreamsDeserializationFile ClassJava NIO New I/OSerializationArrays in JavaArray OperationsArray Practice ProgramsArray Interview QuestionsJagged ArrayMulti-Dimensional ArraysOne Dimensional Arraybreak aur continuedo-while Loopfor Loopif-else StatementsNested Loopsswitch-case Statementwhile LoopJava Compilation ProcessJava ki FeaturesPehla Java ProgramHistory of JavaJava EditionsJava Program StructureJDK, JRE aur JVMJava Kya Hai?Immutable StringsString Class in JavaString Interview QuestionsString MethodsString Practice ProgramsStringBufferStringBuilderComments in JavaData Types in JavaIdentifiers in JavaInput & Output in JavaJava KeywordsOperators in JavaType Casting in JavaVariables in JavaJava Coding Interview QuestionsCollections Interview QuestionsCore Java Interview QuestionsJDBC Interview QuestionsMultithreading Interview QuestionsOOPs Interview QuestionsSpring Framework Interview QuestionsJava 8 Date/Time APIDefault aur Static Methods in InterfaceFunctional InterfaceLambda ExpressionMethod ReferenceOptional ClassStream APIBatch ProcessingCallableStatementJDBC ArchitectureJDBC — Java Database ConnectivityJDBC Practice ProjectsMySQL ConnectionPreparedStatementResultSetStatement InterfaceTransaction ManagementCreating ThreadsDaemon ThreadExecutor FrameworkInter-Thread CommunicationMultithreading Practice ProgramsRunnable InterfaceSynchronizationThread ClassMultithreading IntroductionThread Life CycleThread PriorityJava Cheat SheetImportant Formulas & Key ConceptsImportant Java ProgramsJava Quick RevisionAbstract ClassAbstractionAnonymous ClassClass and ObjectConstructorEncapsulationInheritanceInner ClassInterfaceMethod OverloadingMethod OverridingObject ClassObject CloningObject-Oriented Programming OOPPolymorphismstatic Keywordsuper Keywordthis KeywordWrapper ClassesArray Practice ProgramsBasic Java Practice ProgramsCollection Practice ProgramsJDBC Practice ProgramsMultithreading Practice ProgramsOOPs Practice ProgramsPlacement Coding QuestionsString Practice ProgramsBanking SystemChat ApplicationE-Commerce Backend — Spring Boot REST APIEmployee Management SystemLibrary Management SystemSpring Boot Fullstack ProjectStudent Management SystemCookies in ServletExpression Language ELGenericServletHttpServletJavaServer Pages JSPJSP TagsMVC ArchitectureHttpServletRequest & HttpServletResponseServlet IntroductionServlet Life CycleSession TrackingDependency InjectionSpring BeansSpring CoreSpring FrameworkAPI GatewaySpring Cloud Config ServerDocker DeploymentEureka Server — Service DiscoverySpring Boot CRUD ApplicationException Handling in Spring BootJWT AuthenticationREST API in Spring BootSpring SecuritySpring BootSpring Data JPAValidation in Spring Boot
Session Tracking
Last Updated : 26 May, 2026
title: Session Tracking in Servlet
Servlets JSP41 words5 headingsExamples included
title: Session Tracking in Servlet description: User session maintain karne ke tarike
HTTP stateless hai — session tracking se user state maintain karte hain.
4 Techniques
1. HttpSession (Most Common)
java exampleWoHoTech
// Login servlet — session create
HttpSession session = request.getSession();
session.setAttribute("userId", userId);
session.setAttribute("username", username);
session.setMaxInactiveInterval(30 * 60); // 30 minutes
// Other servlets — session read
HttpSession session = request.getSession(false); // false = don't create new
if (session != null) {
String username = (String) session.getAttribute("username");
System.out.println("Logged in: " + username);
}
// Logout
session.invalidate(); // Session destroy2. Cookies
java exampleWoHoTech
// Set cookie
Cookie cookie = new Cookie("userId", "12345");
cookie.setMaxAge(60 * 60 * 24); // 1 day in seconds
cookie.setPath("/"); // Available for all paths
cookie.setHttpOnly(true); // JS access nahi
response.addCookie(cookie);
// Read cookies
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
if ("userId".equals(c.getName()))
System.out.println("User: " + c.getValue());
}
}
// Delete cookie
Cookie toDelete = new Cookie("userId", "");
toDelete.setMaxAge(0);
response.addCookie(toDelete);3. URL Rewriting
java exampleWoHoTech
String url = response.encodeURL("/profile?userId=123");
out.println("<a href='" + url + "'>Profile</a>");
// Session ID URL mein add ho jaata hai jab cookies disabled ho4. Hidden Form Fields
<form action="/submit" method="post">
<input type="hidden" name="userId" value="123">
<!-- Visible fields -->
</form>
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Session Tracking.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, servlets
Related Java Topics
Continue learning this concept
Servlets JSPCookies in Servlettitle: Cookies in ServletServlets JSPExpression Language ELtitle: Expression Language EL in JSPServlets JSPGenericServlettitle: GenericServlet in JavaServlets JSPHttpServlettitle: HttpServlet in JavaServlets JSPJavaServer Pages JSPtitle: JSP IntroductionServlets JSPJSP Tagstitle: JSP Tags