|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ix.util.http.HttpServer
public class HttpServer
An HTTP 1.1 server that can serve static content (files) and also supports servlets.
This is intended as a convenient class that makes it easy to do the things we most want to do, rather than a class that maximises functionality and configurability.
Example:
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import ix.util.http.HttpServer; public class Example { public static void main(String[] argv) { HttpServer server = new HttpServer(8080); server.setDocRoot("myfiles", "files"); server.setLogPath("logs", "myapp"); server.addServlet(new HelloServlet(), "/hello/*"); server.start(); } } public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setStatus(HttpServletResponse.SC_OK); resp.getWriter().println("Hello!"); } }
HttpObjectServlet
,
HttpStringServlet
,
HelloServlet
Constructor Summary | |
---|---|
HttpServer(int port)
Construct a server that listens on the specified port. |
Method Summary | |
---|---|
void |
addServlet(javax.servlet.http.HttpServlet servlet,
java.lang.String pathSpec)
Add a servlet serving URLs that match the specified path. |
void |
addServlet(java.lang.String pathSpec,
javax.servlet.http.HttpServlet servlet)
|
java.lang.String |
getBaseUrl()
Returns a base URL that will produce requests to this server. |
java.lang.String |
getDocPath()
Returns the base path of the URLs that correspond to the files being served as static content. |
java.lang.String |
getDocRoot()
Returns the name of the directory, if any, that contains the files being served as static content. |
int |
getServerPort()
Returns the port this server is listening on. |
void |
join()
|
static void |
main(java.lang.String[] argv)
Main program for testing. |
protected org.mortbay.jetty.servlet.ServletHolder |
makeStaticContentServlet()
|
void |
parseDocRoot(java.lang.String spec,
java.lang.String defaultFilePath)
Interprets the 'spec' as docRoot[:docPath] and tells this server to serve files from the tree below docRoot and to use /docPath in the corresponding URLs. |
void |
setDocRoot(java.lang.String docRoot,
java.lang.String docPath)
Tells this server to serve files from the tree below the specified root directory and to use the specified path in the corresponding URLs. |
void |
setLogPath(java.lang.String dir,
java.lang.String prefix)
Tells this server to write an NCSA-format request log in the specified directory. |
void |
start()
Starts the server running. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public HttpServer(int port)
getServerPort()
.
Method Detail |
---|
public static void main(java.lang.String[] argv)
public int getServerPort()
public java.lang.String getBaseUrl()
http://serverHost:serverPort/
public java.lang.String getDocRoot()
setDocRoot(String docRoot, String docPath)
public java.lang.String getDocPath()
setDocRoot(String docRoot, String docPath)
public void setDocRoot(java.lang.String docRoot, java.lang.String docPath)
If this method has not been called before the server has been started, no files will be served. Otherwise, file docRoot/... will be available as URL http://serverHost:serverPort/docPath/....
parseDocRoot(String docRoot, String defaultFilePath)
public void parseDocRoot(java.lang.String spec, java.lang.String defaultFilePath)
This method calls setDocRoot(String, String)
;
see that method for more information.
public void setLogPath(java.lang.String dir, java.lang.String prefix)
A log will be created only if this method is called before the server has been started.
protected org.mortbay.jetty.servlet.ServletHolder makeStaticContentServlet()
public void addServlet(javax.servlet.http.HttpServlet servlet, java.lang.String pathSpec)
server.addServlet(new HelloServlet("Hello!"), "/hello/*");This method can be called while the server is running and should cause the servlet to become available for handling requests.
public void addServlet(java.lang.String pathSpec, javax.servlet.http.HttpServlet servlet)
public void start()
public void join()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |