Response Crafting: Status Codes (200–500), Content-Type, and Caching
Status Codes, Content-Type, and Caching in Web Development
जब भी हम किसी website या web application को access करते हैं, तब browser और server के बीच एक communication होता है जिसे हम HTTP Cycle कहते हैं। इस communication में तीन main elements का बहुत बड़ा role होता है — Status Codes, Content-Type, और Caching। ये तीनों factors किसी भी web page की performance, user experience और SEO ranking को काफी प्रभावित करते हैं।
What are Status Codes?
Status Codes वो response messages होते हैं जो server, client (browser) को भेजता है यह बताने के लिए कि request का result क्या रहा — successful हुआ या error आया। ये codes तीन अंकों (3-digit numbers) के होते हैं, और हर code का एक specific meaning होता है।
Types of HTTP Status Codes
Status codes को 5 major categories में divide किया गया है:
- 1xx – Informational: Request receive हो गई है और process जारी है।
- 2xx – Success: Request successfully complete हो गई है।
- 3xx – Redirection: Client को किसी दूसरे URL पर redirect किया जा रहा है।
- 4xx – Client Error: Request में कोई mistake है, जैसे wrong URL या unauthorized access।
- 5xx – Server Error: Server में problem है और वो request process नहीं कर पा रहा।
Commonly Used HTTP Status Codes
| Status Code | Meaning | Description |
|---|---|---|
| 200 | OK | Request successfully completed — everything is fine. |
| 201 | Created | New resource successfully created on the server. |
| 301 | Moved Permanently | URL permanently redirect हो चुका है। |
| 302 | Found | Temporary redirect है — future में original URL वापस valid रहेगा। |
| 400 | Bad Request | Client side पर request गलत या incomplete है। |
| 401 | Unauthorized | Access के लिए authentication required है। |
| 403 | Forbidden | Client को resource access करने की permission नहीं है। |
| 404 | Not Found | Requested resource server पर नहीं मिला। |
| 500 | Internal Server Error | Server के अंदर कोई unexpected error हुआ है। |
| 503 | Service Unavailable | Server temporarily down है या maintenance में है। |
Example of Sending Status Codes in Servlets
अगर आप Java Servlets use कर रहे हैं, तो status code को इस तरह set किया जा सकता है:
response.setStatus(HttpServletResponse.SC_OK); // 200 OK
response.setStatus(HttpServletResponse.SC_NOT_FOUND); // 404 Not Found
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // 500 Error
ये status codes browser को बताते हैं कि server ने request को कैसे handle किया।
Understanding Content-Type
Content-Type एक HTTP header है जो यह बताता है कि server किस type का data भेज रहा है। इससे browser decide करता है कि data को कैसे render या display करना है।
Commonly Used Content Types
| Content-Type | Meaning |
|---|---|
| text/html | HTML content भेजा जा रहा है। |
| text/plain | Plain text data है, कोई formatting नहीं। |
| application/json | JSON format में data भेजा जा रहा है। |
| application/xml | XML format data भेजा जा रहा है। |
| multipart/form-data | File upload के समय use किया जाता है। |
Example of Setting Content-Type in Servlet
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Welcome to Servlet Example</h2>");
यह code server को बताता है कि client को HTML format में response भेजना है।
What is Caching?
Caching एक technique है जिससे website की speed और performance improve होती है। जब user पहली बार किसी page को access करता है, तो उसका data browser या proxy server में temporarily store हो जाता है ताकि अगली बार वही data तुरंत load हो सके।
Types of Web Caching
- Browser Cache: Client side पर data store होता है ताकि अगली बार page तेजी से open हो।
- Proxy Cache: Network level पर caching होती है — multiple users के लिए common data store करता है।
- Server Cache: Server side caching जैसे Redis या Memcached performance को boost करते हैं।
Benefits of Caching
- Website loading speed बढ़ती है।
- Server load और bandwidth usage कम होता है।
- Better user experience मिलता है।
- SEO performance improve होती है।
Setting Caching Headers
HTTP caching के लिए server कुछ specific headers भेजता है — जैसे:
| Header | Description |
|---|---|
| Cache-Control | Cache behavior को define करता है जैसे max-age या no-cache। |
| Expires | Response कब तक valid रहेगा, इसका time बताता है। |
| ETag | File का unique identifier होता है, जिससे browser check करता है कि file change हुई या नहीं। |
| Last-Modified | File के last modification time को बताता है। |
Example: Cache-Control Header in Servlet
response.setHeader("Cache-Control", "max-age=3600"); // Cache for 1 hour
response.setHeader("Expires", "Wed, 21 Oct 2025 07:28:00 GMT");
इस code से browser को instruction मिलती है कि page का data 1 घंटे तक cache में रखा जा सकता है।
How Status Codes, Content-Type, and Caching Work Together
ये तीनों elements मिलकर एक complete HTTP response बनाते हैं। उदाहरण के लिए:
HTTP/1.1 200 OK
Content-Type: text/html
Cache-Control: max-age=3600
<html>
<body>
<h2>Welcome to My Website</h2>
</body>
</html>
यहाँ:
- Status Code (200): बताता है कि request successful है।
- Content-Type: Data का format HTML है।
- Cache-Control: Data को 1 घंटे तक cache किया जा सकता है।
Why These Headers Are Important for SEO
- 200 OK search engines को बताता है कि page accessible है।
- 301 Redirect SEO value को नए URL पर transfer करता है।
- 404 Not Found गलत link या missing page को indicate करता है — इसे handle करना जरूरी है।
- Content-Type बताता है कि crawler को data कैसे interpret करना चाहिए।
- Caching page speed बढ़ाता है, जो Google ranking factor है।
Best Practices
- हर servlet या API में सही Status Code भेजें।
- Correct Content-Type set करें ताकि browser data सही format में दिखा सके।
- Effective Caching policy अपनाएँ ताकि website fast load हो।
- Error pages (जैसे 404, 500) के लिए custom HTML page design करें।
- SEO और performance testing tools जैसे Google Lighthouse से optimization check करें।
Quick Notes for Exams
- Status Code: Server response का signal होता है।
- Content-Type: Data का format define करता है।
- Caching: Performance बढ़ाने की technique है।
- 200 OK: Successful request का indication।
- 404: Resource not found।
- 301: Permanent redirection।
- Cache-Control: Caching behavior control करता है।
- ETag: File version identify करने के लिए use होता है।
- Exam Tip: हमेशा remember करें — HTTP response तीन चीज़ों पर depend करता है: Status Code, Content-Type, और Caching headers।