GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes bean in servlet and bean in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "bean in servlet and bean in jsp" Watch "bean in servlet and bean in jsp" New topic
Author

bean in servlet and bean in jsp

Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
test1
--------------------------------------------------------
when i have something like this in my doGet servlet method:

TestStdActSrv person= new TestStdActSrv();
person.setName("johny");
request.setAttribute("person", person);

and then in my jsp to which is dispatched from servlet:
<jsp:useBean id="person" class="com.TestStdActSrv" scope="request" />
<jsp:getProperty name="person" property="name" />

everythin is fine


test2
--------------------------------------
nothing in servlet :
but in jsp
<%
TestStdActSrv person= new TestStdActSrv();
person.setName("johny");
request.setAttribute("person", person);
%>
<jsp:useBean id="person" class="com.TestStdActSrv" scope="request" />
<jsp:getProperty name="person" property="name" />

i getting error
person is already defined in _jspService(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
com.TestStdActSrv person = null;

but why??? i thought the request ist request and if i set some Attribut in servlet via request.setAttribut it is the same as if i set it in jsp viarequest.setAttribut?

can somone explain this?
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi,

Without testing this, I am thinking that what the error is really saying is that you already have a variable called person defined in the jsp.

This creates a person variable
TestStdActSrv person = new TestStdActSrv();

this line kind of wants to create a second one
<jsp:useBean id="person" class="com.TestStdActSrv" scope="request" />

if you change your jsp to


it should all work

I haven't tested this so I hope I am not making a fool of myself

HTH

Mat
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
tnx Mat,
yes u r right, but i jut figured the reall reason,
looking in the translated tomcat code, which is actually very logical.
In test1 person is created once in dervlets doGet() method and
once in jsp's _jspservice method.
But in test2 both person r tryed to be instantieted in jsp's _jspservice method, which is preaty obvious thta wont work silly me
 
GeeCON Prague 2014
 
subject: bean in servlet and bean in jsp