Servlet - doPut() Example

[Last Updated: Aug 27, 2017]

An HTTP PUT request is used to store the enclosed entity at the requested URI. If the requested URI refers to an already existing resource then it may be modified otherwise it should be stored at the new URI.

The difference between POST and PUT is that, POST creates a resource without defining the new resource path. whereas, PUT creates a new resource by defining complete resource path.

In Java Servlet, HttpServlet#doPut() method is overridden to handle a PUT request. Let's see an example to see how to do that.


In this example, we will send a PUT request from the web browser via JQuery.


<h2>Create New User</h2>
<form id="user-form">
            <td> User id:</td>
            <td><input type="text" name="userId"/></td>
            <td> Name:</td>
            <td><input type="text" name="name"/></td>
            <td> email:</td>
            <td><input type="text" name="email"/></td>
    <input type="submit" value="Submit"/>

<div id="msg"></div>

            var $form = $(this);
            var userId = $form.find('input[name="userId"]').val();
            var url = 'http://localhost:8080/users/'+userId;
            var userName = $form.find('input[name="name"]').val();
            var userEmail = $form.find('input[name="email"]').val();

                type : 'PUT',
                url : url,
                contentType: 'application/json',
                data : JSON.stringify({name: userName, email: userEmail}),
                success : function(data, status, xhr){
                error: function(xhr, status, error){
                $('#msg').html('<span style=\'color:red;\'>'+error+'</span>')


The Servlet

@WebServlet(name = "userServlet", urlPatterns = {"/users/*"})
public class UserServlet extends HttpServlet {
  protected void doPut(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      int userId = retrieveUserid(req);
      String body = inputStreamToString(req.getInputStream());
      System.out.println("body: " + body);
      UserDataService.Instance.saveUserById(userId, body);

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      int userId = retrieveUserid(req);
      String user = UserDataService.Instance.getUserById(userId);
      PrintWriter writer = resp.getWriter();

  private static int retrieveUserid(HttpServletRequest req) {
      String pathInfo = req.getPathInfo();
      if (pathInfo.startsWith("/")) {
          pathInfo = pathInfo.substring(1);
      return Integer.parseInt(pathInfo);

  private static String inputStreamToString(InputStream inputStream) {
      Scanner scanner = new Scanner(inputStream, "UTF-8");
      return scanner.hasNext() ? scanner.useDelimiter("\\A").next() : "";
public enum UserDataService {
  private Map<Integer, String> dataMap = new HashMap<>();

  public void saveUserById(int id, String data){
      dataMap.put(id, data);
  public String getUserById(int id){
      return dataMap.get(id);

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Submitting PUT request at 'http://localhost:8080':

Example Project

Dependencies and Technologies Used:

  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Servlet Do Put Example Select All Download
  • put-request-method-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • webapp

    See Also