Session and Cookie Management in IKS Learning Portals using JSP
In modern Indian Knowledge System (IKS) learning portals, it is very useful to remember a learner’s preferences such as their preferred Vedic topic (Yoga, Ayurveda, Vedanta) or language setting (Sanskrit, Hindi, English). This is where Session and Cookie Management in JSP becomes important.
🌿 What are Sessions and Cookies?
- Session: Stores user information on the server, valid until logout or timeout.
- Cookie: Small text file stored on the browser, used to remember preferences across visits.
📌 Example: Storing and Retrieving Preferences
setPreference.jsp
<%@ page import="javax.servlet.http.Cookie" %> <% String topic = request.getParameter("topic"); String language = request.getParameter("language"); session.setAttribute("preferredTopic", topic); Cookie langCookie = new Cookie("preferredLanguage", language); langCookie.setMaxAge(7 * 24 * 60 * 60); response.addCookie(langCookie); out.println("<h2>Preferences Saved!</h2>"); out.println("Topic: " + topic + "<br>"); out.println("Language: " + language + "<br>"); %>
getPreference.jsp
<%@ page import="javax.servlet.http.Cookie" %> <% String topic = (String) session.getAttribute("preferredTopic"); String language = "Not Set"; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie c : cookies) { if (c.getName().equals("preferredLanguage")) { language = c.getValue(); } } } out.println("<h2>Welcome Back!</h2>"); out.println("Your preferred Vedic topic: " + topic + "<br>"); out.println("Your preferred language: " + language + "<br>"); %>
✅ Conclusion
Using Sessions and Cookies in JSP, IKS websites can provide a personalized learning experience for students. This helps display content like Sanskrit Shlokas, Ayurveda tips, or Vedanta teachings based on the learner’s preferences.
<%@ page import="javax.servlet.http.*" %> <%@ page import="javax.servlet.*" %> <% String topic = request.getParameter("topic"); String language = request.getParameter("language"); // Save in session session.setAttribute("topic", topic); session.setAttribute("language", language); // Save in cookie (for 7 days) Cookie c1 = new Cookie("topic", topic); Cookie c2 = new Cookie("language", language); c1.setMaxAge(60*60*24*7); c2.setMaxAge(60*60*24*7); response.addCookie(c1); response.addCookie(c2); out.println("