Feedback Form

Request Parsing: Headers, Method, PathInfo, ServletPath, ContextPath

Request Parsing in Servlets (Headers, Method, PathInfo, ServletPath, ContextPath)

Request Parsing in Servlets क्या होता है?

जब भी कोई client (जैसे web browser) server को request भेजता है, तो server-side program यानी Servlet उस request को process करता है। इस पूरी process में request object बहुत important role निभाता है। Request parsing का मतलब है — client द्वारा भेजे गए data को read और analyze करना ताकि servlet उस data पर सही action ले सके।

Servlet API हमें कई methods provide करता है जिससे हम request के अलग-अलग parts जैसे headers, method type, path info, servlet path और context path को access कर सकते हैं।

HttpServletRequest Object क्या है?

HttpServletRequest एक interface है जो client से आने वाली पूरी HTTP request को represent करता है। इसमें methods होते हैं जिनकी help से हम request से जुड़ी सारी information जैसे parameters, headers, path आदि निकाल सकते हैं।

Example के लिए — जब कोई user form submit करता है, तो वही data HttpServletRequest object के अंदर आता है। Servlet इस object से values निकालकर process करता है।

Important Methods of HttpServletRequest

  • getParameter(String name) – form data से parameter value लाने के लिए।
  • getHeader(String name) – specific header value प्राप्त करने के लिए।
  • getMethod() – HTTP method (GET या POST) जानने के लिए।
  • getContextPath() – web application के base path को return करता है।
  • getServletPath() – servlet mapping path को return करता है।
  • getPathInfo() – extra path information return करता है जो servlet mapping के बाद आती है।

Headers in Servlets

HTTP headers वो extra information होती है जो client request के साथ भेजता है। उदाहरण के लिए — browser type, language preference, cookies, etc. Servlet में headers को पढ़ने के लिए हम getHeader() और getHeaderNames() methods का उपयोग करते हैं।

Example: Reading Headers

Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
  String name = headerNames.nextElement();
  String value = request.getHeader(name);
  System.out.println(name + " : " + value);
}

इस example में servlet हर header का नाम और उसकी value print करता है।

Commonly Used Headers

Header Name Description
User-Agent Browser और Operating System की जानकारी देता है
Accept-Language Preferred language बताता है
Cookie Client side cookies की जानकारी देता है
Host Server के hostname और port number को बताता है

Request Method (GET और POST)

Servlets में request method यह बताता है कि client ने server से कैसे communication किया है। सबसे common दो methods हैं — GET और POST

GET Method

GET method में data URL के साथ भेजा जाता है। यह method generally data fetch करने के लिए use होती है।

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  String name = request.getParameter("name");
  response.getWriter().println("Hello " + name);
}

POST Method

POST method में data request body में भेजा जाता है, जो secure होता है और large data transfer के लिए use किया जाता है।

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  String email = request.getParameter("email");
  response.getWriter().println("Email received: " + email);
}

PathInfo, ServletPath और ContextPath

जब URL server पर servlet तक पहुँचता है, तो वह कई parts में divide होता है। इन्हें समझना जरूरी है ताकि हम dynamic routing और resource handling को सही से manage कर सकें।

Context Path

Context Path वो base path होता है जहाँ आपकी web application deploy की गई होती है। यह application का root path represent करता है।

Example:

http://localhost:8080/myapp/servlet1

यहाँ Context Path = /myapp

Servlet Path

Servlet Path वो path होता है जो web.xml या annotation में servlet के लिए define किया गया है।

Example:

@WebServlet("/servlet1")

यहाँ Servlet Path = /servlet1

Path Info

Path Info servlet path के बाद आने वाला additional data होता है। इसे dynamic URL handling के लिए use किया जाता है।

Example URL:

http://localhost:8080/myapp/servlet1/student/101

यहाँ Path Info = /student/101

Example Code for Path Extraction

String context = request.getContextPath();
String servlet = request.getServletPath();
String info = request.getPathInfo();
System.out.println("Context Path: " + context);
System.out.println("Servlet Path: " + servlet);
System.out.println("Path Info: " + info);

एक Complete Example

मान लीजिए हमारे पास एक servlet है जो student information handle करता है।

@WebServlet("/student/*")
public class StudentServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String context = request.getContextPath();
    String servlet = request.getServletPath();
    String info = request.getPathInfo();

    out.println("<h3>Context Path: " + context + "</h3>");
    out.println("<h3>Servlet Path: " + servlet + "</h3>");
    out.println("<h3>Path Info: " + info + "</h3>");
  }
}

अगर URL है http://localhost:8080/myapp/student/102, तो output कुछ ऐसा होगा:

PropertyValue
Context Path/myapp
Servlet Path/student
Path Info/102

Request Parsing Flow

Servlet में request parsing का step-by-step flow कुछ इस प्रकार होता है:

  • Client कोई request भेजता है (जैसे form submit)।
  • Server उस request को Servlet container (जैसे Tomcat) को देता है।
  • Container HttpServletRequest object बनाता है।
  • Servlet उस object से headers, parameters, path info आदि पढ़ता है।
  • Servlet logic apply करके response generate करता है।

Request और Response का Relationship

Request parsing का असली फायदा तब दिखता है जब हम सही तरीके से response generate करते हैं। Servlet हमेशा request → process → response cycle पर काम करता है।

जितनी सटीक parsing होगी, उतना ही accurate output मिलेगा। इसलिए headers, path, method और parameters की समझ servlet development का core हिस्सा है।

Important Points (Quick Notes)

  • Request parsing का मतलब है incoming data को समझना।
  • HttpServletRequest interface इसके लिए core tool है।
  • Headers browser और client की information देते हैं।
  • getMethod() से पता चलता है कि GET है या POST।
  • ContextPath application का base URL होता है।
  • ServletPath mapping का actual path होता है।
  • PathInfo extra dynamic data को represent करता है।

Exam-Oriented Notes (Short Revision)

  • Request parsing → Request data को read और analyze करने की process।
  • Header → Client द्वारा भेजी गई additional information।
  • Method → GET/POST; data भेजने का तरीका।
  • PathInfo → Extra dynamic path जो servlet mapping के बाद आता है।
  • ServletPath → Servlet के mapping का actual URL path।
  • ContextPath → Application का root path।
  • HttpServletRequest → Object जो पूरी request को represent करता है।
  • Common headers: User-Agent, Host, Cookie, Accept-Language।
  • Parsing का use → Request understanding, data extraction और routing control।
  • Exam Tip → Methods और URLs के examples याद रखें।