![]() |
VOOZH | about |
The ServletRequest and ServletResponse interfaces are used in Java Servlet to handle client requests and server responses. ServletRequest reads data sent by the client, while ServletResponse sends the processed response back to the browser.
ServletRequest is used to access form data, headers, and request information. ServletResponse is used to generate and send responses to the client. The ServletRequest interface is used to receive and process client request data in a servlet application.
The ServletResponse interface is used to send responses from the servlet to the client browser.
Follow these steps to display the name of user using servlet.
This HTML form takes the user's name as input and sends it to the servlet using the GET method.
This servlet receives the request, extracts the user name using getParameter(), and sends a response back to the browser.
This configuration maps the servlet URL pattern to the servlet class.
<servlet>
<servlet-name>MockServ</servlet-name>
<servlet-class>MockServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MockServ</servlet-name>
<url-pattern>/welcomeUser</url-pattern>
</servlet-mapping>
Output:
Open browser and type:
http://localhost:8080/ServletExample/newindex.html
If the user enters Geeks, the browser displays
Welcome User Geeks
| Method | Description |
|---|---|
| public String getParameter(String name) | Returns the value of the specified request parameter |
| public String[] getParameterValues(String name) | Returns all values of the specified parameter as an array |
| java.util.Enumeration getParameterNames() | Returns all request parameter names |
| public int getContentLength() | Returns the length of the request content |
| public String getCharacterEncoding() | Returns the character encoding of the request |
| public String getContentType() | Returns the MIME type of the request |