File Path Resolution: Relative vs Absolute, WEB-INF Protection
File Path Resolution: Relative vs Absolute, WEB-INF Protection
File Path Resolution क्या होता है?
जब भी हम किसी web application में file या resource को access करते हैं, तो server को यह जानना होता है कि उस file का exact location कहाँ है। इसी process को File Path Resolution कहा जाता है। यह define करता है कि web container किसी requested resource को कैसे locate और load करेगा।
Web development में path दो तरह के होते हैं — Relative Path और Absolute Path। इनका सही use करना बहुत ज़रूरी है ताकि files सही तरीके से load हों और application secure बनी रहे।
Relative Path क्या होता है?
Relative Path वह path होता है जो current file या directory के relative होता है। इसका मतलब है कि path में root directory का पूरा location नहीं दिया जाता, बल्कि current file की position से resource को access किया जाता है।
Example (Relative Path):
<img src="images/logo.png" alt="Website Logo">
ऊपर दिए गए example में “images/logo.png” एक relative path है क्योंकि यह current directory के हिसाब से image को load करता है।
Relative Path के फायदे:
- Website को move करना या copy करना आसान होता है।
- Short और readable होता है।
- Server configuration में flexibility देता है।
Relative Path के नुकसान:
- अगर directory structure बदल जाए, तो links टूट सकते हैं।
- बड़ी web projects में maintenance मुश्किल होता है।
Absolute Path क्या होता है?
Absolute Path वह path होता है जो root directory या full URL से शुरू होता है। इस path में resource का पूरा location लिखा होता है ताकि server उसे कहीं से भी easily access कर सके।
Example (Absolute Path):
<img src="/project/images/logo.png" alt="Website Logo">
या external source के लिए:
<img src="https://www.example.com/images/logo.png" alt="Logo">
Absolute Path के फायदे:
- Directory structure बदलने पर भी links टूटते नहीं हैं।
- Server-side referencing के लिए reliable होता है।
- Static resources जैसे CSS, JS, Images के लिए best होता है।
Absolute Path के नुकसान:
- Development से Production environment में path change करना पड़ता है।
- Long और complex URLs readability कम करते हैं।
Relative और Absolute Path में Difference
| Basis | Relative Path | Absolute Path |
|---|---|---|
| Starting Point | Current file या directory से शुरू होता है। | Root directory या full URL से शुरू होता है। |
| Example | images/logo.png | /project/images/logo.png या https://example.com/images/logo.png |
| Flexibility | Portable और flexible होता है। | Static और fixed होता है। |
| Maintenance | Structure change होने पर update ज़रूरी। | Structure change का असर कम। |
| Use Case | Development या internal linking के लिए। | Deployment या external referencing के लिए। |
JSP में File Path Resolution कैसे होता है?
JSP या Servlet environment में, file path resolution server के अंदर web application के folder structure के हिसाब से होता है। Web container जैसे Tomcat यह decide करता है कि किसी resource को कैसे locate किया जाए।
Example:
RequestDispatcher rd = request.getRequestDispatcher("header.jsp");
rd.include(request, response);
यहाँ “header.jsp” relative path है क्योंकि यह current JSP file की directory से resolve होगा। अगर absolute path देना हो तो:
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/header.jsp");
rd.include(request, response);
यह absolute path है जो application के root से शुरू होता है।
WEB-INF Folder क्या है?
WEB-INF web application का सबसे secure directory होती है। यह folder directly accessible नहीं होता client (browser) से। यानी कोई user अगर URL में /WEB-INF लिखे, तो server उसे access नहीं करने देगा।
यह folder केवल server-side files के लिए होता है, जैसे configuration files, libraries और JSPs जिन्हें directly access नहीं करना चाहिए।
WEB-INF के अंदर आम तौर पर ये files होती हैं:
- web.xml — application configuration और servlet mappings define करता है।
- classes/ — compiled Java classes और servlets रखे जाते हैं।
- lib/ — application-specific .jar files रखे जाते हैं।
- jsp/ या views/ — ऐसे JSP files जो user को directly नहीं दिखाने हैं।
Example Directory Structure:
MyWebApp/
│
├── index.jsp
├── images/
│ └── logo.png
├── WEB-INF/
│ ├── web.xml
│ ├── classes/
│ │ └── MyServlet.class
│ └── lib/
│ └── mysql-connector.jar
WEB-INF Protection कैसे काम करता है?
जब कोई user किसी URL से WEB-INF folder में direct access करने की कोशिश करता है, जैसे:
https://www.example.com/WEB-INF/web.xml
तो server automatically 404 error या forbidden message return करता है, क्योंकि WEB-INF folder public के लिए blocked होता है।
यह protection Servlet Specification में define है, जो कहता है कि “Resources under WEB-INF and META-INF are not visible to clients.”
इस protection के फायदे:
- Application की security बढ़ जाती है।
- Confidential files जैसे configuration और credentials safe रहते हैं।
- Backend JSPs को direct access से बचाया जाता है।
JSP में WEB-INF resources को access कैसे करें?
हालांकि WEB-INF folder browser से accessible नहीं होता, लेकिन server-side code के जरिए हम वहां की files को access कर सकते हैं।
Example (JSP में Include Directive):
<%@ include file="/WEB-INF/views/header.jsp" %>
यह code compile time पर header.jsp को current JSP में include कर देगा। यह तरीका safe है क्योंकि file browser से hidden रहती है।
Example (Servlet से Forward करना):
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/home.jsp");
rd.forward(request, response);
यह server-side forwarding है, जो user को directly path नहीं दिखाता।
Best Practices for File Path & WEB-INF Usage
- Static files (CSS, JS, images) को web root में रखें, ताकि browser उन्हें directly access कर सके।
- JSP files जो user को दिखानी नहीं हैं, उन्हें WEB-INF/views folder में रखें।
- Always use relative paths inside JSPs ताकि deployment environment के अनुसार easily adapt हों।
- External resources के लिए absolute URL use करें, जैसे CDN links।
- Confidential files (जैसे database credentials) कभी भी web root में न रखें।
Real-life Example (Relative vs Absolute Path in JSP)
Scenario:
मान लो तुम्हारे पास एक JSP file है /views/home.jsp और तुम उसमें style.css को link करना चाहते हो जो /assets/css/ folder में है।
Relative Path:
<link rel="stylesheet" href="../assets/css/style.css">
Absolute Path:
<link rel="stylesheet" href="/MyWebApp/assets/css/style.css">
अगर project को किसी दूसरे folder में deploy करते हो, तो relative path change करना पड़ सकता है, जबकि absolute path deployment के base path पर depend करता है।
Quick Summary Table
| Concept | Explanation |
|---|---|
| Relative Path | Current file के base पर resource locate करता है। |
| Absolute Path | Root directory या URL से resource locate करता है। |
| WEB-INF | Server-only folder जो direct browser access से protected रहता है। |
| Access Type | Server-side include या forward methods से possible। |
| Security Level | High — क्योंकि user direct URL से access नहीं कर सकता। |
Exam-Oriented Notes
- Relative Path: Current directory के अनुसार path define करता है।
- Absolute Path: Root या complete URL से resource access करता है।
- WEB-INF: Secure directory जो browser से hidden रहता है।
- RequestDispatcher: Servlet में server-side resource access के लिए use होता है।
- include directive: Compile time पर JSP को merge करता है।
- forward method: Runtime पर control दूसरे JSP को pass करता है।
- Protection: WEB-INF और META-INF folders browser-accessible नहीं होते।
- Best Practice: Secure JSPs को हमेशा WEB-INF/views में store करो।