Session and Application Scope in JSP: Sharing Data Across Pages
Session and Application Scope in JSP: Sharing Data Across Pages
जब हम JSP (Java Server Pages) में काम करते हैं, तो अक्सर हमें एक page से दूसरे page तक data share करने की ज़रूरत होती है। इस काम के लिए JSP हमें अलग-अलग scope provide करता है — जिनमें सबसे important हैं Session Scope और Application Scope। इन दोनों का use करके हम user-specific या application-level data को manage कर सकते हैं। आज हम इन्हीं दो concepts को simple और exam-oriented तरीके से समझेंगे।
What is Scope in JSP?
Scope का मतलब होता है कि कोई data या variable कितने समय तक और कहाँ तक available रहेगा। JSP में चार तरह के scopes होते हैं:
- Page Scope: Data सिर्फ current page तक ही रहता है।
- Request Scope: Data एक request तक valid रहता है (forward या include के दौरान)।
- Session Scope: Data पूरे user session तक valid रहता है।
- Application Scope: Data पूरे web application के लिए valid रहता है।
अब हम detail में Session Scope और Application Scope को समझते हैं।
Session Scope in JSP
Session Scope का मतलब होता है कि data एक particular user के लिए तब तक stored रहेगा जब तक उसका session active है। यानी, जब कोई user website पर आता है, तो server उसके लिए एक session create करता है और उसी session के अंदर हम data store कर सकते हैं जो सभी JSP pages में accessible होता है।
Session Scope Example
मान लो, कोई user login करता है, और हम उसका username कई pages पर दिखाना चाहते हैं। इस case में हम session object का use करेंगे।
<%
session.setAttribute("username", "Rohit");
%>
अब इस data को किसी भी JSP page पर हम इस तरह access कर सकते हैं:
<%
String user = (String) session.getAttribute("username");
out.print("Welcome, " + user);
%>
Session Scope के Important Points
- हर user के लिए separate session create होता है।
- Session data automatically expire हो जाता है जब session time-out होता है।
- Default session timeout 30 minutes होता है (web.xml में change किया जा सकता है)।
- Session का use sensitive data (जैसे login info) store करने के लिए किया जाता है।
Session Timeout Configuration
अगर आप session timeout को change करना चाहते हैं, तो web.xml में इस तरह define कर सकते हैं:
<session-config>
<session-timeout>20</session-timeout>
</session-config>
यहाँ session 20 minutes बाद expire हो जाएगा।
Session Scope का Use कब करें?
- जब आपको data user-specific रखना हो।
- जब data को multiple JSP pages में use करना हो।
- जैसे: login info, user preferences, shopping cart data, आदि।
Application Scope in JSP
Application Scope का use तब किया जाता है जब हमें ऐसा data store करना हो जो पूरे web application के लिए common हो। यानी, सभी users और सभी sessions उस data को access कर सकते हैं।
Application Scope का object होता है application (या servlet context)।
Application Scope Example
मान लो हमें किसी website का total visitor count रखना है। यह data हर user के लिए same रहेगा, इसलिए इसे application scope में रखना बेहतर है।
<%
Integer count = (Integer) application.getAttribute("visitorCount");
if(count == null) {
count = 1;
} else {
count = count + 1;
}
application.setAttribute("visitorCount", count);
out.print("Total Visitors: " + count);
%>
Application Scope के Important Points
- Application scope data पूरे web app के लिए common रहता है।
- Data तब तक रहेगा जब तक server restart या app stop नहीं होता।
- Common configuration या global information store करने के लिए use किया जाता है।
Application Scope का Use कब करें?
- जब data सभी users के लिए same हो।
- जब data पूरे web application में accessible होना चाहिए।
- जैसे: visitor count, configuration data, system-wide settings।
Difference Between Session Scope and Application Scope
| Feature | Session Scope | Application Scope |
|---|---|---|
| Definition | Particular user के लिए data store करता है। | पूरे web application के लिए common data store करता है। |
| Object | session |
application |
| Lifetime | जब तक user का session active है। | जब तक application run हो रहा है। |
| Scope | Specific user तक limited। | सभी users के लिए common। |
| Use Case | Login info, user preferences। | Visitor count, configuration settings। |
| Memory Usage | Per user memory allocate होती है। | Shared memory use होती है। |
Example: Sharing Data Across Pages
अब एक simple example देखते हैं जहाँ हम Session Scope और Application Scope दोनों को use करते हैं।
Page 1: setData.jsp
<%
session.setAttribute("user", "Amit");
application.setAttribute("college", "ABC University");
response.sendRedirect("getData.jsp");
%>
Page 2: getData.jsp
<%
String user = (String) session.getAttribute("user");
String college = (String) application.getAttribute("college");
out.print("User: " + user + "<br>");
out.print("College: " + college);
%>
Output:
User: Amit College: ABC University
यहाँ user variable सिर्फ उसी user के लिए accessible रहेगा क्योंकि वो session में stored है, जबकि college variable सभी users के लिए accessible रहेगा क्योंकि वो application scope में है।
Best Practices for Using Scope in JSP
- Unnecessary data को session में store न करें, memory waste होती है।
- Application scope data को update करते समय synchronization का ध्यान रखें।
- Sensitive data को session में ही store करें, application scope में नहीं।
- Session timeout को proper configure करें।
- Scope selection हमेशा data की need के हिसाब से करें।
Real-World Uses of Session and Application Scope
- Session Scope: Login management systems, personalized dashboards, online exams।
- Application Scope: Global announcements, configuration values, analytics counters।
Important Exam Points
- JSP में session और application scope data sharing के लिए use होते हैं।
session.setAttribute()औरapplication.setAttribute()दोनों data store करने के लिए use किए जाते हैं।sessionobject user-specific होता है जबकिapplicationobject global होता है।- Exam में अक्सर difference table या example-based question आता है।
Quick Notes for Revision
- Session Scope: user-specific data store करता है।
- Application Scope: पूरे web app के लिए common data store करता है।
- Objects: session और application।
- Lifetime: session या server lifetime तक।
- Use Case: login, visitor count, configuration data।