This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JSP and the fly likes How to transfer Objects from JSP to  a Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to transfer Objects from JSP to  a Servlet?" Watch "How to transfer Objects from JSP to  a Servlet?" New topic
Author

How to transfer Objects from JSP to a Servlet?

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

You can store the Employee Object in request/session/application in jsp and get it from servlet and save it.

SCJP 5, SCWCD 5
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Apr 18, 2009
Posts: 2234

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

Joined: May 24, 2010
Posts: 121

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

Joined: Feb 22, 2010
Posts: 25

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

Joined: Apr 18, 2009
Posts: 2234

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 .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to transfer Objects from JSP to a Servlet?
 
Similar Threads
Trying to access an arraylist or a cart object from a jsp page
how to call a jsp function with in a hyperlink
Data Transfer Object
How to use a jsp:useBean inside a Servlet
Using EL to retrive data from Arraylist