简体   繁体   English

Java从发布请求中获取参数

[英]Java get parameters from post request

I am currently trying to handle some incoming POST requests in Java. 我目前正在尝试处理Java中的一些传入POST请求。

It is working partly so far. 到目前为止,它正在部分工作。 I can receive the requests, but I can't seem to extract the parameters and values sent with the request. 我可以收到请求,但是似乎无法提取与请求一起发送的参数和值。

Here is my Java code for receiving the requests so far: 这是到目前为止我接收请求的Java代码:

public HybridRemoteReceiver() {

try {
    System.out.println("running...");
    ServerSocket ss = new ServerSocket(3434);

    int i = 0;
    while (true) {
        Socket client = ss.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String line;

        while ((line = in.readLine()) != null) {
            if (line.length() == 0)
                break;
            i++;
        }
        in.close();
        client.close();
    }


} catch (Exception e) {
    e.printStackTrace();
}

}

I am aware that there is a class called HttpServer , but It's like this class doesn't exist in Java. 我知道有一个名为HttpServer的类,但这就像Java中不存在此类。

I am using java 1.8 in eclipse btw. 我在Eclipse BTW中使用Java 1.8。

However, I can find the classes HttpExchange and HttpHandler , but can't seem to make them work. 但是,我可以找到HttpExchangeHttpHandler类,但似乎无法使它们正常工作。

Basically what I want to achieve, is to receive the data sent with the post request and NOT send anything back to the client. 基本上,我要实现的是接收与发布请求一起发送的数据,而不将任何内容发送回客户端。

I really hope anyone can help me out with this. 我真的希望有人能帮助我解决这个问题。

Use a servlet container like tomcat. 使用Tomcat容器之类的servlet容器。

Then, study Servlets (extends HttpServlet), you can use its GET and POST method. 然后,研究Servlet(扩展HttpServlet),可以使用其GET和POST方法。

Here is the POST. 这是POST。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        username = request.getParameter("username");

} }

//username is a parameter that was sent via POST from a JSP. //用户名是从JSP通过POST发送的参数。

If you are looking to handle HTTP requests it would be best practice to use a servlet container such as Tomcat. 如果要处理HTTP请求,最好的做法是使用Servlet容器,例如Tomcat。 Tomcat will not implement the entire Java EE specification but will provide you with an implementation of a servlet container, which will be able to handle the post request easily. Tomcat不会实现整个Java EE规范,但是会为您提供servlet容器的实现,该容器将能够轻松处理post请求。 This keeps you from reinventing the wheel and allows you to rely upon a proven technology incorporated into many projects. 这使您不必重新发明轮子,而使您可以依赖于已纳入许多项目的成熟技术。

Once the servlet containers libraries are on the classpath, you will then be able to use classes such as HttpServletRequest . 一旦servlet容器库位于类路径上,您就可以使用HttpServletRequest类的类。 One you have the servlet container stood up and a Servlet declared and mapped, its as simple as calling the HttpServletRequest#getParameter() method. 您只需站起来一个Servlet容器,然后声明并映射一个Servlet ,其操作就像调用HttpServletRequest#getParameter()方法一样简单。

There is indeed a HttpServer class, it is found somewhere within the sun.net.httpserver package. 确实有一个HttpServer类,可以在sun.net.httpserver包中的某个位置找到它。

The sun package and subpackages are safe to use, unlike the com.sun package and subpackages, which may be (re)moved. com.sun软件包和子软件包不同, sun软件包和子软件包可以安全使用。

Edit: If you want to, you can always write your own server. 编辑:如果愿意,您始终可以编写自己的服务器。 If you wish to do so, make sure it follows all the RFCs that define the HTTP version you'll be using. 如果您希望这样做,请确保它遵循定义您将使用的HTTP版本的所有RFC。 Certain things are not necessary and you can just throw a 501 Not Implemented at them. 某些事情不是必需的,您可以向其抛出501未实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM