If you throw null to Servlet with ajax, it will be converted to blank ""

When making a request from ajax to Servlet, null was converted to blank. specification?

sampleAjax.js



  var sampleAjax = function(){

    //request
    var request = {para : null};

    //ajax communication
    $.ajax({
      type    : "GET",
      url     : "urlSample",
      data    : request,
      async   : false,
      success : function(data) {
        console.log(data);
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert("Some kind of error occurred during the request\n" + textStatus +":\n" + errorThrown);
      }
    });

  }

servlet.java



package servlet;

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(urlPatterns={"/urlSample"})
public class SvSample extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doGet (HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

      //Parameters
      String para = req.getParameter("para");
      String para = req.getParameter("paraDummy");

      //display
      System.out.println("----");
      System.out.println(para);
      System.out.println(paraDummy);
      System.out.println("----");
    }
}

result.txt


----

null
----

Since it is blank, nothing is displayed. Parameters not in the request will be null.

Recommended Posts

If you throw null to Servlet with ajax, it will be converted to blank ""
If you dare to compare Integer with "==" ...
If you want to use Mockito with Kotlin, use mockito-kotlin
The @Rule'mActivityRule' must be public. If you convert the Espresso test to Kotlin, an error will occur.