• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Getting JSP bean from Servlet

 
Greenhorn
Posts: 21
  • 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.
 
drifter
Posts: 1364
  • 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().
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • 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
 
Ranch Hand
Posts: 109
  • 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?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about session scoped attributes or hidden variables ?
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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 VI Editor Redhat
  • 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.
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic