This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes How to get the bean object in servlet from jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSP
Bookmark "How to get the bean object in servlet from jsp? " Watch "How to get the bean object in servlet from jsp? " New topic

How to get the bean object in servlet from jsp?

Mukund Pande

Joined: May 21, 2003
Posts: 5
<%@ page language="java" %>
<%@ page import="EmpBean" %>
<jsp:useBean id="user" class="EmpBean" scope="session">
<jsp:setProperty name="user" property="*"/>
<form method=post action=/abacus/servlet/SampleServlet>
<input type="text" name="empcode"><br>
<input type="text" name="empname"><br>
<input type="submit" value="submit">
I have the above jsp page which is submitted to the SampleServlet. How to pass the bean object "user" which is set in the Jsp to the servlet?
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 231

I don't see any reason why your servlet should'nt be able to access the said bean like any other regular class. Just use the beans instance to access the properties you had set using the JSP.

SCJP2, SCWCD 1.4, PMP, ITIL Foundation
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Hi Mukund,
u have defined the scope of the bean as session in the jsp hence in the servlet u can access it from the seeion object by using
EmpBean e = (EmpBean)session.getAttribute("user");
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13001
Problem: you don't have the bean class in a package.

You will waste a lot of time chasingn weird errors if you don't put the bean class in a package. In addition to using the package name in the useBean tag:

you will have to import the package in both the JSP and the servlet.
Have you checked out Aspose?
subject: How to get the bean object in servlet from jsp?
It's not a secret anymore!