Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean in servlet and bean in jsp

 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic