What is a Request Parameter?
A request parameter is a piece of data sent by the client to the server as part of an HTTP request. These are commonly used to pass information such as form data, query strings, or URL parameters.
How to Read Request Parameters in a Servlet
Servlets provide methods to read request parameters:
getParameter(String name): Retrieves the value of a specific parameter.getParameterNames(): Retrieves all parameter names as an enumeration.getParameterValues(String name): Retrieves all values for a given parameter (useful for checkboxes).
Query Strings
Query strings are used to pass parameters to a servlet or web page by appending them to the URL. They are a common way of transferring data in GET requests.
- The query string starts after the
?symbol in a URL. - Parameters are separated by
&. - Each parameter consists of a name and a value, separated by an equal sign (
=).
Example of a Query String
Here is an example URL with a query string:
http://example.com/servlet?name=JohnDoe&age=25&city=NewYork
This URL contains the following parameters:
- name: JohnDoe
- age: 25
- city: NewYork
How to Read Query Strings in a Servlet?
In the servlet, you can use the getParameter() method of HttpServletRequest to read the query string parameters:
String name = request.getParameter("name");
String age = request.getParameter("age");
String city = request.getParameter("city");
You can then use the retrieved values to process the request and generate a dynamic response.
Example Servlet Code:
public class QueryStringServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
String city = request.getParameter("city");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>User Information</h1>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Age: " + age + "</p>");
out.println("<p>City: " + city + "</p>");
}
}
Form Data Reading in Servlet
In this section, we will discuss how form data is sent from the client to the server and how to read that data in a servlet using the HttpServletRequest object.
How Form Data is Sent
When you submit a form from an HTML page, the form data is sent to the server using either the GET or POST method. The form data is sent as key-value pairs in the request body (POST) or in the URL (GET).
HTML Form Example
<form action="/submitForm" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br><br>
<input type="submit" value="Submit">
</form>
Reading Form Data in Servlet
In the servlet, we can read the form data using the getParameter() method of the HttpServletRequest object:
String name = request.getParameter("name");
String email = request.getParameter("email");
String age = request.getParameter("age");
These methods return the form data as strings. You can then use the data to process the request or perform any necessary operations on the server.
Java Code Example:
public class FormDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String age = request.getParameter("age");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Form Data Submitted</h1>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Email: " + email + "</p>");
out.println("<p>Age: " + age + "</p>");
}
}
GET vs POST Requests
In HTTP, GET and POST are two commonly used methods for sending data between a client and a server. While they serve similar purposes, they differ in how the data is sent, security, and use cases.
1. GET Request
The GET method is used to request data from a specified resource. It appends data to the URL, which makes it less secure and suitable for non-sensitive data or retrieving information.
- Data Sending: Data is appended to the URL as query parameters (e.g.,
?name=John&age=25). - Security: Data is visible in the URL, making it less secure.
- Length Limit: URL length is limited (around 2000 characters depending on the browser and server).
- Use Cases: Used for retrieving data or requesting non-sensitive information (e.g., search queries, pagination).
2. POST Request
The POST method is used to send data to the server for processing (e.g., submitting a form). It sends data in the request body, making it more secure than GET for transmitting sensitive data.
- Data Sending: Data is sent in the body of the request, not visible in the URL.
- Security: More secure than GET because the data is not exposed in the URL.
- Length Limit: No practical size limit for data compared to GET.
- Use Cases: Used for submitting form data, login credentials, or any data that modifies the server state.
3. GET vs POST Comparison
| Aspect | GET | POST |
|---|---|---|
| Data Location | Appended to URL as query parameters | Sent in the body of the request |
| Security | Less secure (data visible in URL) | More secure (data hidden in body) |
| Data Size | Limited by URL length (around 2000 characters) | No significant limit |
| Use Case | Retrieving data, search queries, navigation | Submitting data, forms, login |
4. Example GET Request
<a href="/search?query=java">Search for Java</a>
This example demonstrates a GET request, where the query parameter query=java is appended to the URL.
5. Example POST Request
<form action="/submit" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit">
</form>
This example demonstrates a POST request, where the form data is sent securely in the request body when the form is submitted.