• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a pojo in a Servlet

 
Vinod Dubey
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use pojo class variable in servlet.?
I have a pojo class
class details
{
String firstName;
String lastName;
here is setter & getter method
}
this variable i want to use in servlet. how can i use in servlet & set the value from HTML FORM
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are just like any other Java class, so you can create and initialize the pojo just like you would in any other Java class.

You can obtain the values submitted form a form via the request.getParameter() and related methods.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However - very important - note that multiple people/requests can be processed "at the same time" by your servlet.

Therefore you can not keep a reference to your POJO as an instance variable in the servlet code - every request would be trying to use the same object. A common error by beginning servlet programmers.

Instead you must use the session mechanism to keep separate references to the POJO for each request.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Instead you must use the session mechanism to keep separate references to the POJO for each request

Or a local variable, or request scope, or even application scope when appropriate.

But yes, never use instance variables in a servlet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!