简体   繁体   中英

Java get parameters from post request

I am currently trying to handle some incoming POST requests in Java.

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:

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.

I am using java 1.8 in eclipse btw.

However, I can find the classes HttpExchange and HttpHandler , but can't seem to make them work.

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.

Then, study Servlets (extends HttpServlet), you can use its GET and POST method.

Here is the 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.

If you are looking to handle HTTP requests it would be best practice to use a servlet container such as 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. 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 . One you have the servlet container stood up and a Servlet declared and mapped, its as simple as calling the HttpServletRequest#getParameter() method.

There is indeed a HttpServer class, it is found somewhere within the sun.net.httpserver package.

The sun package and subpackages are safe to use, unlike the com.sun package and subpackages, which may be (re)moved.

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. Certain things are not necessary and you can just throw a 501 Not Implemented at them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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