aspose file tools*
The moose likes JSP and the fly likes Using beans with my jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using beans with my jsp" Watch "Using beans with my jsp" New topic
Author

Using beans with my jsp

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
I have tottally changed my approach for this jsp/servlet project, and i am now using beans and the MVC architecture. SO i am trying to login a user when they enter their username and password, and click login. My main class, which acts as my controller, is like so


So it basically request the parameters and sets them into my bean, and then forwards it to logUser.jsp. My bean is looking like so

Its the getLogin method in their i need to invoke, but i am having problems in my jsp on how to invoke it through a bean. This is my jsp as far



So i request the bean in this jsp, but then how would i invoke the getLogin method in my bean class, with the two current set variables (userName, passWord) of the bean?
any advise would save a life (atleast my poetry is getting better)
cheers
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Why are you delaying work until it gets to the JSP? You already have a page controller (research better naming than "main", and what's with the messy "mywork" stuff?) -- your work should be done there. The JSP should be to construct the view only.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
I was thinking to change the names of my files, will do that. You say that a jsp is just to construct a view, and i can understand that for what i am doing at the moment. But the reason i am working with beans is because afterwards i will be doing searches in my database and outputting the results to the jsp to be displayed in a table. This means that surely i would have to mix jsp with my java beans?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

No, it does not. All work should be done in controllers and the results passed (in beans or other constructs) to the JSP as read-only data.

All data processing should be completed before the JSP is invoked.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
So in regards to this, i am trying to add somthing like this to my controller to log someone in


The problem then is, 2 will always be returned as i am using the bean to set the username and password for a specific person, but then i am just calling up the getLogin method through an instance, which doesnt relate to whats been inserted as the username and login. I would be better off just doing somthing like
String username = request.getParameter("userName")); and then pass these variables to the method. I was just thinking it might be better to use beans.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Your code is using 1 and 2 to indicate results? Why not a boolean? "Magic numbers" are always going to cause trouble at one point or another.

Beans are useful for gathering data into a single convenient package. Don;t use them just because you can, but when it makes sense to.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price: you are familiar with this? http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using beans with my jsp