Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to figure out the correct approach...  RSS feed

Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm new to Java and to these forums, I got the Head First servlets book and have been at it for 3 days now and have been attempting to convert some of my older non java projects and have come up against something that google hasnt been able to help with.

I'm trying to determine that correct solution to something, let me outline my application. ( JSP App, written in netbeans )

My Application has a Listener which when first started will connect to a database and place a (Connection) object in the servletcontext attributes for access through my other jsp's and servlets..

I have a simple login page which is a jsp, takes a name and password which posts to a login servlet. Which in turn validates the credentials in the database and if Ok creates an instance of a 'SiteUser' Bean and attaches that to the Session context(again using setAttribute).
A requestDispatcher then forwards the user to a "UserProfile" JSP showing the users associated data. I display this in the jsp with a <usebean> call at the top and lots of EL syntax tags "${}" to display the data on the page.

My problem i've hit is I have not yet learnt the JSTL stuff , (that comes later in my book ) But I want to see if I can solve this problem without using JSTL. Anyway I have to output a whole set of data from the database onto the page. I 'DO NOT' want to put scripting in the page, Desperately trying to stay away from that, So I thought what I could do is use a 'get' function in the "userbean" to connect to the database. that way I can use a simple EL ${} tag in the jsp. BUT here's where i'm stuck.. The bean does not have a servletContext so it cannot access the database connection. I have a Library class which i use to hold some other database access logic, it would make sense to include my routine in there also. But when i tried that i was getting Symbol not found error with "Library" and besides also it meant using scripting anyway in the jsp with a long tag such as...

out.println(Library.myFunction((Connection) application.getAttribute("DBConnection"),UserBean.getMyData()));

I have that feeling, that I may be straying from the path so i'm looking to closely at the wrong things to fix something that i shouldnt be trying to fix if only i'd have gone down the right path at the start anyway.... or something like ;-)

After trying to find some answeres on google, it seemed quite common that people setup a Bean to hold the database connection at the application level as opposed to holding it in an attribute like i have, but if I go down hat route, how would my userprofile bean access the dataconnection bean if I set one up. Can beans speak to other beans ? ( one of those questions I like to try to explain to people who have never heard any java terminology ! )

Ah well, any thoughts and advice greatly appreciated.


Posts: 17314
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Unique Dave"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it


Thanks! and welcome to the JavaRanch!

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!