|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectix.util.http.HttpServer
public class HttpServer
An HTTP 1.1 server that can serve static content (files) and also supports servlets.
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 |
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. |
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 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, no files will be served. Otherwise, file docRoot/... will be available as URL http://serverHost:serverPort/docPath/..., except that docPath must contain the initial "/".
public void setLogPath(java.lang.String dir, java.lang.String prefix)
public void addServlet(javax.servlet.http.HttpServlet servlet, java.lang.String pathSpec)
server.addServlet(new HelloServlet("Hello!"), "/hello/*");
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 |