I am trying to understand the following segment of Java code, which was implemented as a simple server
public class testserver extends AbstractHandler
{
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("movie/html");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>this is a test</h1>");
}
public static void main(String[] args) throws Exception
{
Server server = new Server(1234);
server.setHandler(new testserver());
server.start();
server.join();
}
}
I am kind of confusing about the logic of this code. In specific, in the “main” function, it has
server.setHandler(new testserver());
I know it is to create a new server. But this main function is included in the class of testserver itself. So it functions as calling itself recursively, and it will create a lot of testserver. Is my understanding correct?
The
mainmethod is not called when a new instance of the class is created. It’s called by the system to start the entire process going.