Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
Langkiller Avatar asked Dec 08 '25 19:12

Langkiller


1 Answers

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.

like image 114
shepard23 Avatar answered Dec 11 '25 09:12

shepard23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!