Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to transfer Objects from JSP to a Servlet?

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am displaying the Employee Records from DataBase into a JSP
I mean the Employee means , its a bean with properties as EMPID , EMPNAME , EMPAGE , EMPDEPT .

I am making a query to Database from my servlet and when data is stored i am storing all the records in a ArrayList , and forwarding this ArrayList to a JSP Page to print them .

Upto here its working fine .

After dipslaying this Employee Data inside the JSP Page , the user can modify the Employee details and clicks on save it should update the Modified data in Database , here i need to transfer the whole modified DTO of Employee Object .

Please tell me how can i transfer the Employee Object to servlet from my JSP Page ??

Please share your ideas , thnaks in advance .

Thanks in advance .

 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store the Employee Object in request/session/application in jsp and get it from servlet and save it.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the form submit goes to a servlet, not to a JSP? If it currently does go to a JSP, then you should rewrite it to use a servlet instead - JSPs are part of the view layer, they should not contain any logic or even DB access code.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store the Employee Object in request/session/application in jsp and get it from servlet and save it.


I didn't get the approach you are suggesting .

After making chnages and clicks on Update Button , i have a function named as updateEmployee() inside my jsp

Now please tell me how can i send this Modified EMployee DTO into this function ??

function updateEmployee()
{

// Need your help here ??
}

Thanks inn advnace .
 
Jagdeep Sharma
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:
You can store the Employee Object in request/session/application in jsp and get it from servlet and save it.


I didn't get the approach you are suggesting .

After making chnages and clicks on Update Button , i have a function named as updateEmployee() inside my jsp

Now please tell me how can i send this Modified EMployee DTO into this function ??

function updateEmployee()
{

// Need your help here ??
}

Thanks inn advnace .



I think you should reframe your question. As we all know JSP is RESPONSE. Means once the response has been arrived to the browser, it's been treated as HTML by the browser. So in my opinion you can't pass Object from JSP to Servlet. Because at the browser end we can only talk about html not jsp.

 
Ian Cabigon
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I didn't get the approach you are suggesting .

After making chnages and clicks on Update Button , i have a function named as updateEmployee() inside my jsp

Now please tell me how can i send this Modified EMployee DTO into this function ??

function updateEmployee()
{

// Need your help here ??
}

Thanks inn advnace .



Looking at the code, I'd say updateEmployee() is a javascript function. Maybe inside this function is where we try to send the data to the server using forms. And in the jsp/servlet end, we parse the sent data and translate it to the intended javabeans.

Maybe something like this:



But somethings not quite right here..you should'nt use JSP for your parsing and logic.. In MVC, JSPs are for view only. So might as well move your logic inside a servlet.

Thanks,
Ian
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , i have found an another way for doing this .

May be helpful to some others with same requirement .

While display data , i am displaying them inside the TextFileds and when submit button is made the form is mapped to an appropiate servlet , which takes care of submitting data to Database .

Thanks for your help .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic