tags:

views:

562

answers:

4

Let me just start out by saying I am completely new to Java web apps, so I'm sorry if this is a very basic question.

I am writing an app that has the ability for users to login to an account. The top part of each page contains an info bar (kind of like the top bar in GMail) with something like:

Logged in as <userid> | Settings | Help

Since that part is going to be on every page, it makes sense to me to split that functionality out so I do not duplicate my code.

I did some searching, and found that I could use this:

<jsp:include page="userInfoBar.jsp" flush="true" />

in the middle of another JSP. I got into problems for my userSettings.jsp page though, which also gets some User detail from the database. Not only am I duplicating some code between the 2 JSPs, but I have a feeling that I am doing double the work during the render of the web page.

For example (Pseudo code):

userInfoBar.jsp:
user = getCurrentUser
if ( user ) {
  print Logged in as user.userid | Settings | Help
}

userSettings.jsp:
<jsp:include page="userInfoBar.jsp" flush="true" />
user = getCurrentUser
Change settings for user.userid

So in that little example, getCurrentUser gets called twice (I think, unless Java somehow optimizes that).

How can I avoid this duplication of work, but still keep the userInfoBar somewhat separate from each JSP?

Thanks in advance!

Brian

+3  A: 

The problem is that you have java code in your JSPs. Most evil things happen when you do that. You should look up the MVC pattern on google.

Anyway, the good way to do this would be to use the MVC pattern, and do your heavy lifting in the controller, after the heavy lifting is done, you can pass the strings/lists to display the view, which displays them and NOTHING MORE.

Also, if you start using MVCs with JSTL, a lot more people will be able to help you, because everyone knows how to do that stuff, and hopefully not many people still do inline java in their JSPs

mkoryak
Cool - I'm familiar with MVC because of Ruby on Rails and Django - I just wasn't sure how to make that happen in Java. I'm looking it up now :)
BrianH
Reading up on JSTL - that looks like the way to go. Looks pretty different, so I'll be studying this for a little while. Thanks much for the help!
BrianH
+2  A: 

Try using JSF. This allows for easy implementation of MVC.

You can point two front-ends to the same session-scoped bean to get user information.

Zack
+2  A: 

You can also use jspf (JSP fragment files) to store chunks of JSP.

jsp:include is one way to include a JSP.

Another way is to use c:include from the JSTL libraries.

The differences are covered here: * http://sharat.wordpress.com/2006/09/08/40-what-is-the-difference-between-jspinclude-and-cimport/

Hope that helps. I'm sure there are framework specific approaches, but these are the two JSP approaches I know of.

Jon
A: 

Well, for a small project with a few pages I think it's ok to do plain jsp and it's not a bad idea to get a hang of it.

I can assure you that any framwork will add much more "unneccesary" code thats called over and over again, but you won't notice as it's hidden behind the abstractions.

(I can't really recommend a decent html framework for java, but maybe GWT is the most promising or at least the least painful. At least I get to write real debuggable code, in a decent editor...)

KarlP