Servlet retrieve Object

1

Is it possible for a servlet to retrieve populated object in the requestParameter?

Type coming from Javascript?

req.getParameter("objeto");

I have the Person object that has

Name

Phone

in Javascript I submit

var obj = {nome: 'Carlos', telefone: '929999999'}

How to retrieve this populated object in Servlet instead of String

Obj obj = req.getParameter("obj");
    
asked by anonymous 22.01.2017 / 15:31

1 answer

0

According to your description, you should be sending this object via AJAX .

var obj = {nome: 'Carlos', telefone: '929999999'}
$.ajax("url", {
  data: obj,
  method: "POST"
  success:function(data){
          console.log(data);
  }
}

In your servlet, just grab this object using the getParameter() method available on the object HttpServletRequest .

It would look like this

doPost(HttpServletRequest request, HttpServletResponse response) {
  String nome = request.getParameter("nome");
  String telefone = request.getParameter("telefone");

  response.getWriter().write("Nome submetido foi: " + nome + "\n Telefone submetido foi: " + telefone);
}

I hope I have helped. Any questions post in the comments.

    
22.01.2017 / 20:33