Class HttpResponse
java.lang.Object
com.creativityfactory.swiftserver.response.HttpResponse
- All Implemented Interfaces:
Response
An implementation of the
Response
interface that provides methods for setting HTTP response
attributes and writing response data.
This class is intended for use in Java EE web applications, where it can be used to generate
HTTP responses for client requests.
Example usage:
Response response = new HttpResponse(req, res);
response.status(Response.REST_OK)
.cacheControl(60 * 60 * 24 * 30 * 12) // cache for one year
.setHeader("Content-Type", "text/html")
.write("<html><body><h1>Hello, world!</h1></body></html>");
-
Field Summary
Fields inherited from interface com.creativityfactory.swiftserver.response.Response
REST_BAD_REQUEST, REST_CREATED, REST_INTERNAL_SERVER_ERROR, REST_NOT_FOUND, REST_NOT_MODIFIED
-
Constructor Summary
ConstructorDescriptionHttpResponse
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) -
Method Summary
Modifier and TypeMethodDescriptioncacheControl
(int maxAge) Sets the max-age attribute of control-cache header.jakarta.servlet.http.HttpServletRequest
void
Writes an object in JSON format to the response output stream.void
Renders a JSP page to the response output stream.lastModified
(long last) Sets the header last-modified in the HTTP response.out()
Gets the PrintWriter object to write to the response output stream.void
Redirects the request to the specified URL.void
Sends an error response to the client using the specified status code and message.setDateHeader
(String name, long time) Sets a date header for the HTTP response.Sets a header for the HTTP response.status
(int n) Sets the status code for the response of an HTTP request.Writes a string to the response output stream.
-
Constructor Details
-
HttpResponse
public HttpResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException - Throws:
IOException
-
-
Method Details
-
status
Description copied from interface:Response
Sets the status code for the response of an HTTP request. -
cacheControl
Description copied from interface:Response
Sets the max-age attribute of control-cache header.- Specified by:
cacheControl
in interfaceResponse
- Parameters:
maxAge
- the max age in milliseconds.- Returns:
- the current Response object for chaining.
-
lastModified
Description copied from interface:Response
Sets the header last-modified in the HTTP response.- Specified by:
lastModified
in interfaceResponse
- Parameters:
last
- the date in milliseconds.- Returns:
- the current Response object for chaining.
-
write
Description copied from interface:Response
Writes a string to the response output stream. -
out
Description copied from interface:Response
Gets the PrintWriter object to write to the response output stream.- Specified by:
out
in interfaceResponse
- Returns:
- the PrintWriter object.
- Throws:
IOException
- if an I/O error occurs.
-
json
Description copied from interface:Response
Writes an object in JSON format to the response output stream. -
getRequest
public jakarta.servlet.http.HttpServletRequest getRequest() -
redirect
Description copied from interface:Response
Redirects the request to the specified URL. -
setHeader
Description copied from interface:Response
Sets a header for the HTTP response. -
setDateHeader
Description copied from interface:Response
Sets a date header for the HTTP response.- Specified by:
setDateHeader
in interfaceResponse
- Parameters:
name
- the header name.time
- the time in milliseconds.- Returns:
- the current Response object for chaining.
-
jsp
Description copied from interface:Response
Renders a JSP page to the response output stream. -
sendError
Description copied from interface:Response
Sends an error response to the client using the specified status code and message.
-