File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting JSP bean from Servlet

 
Steven Marco
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If in the JSP I have:

<jsp:useBean id="test" class="MyTest" scope="request" />

In the Servlet I have:

MyTest test = new MyTest();
MyTest myTest= (MyTest)request.getattribute("test");


However "myTest" will be empty when running, what's wrong with the above? Thanks.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the request getting from the jsp to the servlet (usually the other way around or it is a different request scope)? Show us the jsp code.

But to get a request parameter use request.getParameter("test") not getAttribute().
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't quite solve your problem, but it might help you find out what IS in your request.

This little chunk of code is priceless:




If you use this, you can loop through all the attributes, or parameters in your request, and see what's in there. It can eliminate spelling mistakes and stuff pretty quickly.

Cheers!

-Cameron McKenzie
 
Donna Bachner
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem. I have a JSP and want to call the Servlet also. I have a link I want to take to the servlet. I can go to another JSP fine but I can't get the resulets I want. From the jsp I was using a a href to the other jsp but it does not work for the servlet. Any ideas?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about session scoped attributes or hidden variables ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Donna Bachner:
I have a similar problem. I have a JSP and want to call the Servlet also. I have a link I want to take to the servlet. I can go to another JSP fine but I can't get the resulets I want. From the jsp I was using a a href to the other jsp but it does not work for the servlet. Any ideas?


Once the response for the first JSP is sent to the browser, that request goes out of scope so any scoped variabled placed on it will not be available for a new request generated by clicking on a link in the page.

For a scoped variable to be available to multiple requests, you must use session scope (for user-specific data) or application scope (for application-shared data).

You could also use hidden variables on the page to store information that will be submitted back to the next request.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Marco:
If in the JSP I have:

<jsp:useBean id="test" class="MyTest" scope="request" />

In the Servlet I have:

MyTest test = new MyTest();
MyTest myTest= (MyTest)request.getattribute("test");


However "myTest" will be empty when running, what's wrong with the above? Thanks.


Two things:

1.)
See these two links to understand why it's important that your beans be packaged:
http://faq.javaranch.com/view?PackageYourBeans
http://faq.javaranch.com/view?BeansNotFound
They're short.

2.)
Are you forwarding, redirecting, or getting from the JSP to the servlet by drawing the webpage and providing a link?
Of the above, only the first will allow you to use request scope.
For the others, you'll need to use session scope.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Donna Bachner:
I have a similar problem. ...


Donna,
It would be better for you to start a new thread with your problem.
Unless you're issue has the same cause as the original poster's, any help you get here will detract from the original question and result in a hijacked thread.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic