Explain how to read servlet parameters with an example

7.B] Explain how to read servlet parameters with an example

Answer:

Note: if difficult to understand, Reading Form Parameters is enough

In a servlet, parameters are often sent from the client (e.g., via an HTML form) and can be accessed using the HttpServletRequest object. Parameters can be accessed in a variety of ways, depending on whether they are query parameters, form parameters, or parameters from a URL.

Here’s a breakdown of how to read servlet parameters:

1. Query Parameters

  • Purpose: Parameters included in the URL after the ? (query string). Example URL: http://example.com/servlet?param1=value1&param2=value2.
  • Method to Use: getParameter(String name)
  • Example Code:
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.IOException;

  public class QueryParamServlet extends HttpServlet {
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
          // Retrieve query parameters
          String param1 = req.getParameter("param1");
          String param2 = req.getParameter("param2");

          // Set content type and write response
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          out.println("<html><body>");
          out.println("<h1>Query Parameters</h1>");
          out.println("<p>param1: " + param1 + "</p>");
          out.println("<p>param2: " + param2 + "</p>");
          out.println("</body></html>");
      }
  }

2. Form Parameters

  • Purpose: Parameters sent via an HTML form using either the GET or POST method.
  • Method to Use: getParameter(String name)
  • Example HTML Form:
  <form action="formServlet" method="post">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name">
      <label for="age">Age:</label>
      <input type="text" id="age" name="age">
      <input type="submit" value="Submit">
  </form>
  • Example Servlet Code:
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.IOException;

  public class FormParamServlet extends HttpServlet {
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
          // Retrieve form parameters
          String name = req.getParameter("name");
          String age = req.getParameter("age");

          // Set content type and write response
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          out.println("<html><body>");
          out.println("<h1>Form Parameters</h1>");
          out.println("<p>Name: " + name + "</p>");
          out.println("<p>Age: " + age + "</p>");
          out.println("</body></html>");
      }
  }

3. URL Path Parameters

  • Purpose: Parameters included in the URL path, typically used with URL rewriting or RESTful web services.
  • Method to Use: getPathInfo(), getServletPath()
  • Example URL: http://example.com/servlet/path/to/resource
  • Example Servlet Code:
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.IOException;

  public class PathParamServlet extends HttpServlet {
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
          // Retrieve path info
          String pathInfo = req.getPathInfo();

          // Set content type and write response
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          out.println("<html><body>");
          out.println("<h1>Path Parameters</h1>");
          out.println("<p>Path Info: " + pathInfo + "</p>");
          out.println("</body></html>");
      }
  }

Summary

  1. Query Parameters:
  • Accessed using req.getParameter("paramName").
  • Sent as part of the URL after the ?.

2. Form Parameters:

  • Accessed using req.getParameter("paramName").
  • Sent via an HTML form with method="post" or method="get".

3. URL Path Parameters:

  • Accessed using req.getPathInfo() or req.getServletPath().
  • Included in the URL path, often used in RESTful services.

Leave a Reply

Your email address will not be published. Required fields are marked *