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
ConstructorsConstructorDescriptionHttpResponse(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.HttpServletRequestvoidWrites an object in JSON format to the response output stream.voidRenders 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.voidRedirects the request to the specified URL.voidSends 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:ResponseSets the status code for the response of an HTTP request. -
cacheControl
Description copied from interface:ResponseSets the max-age attribute of control-cache header.- Specified by:
cacheControlin interfaceResponse- Parameters:
maxAge- the max age in milliseconds.- Returns:
- the current Response object for chaining.
-
lastModified
Description copied from interface:ResponseSets the header last-modified in the HTTP response.- Specified by:
lastModifiedin interfaceResponse- Parameters:
last- the date in milliseconds.- Returns:
- the current Response object for chaining.
-
write
Description copied from interface:ResponseWrites a string to the response output stream. -
out
Description copied from interface:ResponseGets the PrintWriter object to write to the response output stream.- Specified by:
outin interfaceResponse- Returns:
- the PrintWriter object.
- Throws:
IOException- if an I/O error occurs.
-
json
Description copied from interface:ResponseWrites an object in JSON format to the response output stream. -
getRequest
public jakarta.servlet.http.HttpServletRequest getRequest() -
redirect
Description copied from interface:ResponseRedirects the request to the specified URL. -
setHeader
Description copied from interface:ResponseSets a header for the HTTP response. -
setDateHeader
Description copied from interface:ResponseSets a date header for the HTTP response.- Specified by:
setDateHeaderin interfaceResponse- Parameters:
name- the header name.time- the time in milliseconds.- Returns:
- the current Response object for chaining.
-
jsp
Description copied from interface:ResponseRenders a JSP page to the response output stream. -
sendError
Description copied from interface:ResponseSends an error response to the client using the specified status code and message.
-