Category: java
request and response handling
Published on 30 Sep 2026
Explanation
HttpServletRequest provides information about the incoming HTTP request, while HttpServletResponse is used to construct the response.
Code:
doGet(HttpServletRequest request,
HttpServletResponse response) {
// request β client information
// response β server response
}
Explanation
Read request information such as HTTP method, URI, and client information from HttpServletRequest.
Code:
String method = request.getMethod(); String uri = request.getRequestURI(); String ip = request.getRemoteAddr();
Explanation
Set the response content type before writing data to the client.
Code:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello</h1>");
Explanation
Set HTTP status codes to indicate the result of request processing.
Code:
response.setStatus(HttpServletResponse.SC_OK);
// Example error
response.sendError(
HttpServletResponse.SC_NOT_FOUND,
"User not found"
);
Explanation
Redirect the client to another URL using sendRedirect().
Code:
response.sendRedirect("login.html");