Servlet recover Object

Asked

Viewed 139 times

1

It is possible to recover populated object in requestParameter?

Type coming from Javascript?

req.getParameter("objeto");

I have the object Person who possesses

Name

Telephone

in Javascript I send

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

How to recover this populated object in Servlet instead of String

Obj obj = req.getParameter("obj");
  • Could you improve your question? Can’t understand what you want to achieve.

  • Yes, of course

1 answer

0

According to your description, you must 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 Rvlet, just pick up this object through the method getParameter() available in the object HttpServletRequest.

Would something 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 helped. Any doubt post in the comments.

  • the request.getParameter(), will always receive String then, right?

  • Always. If you want to send another type of data, you should go deeper and search for Part, Multipart etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.