Search code examples
javawebporthttpserver

httpwebserver.stop() is not working in Java web server


I'm confused on how can I prevent this error, when starting the button it works and there's no problem, but when I try stop the button with contains function stopping the httpserver, it says "this.server is null". Is there anything wrong with my code?

Please see the image below which returns and error during executing the stop button

enter image description here

Main.java

package main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
    public static int port = 8006;
    public static void main(String[] args) {
            
        JFrame FRAME = new JFrame();
        JLabel LABEL  = new JLabel("Port");
        JLabel lbl_status  = new JLabel("STATUS");
        JLabel lbl_server  = new JLabel("SERVER");
        JTextField jfield = new JTextField("");
        JButton btnStart = new JButton("START");
        JButton btnStop = new JButton("STOP");
        JRadioButton http=new JRadioButton("HTTP"); 
        JRadioButton https=new JRadioButton("HTTPS"); 
        ButtonGroup group = new ButtonGroup();
        ImageIcon img = new ImageIcon("images/con_img.png");
        
        btnStart.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //your actions
                SimpleHttpServer httpServer = new SimpleHttpServer();
                SimpleHttpsServer httpsServer = new SimpleHttpsServer();
                String portserver = jfield.getText();
                
                port = Integer.parseInt(portserver);
                http.setActionCommand("http");
                https.setActionCommand("https");
                
                if (!portserver.isEmpty()) {
                    String url = group.getSelection().getActionCommand();
                    
                    if(url=="http") {
                        System.out.println("http");
                        httpServer.Start(port);
                        lbl_status.setVisible(true);
                        lbl_status.setText("Status : HTTP Successfully Connected to PORT " + port);
                        lbl_status.setForeground(Color.green);
                    }
                    else {
                        System.out.println("https");
                        httpsServer.Start(port);
                        lbl_status.setVisible(true);
                        lbl_status.setText("Status : HTTPS Successfully Connected to PORT " + port);
                        lbl_status.setForeground(Color.green);
                    }
                }
                else {
                    System.out.println("Don't Leave a blank" + portserver);
                }
                
            }
        });
        
        
        btnStop.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //your actions
                

                String portserver = jfield.getText();
                
                
                if (!portserver.isEmpty()) {
                    String url = group.getSelection().getActionCommand();
                    
                    if(url=="http") {
                        SimpleHttpServer httpServer = new SimpleHttpServer();
                        lbl_status.setText("Status : http PORT " + port + " "+ "has been stopped");
                        lbl_status.setForeground(Color.red);
                        httpServer.Stop();
                    }
                    else {
                        SimpleHttpsServer httpsServer = new SimpleHttpsServer();
                        lbl_status.setText("Status : https PORT " + port + " "+ "has been stopped");
                        lbl_status.setForeground(Color.red);
                        httpsServer.Stop();
                    }
                }
                else {
                    System.out.println("Don't Leave a blank" + portserver);
                }
                
                
            }
        });
        
        
        LABEL.setBounds(160,60,100,100);
        lbl_server.setBounds(150,165,100,100);
        lbl_status.setBounds(220,5,400,100);
        btnStart.setBounds(220,200,100,30);
        btnStop.setBounds(335,200,100,30);
        jfield.setBounds(220,100,215,30);
        http.setBounds(220,150,60,30); 
        https.setBounds(350,150,65,30); 
        
        lbl_status.setVisible(false);
        
        FRAME.setIconImage(img.getImage());
        FRAME.setSize(600,350);
        FRAME.setVisible(true);
        FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FRAME.setTitle("HTTP SERVER MAIN");
        FRAME.setLocationRelativeTo(null);
        

        group.add(http);
        group.add(https);
        
        FRAME.add(btnStart);
        FRAME.add(btnStop);
        FRAME.add(jfield);
        FRAME.add(LABEL);
        FRAME.add(lbl_server);
        FRAME.add(lbl_status);
        FRAME.add(http);
        FRAME.add(https);
        FRAME.setLayout(null);
    
    }
}

SimpleHttpServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class SimpleHttpServer {
    private int port;
    private HttpServer server;

    public void Start(int port) {
        try {
            this.port = port;
            server = HttpServer.create(new InetSocketAddress(port), 0);
            System.out.println("server started at " + port);
            server.createContext("/", new Handlers.RootHandler());
            server.createContext("/echoHeader", new Handlers.EchoHeaderHandler());
            server.createContext("/echoGet", new Handlers.EchoGetHandler());
            server.createContext("/echoPost", new Handlers.EchoPostHandler());
            server.setExecutor(null);
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void Stop() {
        server.stop(0);
        System.out.println("server stopped");
    }
}

Solution

  • You create a new SimpleHttpsServer object at the stop button listener. Please try to remove these two rows.

    SimpleHttpServer httpServer = new SimpleHttpServer();

    if(url=="http") {
        SimpleHttpServer httpServer = new SimpleHttpServer();
        lbl_status.setText("Status : http PORT " + port + " "+ "has been stopped");
        lbl_status.setForeground(Color.red);
        httpServer.Stop();
    }
    else {
        SimpleHttpsServer httpsServer = new SimpleHttpsServer();
        lbl_status.setText("Status : https PORT " + port + " "+ "has been stopped");
        lbl_status.setForeground(Color.red);
        httpsServer.Stop();
    }