Forums Register Login
Beans in servlets?
Hi, I have a JSP which collects user information from another JSP, this information is passed into a bean object(with scope="application")

I have a separate JSP, which displays this bean information, using:


But how could I do this in a servlet, since jsp:useBean, and jsp:getProperty are designed for JSP's ???

[ August 05, 2005: Message edited by: colin shuker ]

[Bear edit: removed shouting from subject]
[ August 05, 2005: Message edited by: Bear Bibeault ]
The scopes available to the JSP page are all available to a servlet with the exception, of course, of page scope.

To retrieve a scoped variable from any of the scopes: request, session or application, you would use the getAttribute() method on that scope.

For example: request.getAttribute()

Since these methods return Object, you usually need to cast the result to the appropriate type.

Oh, and "application" scope really means the servlet context.
[ August 05, 2005: Message edited by: Bear Bibeault ]
OK, but I don't get it.
How would I achieve the same result, as used with the code in the JSP??
First, some advice. Become familiar with customary Java naming conventions. Not following the naming conventions that everyone else uses makes your code surprisingly difficult to read. A class should always start with a capital letter; so, the bean.info class becomes bean.Info.

Now, if in your JSP page you have

the useBean action does a number of things. But if we just focus on the fetching of the scoped variable from application scope (you should be careful not to make typos when posting code), the equivalent "fetch" code within a servlet would be:

[ August 05, 2005: Message edited by: Bear Bibeault ]
Thanks, that makes sense, I understand the casting thing. I'll try it out now.
Ok, I just tried the code above.
I inserted the code into the doGet method of a servlet, but it
says getAttribute(...) is a non-static method being reference from a
static context?

So I'm not sure, does it go in init()??

Show your code. I'm guessing that you are calling getAttribute() incorrectly.
OK, heres my servlet (Spreadsheet.java)
[ August 05, 2005: Message edited by: colin shuker ]

By using the ServletContext class name as the prefix, you have told the compiler to make a static call. And since getAttribute is not static, you get the compiler error.

You need to make the call on the instance of the servlet context.

Notice in my example I used "servletContext" not "ServletContext". This is an example of the naming convention I mentioned earlier. The samll 's' indicates a variable. The uppercase 'S' indicates a class name.

To obtain the servlet context within the doGet or doPost of a servlet, you can use:

OK, thanks, that works, before I thought you had missed off the capital S, but you hadn't, you just didn't mention that I had to create the object servletContext.
Thanks very much!!!
I'm almost there now, my next problem is basically that each bean that is made needs its properties displayed in the servlet(excel), (first name, last name, etc.)
I'll have to have a think about this, not sure at the moment, any thoughts are much appreciated.

Thanks again
I've hit another problem if anyone is interested.
My JSP (Validate.jsp) collects user information from another JSP, then it creates a bean.

My servlet(Spreadsheet.java) now recieves the bean, and its properties are displayed.

The problem is, the servlet only displays the bean, when the page is loaded, so if many users enter information, the single bean I'm using will just keeping changing, and only the latest version will be displayed when Spreadsheet.java is executed.

I'm not keen on using multiple beans, and them letting the servlet display the properties for all of them when it is loaded, for 2 reasons:
1. I would need to name each bean object, seperately
2. This may in principle require thousands of beans.

I would like the bean (after its properties have been set) to have its properties stored in an array perhaps, in the servlet.
Is this approach possible??? any thoughts are welcome.
Just to be sure you understand. When you retrieve a session from a request, then store an object in it using a name (i.e. "userBean"), then you retrieve a session from a different request sent from another user and store it using the name "userBean" you do NOT overwrite the userBean from the previsouly mentioned session. Session are unique per user. Therefore you can store all your user beans using "userBean" and retrieve all you user beans using "userBean" and you will always have the particular instance of userBean for the user who sent you the request.
Just a little more advice on naming conventions, you should name your servlet class SpreadsheetServlet.
This looks like a job for .... legal tender! It says so right in this tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 992 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 19, 2018 17:12:15.