Class HttpResponse

java.lang.Object
com.creativityfactory.swiftserver.response.HttpResponse
All Implemented Interfaces:
Response

public class HttpResponse extends Object implements 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>");
 
  • Constructor Details

    • HttpResponse

      public HttpResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Throws:
      IOException
  • Method Details

    • status

      public Response status(int n)
      Description copied from interface: Response
      Sets the status code for the response of an HTTP request.
      Specified by:
      status in interface Response
      Parameters:
      n - the HTTP code.
      Returns:
      the current Response object for chaining.
    • cacheControl

      public Response cacheControl(int maxAge)
      Description copied from interface: Response
      Sets the max-age attribute of control-cache header.
      Specified by:
      cacheControl in interface Response
      Parameters:
      maxAge - the max age in milliseconds.
      Returns:
      the current Response object for chaining.
    • lastModified

      public Response lastModified(long last)
      Description copied from interface: Response
      Sets the header last-modified in the HTTP response.
      Specified by:
      lastModified in interface Response
      Parameters:
      last - the date in milliseconds.
      Returns:
      the current Response object for chaining.
    • write

      public Response write(String msg)
      Description copied from interface: Response
      Writes a string to the response output stream.
      Specified by:
      write in interface Response
      Parameters:
      msg - the string value to write.
      Returns:
      the current Response object for chaining.
    • out

      public PrintWriter out() throws IOException
      Description copied from interface: Response
      Gets the PrintWriter object to write to the response output stream.
      Specified by:
      out in interface Response
      Returns:
      the PrintWriter object.
      Throws:
      IOException - if an I/O error occurs.
    • json

      public void json(Object obj)
      Description copied from interface: Response
      Writes an object in JSON format to the response output stream.
      Specified by:
      json in interface Response
      Parameters:
      obj - the object to write.
    • getRequest

      public jakarta.servlet.http.HttpServletRequest getRequest()
    • redirect

      public void redirect(String url)
      Description copied from interface: Response
      Redirects the request to the specified URL.
      Specified by:
      redirect in interface Response
      Parameters:
      url - the URL to redirect to.
    • setHeader

      public Response setHeader(String key, String value)
      Description copied from interface: Response
      Sets a header for the HTTP response.
      Specified by:
      setHeader in interface Response
      Parameters:
      key - the header name.
      value - the header value.
      Returns:
      the current Response object for chaining.
    • setDateHeader

      public Response setDateHeader(String name, long time)
      Description copied from interface: Response
      Sets a date header for the HTTP response.
      Specified by:
      setDateHeader in interface Response
      Parameters:
      name - the header name.
      time - the time in milliseconds.
      Returns:
      the current Response object for chaining.
    • jsp

      public void jsp(String jspPage) throws Exception
      Description copied from interface: Response
      Renders a JSP page to the response output stream.
      Specified by:
      jsp in interface Response
      Parameters:
      jspPage - the JSP page to render.
      Throws:
      Exception - if an error occurs while rendering the JSP page.
    • sendError

      public void sendError(int i, String message)
      Description copied from interface: Response
      Sends an error response to the client using the specified status code and message.
      Specified by:
      sendError in interface Response
      Parameters:
      i - the HTTP status code.
      message - the error message.