ro.polak.littleServlet
Class HTTPResponse

java.lang.Object
  extended by ro.polak.littleServlet.HTTPResponse

public class HTTPResponse
extends java.lang.Object

Represents HTTP response

Java Little Web Server Homepage

Version:
1.0/21.02.2008
Author:
Piotr Polak www.polak.ro

Constructor Summary
HTTPResponse(java.net.Socket socket)
          Class constructor
 
Method Summary
 void close()
          Closes the socket
 void flush()
          Flushes the output
 boolean flushHeaders()
          Flushes headers, returns false when headers already flushed
 HTTPResponseHeaders getHeaders()
           
 PrintWriter getPrintWriter()
          Returns request's print writer
 boolean isCommitted()
          Returns a boolean indicating if the response has been committed.
 void sendRedirect(java.lang.String location)
          Redirects the request to the specified location.
 void serveFile(java.io.File file)
          Flushes headers and serves the specified file
 void setContentLength(int length)
          Sets content length
 void setContentLength(long length)
          Sets content length
 void setContentType(java.lang.String contentType)
          Sets content type
 void setCookie(java.lang.String cookieName, java.lang.String cookieValue)
          Sets cookie, expires when browser closed
 void setCookie(java.lang.String cookieName, java.lang.String cookieValue, int cookieExpiresSeconds)
          Sets cookie Use negative time to remove cookie
 void setCookie(java.lang.String cookieName, java.lang.String cookieValue, int cookieExpiresSeconds, java.lang.String cookiePath)
          Sets cookie
 void setHeader(java.lang.String headerName, java.lang.String headerValue)
          Sets header
 void setKeepAlive(boolean keepAlive)
          Sets keepAlive
 void setStatus(java.lang.String status)
          Sets status of response
 void write(byte[] byteArray)
          Writes byte array to the output
 void write(java.lang.String s)
          Writes string to the output
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPResponse

public HTTPResponse(java.net.Socket socket)
Class constructor

Parameters:
socket - - socket to be written
Method Detail

write

public void write(byte[] byteArray)
Writes byte array to the output

Parameters:
byteArray - byte array

write

public void write(java.lang.String s)
Writes string to the output

Parameters:
s - String to be written

setCookie

public void setCookie(java.lang.String cookieName,
                      java.lang.String cookieValue,
                      int cookieExpiresSeconds,
                      java.lang.String cookiePath)
Sets cookie

Parameters:
cookieName - name of the cookie
cookieValue - String value of the cookie
cookieExpiresSeconds - expire time in seconds
cookiePath - path for the cookie

setCookie

public void setCookie(java.lang.String cookieName,
                      java.lang.String cookieValue)
Sets cookie, expires when browser closed

Parameters:
cookieName - name of the cookie
cookieValue - String value of the cookie

setCookie

public void setCookie(java.lang.String cookieName,
                      java.lang.String cookieValue,
                      int cookieExpiresSeconds)
Sets cookie

Use negative time to remove cookie

Parameters:
cookieName - name of the cookie
cookieValue - String value of the cookie
cookieExpiresSeconds - expire time in seconds

flushHeaders

public boolean flushHeaders()
Flushes headers, returns false when headers already flushed

Returns:
true if headers flushed

isCommitted

public boolean isCommitted()
Returns a boolean indicating if the response has been committed. A commited response has already had its status code and headers written.

Returns:
a boolean indicating if the response has been committed

flush

public void flush()
           throws java.io.IOException
Flushes the output

Throws:
java.io.IOException

sendRedirect

public void sendRedirect(java.lang.String location)
Redirects the request to the specified location.

Parameters:
location - - relative or absolute path (URL)

serveFile

public void serveFile(java.io.File file)
Flushes headers and serves the specified file

Parameters:
file - file to be served

setContentType

public void setContentType(java.lang.String contentType)
Sets content type

Parameters:
contentType - content type

setHeader

public void setHeader(java.lang.String headerName,
                      java.lang.String headerValue)
Sets header

Parameters:
headerName - name of the header
headerValue - value of the header

setKeepAlive

public void setKeepAlive(boolean keepAlive)
Sets keepAlive

Parameters:
keepAlive - true for keep alive connection

setContentLength

public void setContentLength(int length)
Sets content length

Parameters:
length - length of content

setContentLength

public void setContentLength(long length)
Sets content length

Parameters:
length - length of content

getHeaders

public HTTPResponseHeaders getHeaders()

setStatus

public void setStatus(java.lang.String status)
Sets status of response

Parameters:
status - status code and message

close

public void close()
           throws java.io.IOException
Closes the socket

Throws:
java.io.IOException

getPrintWriter

public PrintWriter getPrintWriter()
Returns request's print writer

Returns:
request's print writer