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

Accessing JSP beans from a servlet

 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct in assuming that any bean defined and set in a JSP cannot be accessed in a servlet?

My form entry JSP...



In MinutesServlet...



...indexBean is null.

I just want to verify that my assumption is correct so that I can go about finding another way to extract the HTML form data.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've put the cart before the horse, collecting form info in the JSP before the form is even presented to the user.

You're also making it too complicated. Simply submit the form to the servlet and collect the data using the getParameter() family of methods on the request.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I answered my own question on my lunch break.

indexBean is null because my JSP's form action is the servlet. Any HTML submission will take the request directly to the servlet, bypassing the Javabean initialization and setProperty steps.

In order to access indexBean from the servlet I would need to make the form action the JSP page. In the JSP I would need to forward the request to the servlet. This requires the use of scriptlets which I wish
to avoid.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic