Write your own web container Tomjetty (ii) to open the server

Source: Internet
Author: User
Tags config socket thread thread class root directory

In the previous section, we did some web-related knowledge and review for implementing the Tomjetty server. Then from this section officially began to realize the Tomjetty server "cottage" tour. In order to implement a server, the first step is to build the server and be able to run properly in order to wait for requests from the client. With this in mind, we'll deal with the creation and start-up of the Tomjetty server in this section.

First, the server establishment

1. Create a new Java project called Tomjetty.

2. Under the Engineering root directory, create a new Tomjetty.config file to provide server configuration parameters.

tomjetty.port=9527
Tomjetty.requestheader.class=cn.lynn.tomjetty.requestheaderparserimpl

3. Write a tool class Tomjettyutil for the program to read server configuration parameter values.

Package cn.lynn.tomjetty;  
Import java.io.IOException;  
Import Java.io.FileInputStream;  
Import java.util.Properties;  
      
public class Tomjettyutil {  
      
    private static properties props = new properties ();  
          
    static {  
        try {  
            props.load (new FileInputStream (".//tomjetty.config"));  
        \ catch (IOException e) {  
            E.printstacktrace ();  
            System.exit (0);  
        }  
    }  
          
    public static string GetValue (String key) {return  
        props.getproperty (key);  
    }  
}

4. Write a Tomjetty class that inherits from the thread class and is used to encapsulate server objects.

public class Tomjetty extends Thread {  
    private static serversocket server;  
    private socket socket;  
      
    Public Tomjetty (socket socket) {  
        this.socket = socket;  
    }  
          
    ...  
}

5. Provide the OpenServer () and CloseServer () methods for the Tomjetty class to encapsulate the details of opening and shutting down the server.

public static void OpenServer () throws Exception {  
        Server = new ServerSocket (Integer.parseint (tomjettyutil.getvalue ("Tomjetty.port"));  
        while (true) {  
            new Tomjetty (Server.accept ()). Start ();  
      
public static void CloseServer () throws Exception {  
    if (server!= null) {  
        if (!server.isclosed ()) {  
            Server.clo Se ();}}}  

Up to this point, the Tomjetty server has been established, and the OpenServer () and CloseServer () methods are available for external program calls to perform the operation of opening and shutting down the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.