从java浏览器获取消息到HTTP服务器

时间:2016-01-20 09:47:35

标签: java http server

我试图在java中编写一个服务器,它将为注册学生提供一种数据库。只需要照顾"添加" "除去"和" getinfo"。

我通过浏览器在" http://localhost/students/"

中建立服务器访问权限

现在,我尝试通过输入以下命令向服务器发送请求: http://localhost/students/add?id=12324&name=Israel以色列和性别=男性和年级= 90

我想知道如何抓住这条消息来解析它并用它做我需要的一切,我试着使用套接字,但我不知道我是否做得对。 这是服务器的代码:

import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;

public class Httpserver {

public static RequestHandler _handler;

@SuppressWarnings("static-access")
public Httpserver(){
    this._handler = new RequestHandler("welcome");
}
public static void setUp()  {
    HttpServer server;
    try {
        server = HttpServer.create(new InetSocketAddress(80), 40);
        server.createContext("/students",_handler);
        server.setExecutor(null); // creates a default executor
        server.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}
}

这是运行所有内容的主应用程序的代码:

public class Manager {

private Server _server;
private ArrayList<Student> _students;
private Parser _parser;

public Manager(){
    _server = new Server();
    _students = new ArrayList<Student>();
    _parser = new Parser();
}

@SuppressWarnings("static-access")
public void setUpServer(){
    _server.start();

    try {
        //ServerSocket server = new ServerSocket(0);
        Socket getRequests = new Socket("localhost", 80);
        BufferedReader buff =new BufferedReader(new InputStreamReader      (getRequests.getInputStream()));
        PrintWriter out1 =
                new PrintWriter(getRequests.getOutputStream(), true);
        DataOutputStream out = new DataOutputStream(getRequests.getOutputStream());
        BufferedReader stdIn =
                new BufferedReader(
                    new InputStreamReader(System.in));
        String  line;
        while (true){
            line =  buff.readLine();    
            out1.println(line);
            if (line == null){
                out.writeBytes("Connection ended");
                break;
            }
            int response = _parser.parseRequest();
            switch (response){
            case 1: handleAdd(_parser.getParams(), out);

            case 2: handleRemove(_parser.getParams(), out);

            case 3: handleGet(_parser.getParams(), out);
            }

        }
        getRequests.close();
    } catch (IOException e) {
        //STODO Auto-generated catch block
        e.printStackTrace();
    }



}
private void handleGet(ArrayList<String> params, DataOutputStream out) {
    // TODO Auto-generated method stub

}

private void handleRemove(ArrayList<String> params, DataOutputStream out) {
    // TODO Auto-generated method stub

}

private void handleAdd(ArrayList<String> params, DataOutputStream out) throws IOException {
    if (params == null || params.get(0) == "" ){
        out.writeBytes("id is mandatory for creating new student");
        return;
    }
    Student s;
    if (params.size() == 4){
        s = new Student(Long.parseLong(params.get(0)),
                params.get(1), params.get(2), Double.parseDouble(params.get(3)));
    }else{
        s = new Student(Long.parseLong(params.get(0)));
    }
    _students.add(s);
    out.writeBytes("student: " + s.getId() + " was added to the system");

}



public static void main(String[] args){
    Manager m = new Manager();
    m.setUpServer();
}

}

并非所有内容都已实现,我只想弄清楚如何从浏览器捕获消息并将响应发送回浏览器。 感谢

0 个答案:

没有答案