HTTP method is non-idempotent
In HTTP, non-idempotent methods are those that can produce different results when the same request is executed multiple times. This means that repeating the request may cause different effects on the server.
Table of Contents
Non Idempotent HTTP Method
The most common idempotent HTTP method is POST.
- POST Method:
- Used to submit data to the server, such as submitting a form or uploading a file.
- Each request may result in different outcomes, such as creating new resources or causing other changes on the server.
- For example, submitting the same form multiple times may create multiple entries in the database.
Example in Java
Let’s create a Java example that uses a POST request to submit data to a server. We’ll use the HttpURLConnection class to send the POST request.
Server-Side Code:
Java Example
1. Server-Side Code:
- Create a simple servlet that handles POST requests and stores data.
java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/submitData")
public class SubmitDataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = request.getParameter("data");
// Simulate storing data
System.out.println("Data received: " + data);
response.getWriter().write("Data received: " + data);
}
}
Client-Side Code:
2. Client-Side Code:
- Use HttpURLConnection to send a POST request to the servlet.
java
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpPostExample {
public static void main(String[] args) {
try {
// URL of the servlet
URL url = new URL("http://localhost:8080/your_webapp/submitData");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set up the connection
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Data to be sent in the POST request
String data = "data=SampleData";
// Send the POST request
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
// Get the response
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Server-Side Servlet
- The servlet listens at the /submitData URL.
- It handles POST requests by reading the data parameter from the request and simulates storing the data (prints it to the console).
Client-Side Code
- The client sets up an HttpURLConnection to the servlet’s URL.
- The request method is set to POST, and the content type is specified.
- Data is sent in the request body, and the connection is made.
- The response code from the server is printed to the console.