Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User Login Page

 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I am newbie to JSP Programming.
I am doing a Project using JSP, Servlets and Beans.
As a starting point i would like to design a "User Login" page which would allow only Users in the Database.
I designed it entirely using JSP and static HTML pages.
But later i learned that's not an efficient way to program.
Then i came across the MVC Design. Hence i would like to implement to the User Authentication as per the MVC.
Can somebody help me in this???
like what should Bean do (Business Logic)??
Where to open Connection to the Database??
How to handle Connections between Pages?? etc.
Waiting for Replies
Sathish
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't open any connections to the database - instead use a connection pool provided by the web container (you can get a hold of the pool through JNDI, see your vendor docs for details).
For the login screen, you can use simple declarative security constraints in web.xml. You need to declare
- which URL patterns are protected (e.g. "/protected/*")
- which roles are allowed access to the protected areas (roles are mapped to real users by the web container)
- which JSP page acts as your login screen
I'd suggest doing a quick Google for more detailed instructions.
 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No here what i have to do is i have a Table named Users in my Database.
I have to open a Connection to it and check whether the User is an Authorised one or not!!??
I want to design it in the MVC Pattern.
Am struggling about where to put what???
Help me
Sathish
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Here's the classical solution:
Login.jsp
- Contains the HTML form for username/password
LoginServlet
- Reads the username/password from HTTP(S) request
- Gets a connection to the database
- Tries to find a match for username/password
- If successful, puts a User object into HttpSession and forwards the request to Main.jsp
- If not successful, forwards the request back to Login.jsp
Main.jsp (or whatever)
- Includes a small JSP, which checks for the existence of a valid User object in the session
- If the User object is missing, forwards the request to Login.jsp (and invalidates the HttpSession)
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Lasse,
Sorry if im wrong but i think u were talking abt the JSP Architecture-1, not the MVC architecture,
u didnt mention anything abt the Model (java Bean) in the ur classical example..
I think it shld go like this
Login.jsp (View)
LoginServlet.java (Controller )
for getting the username and password
parameters from the jsp page
calling the model and redirecting to the next
process
CheckBean.java (Model)
Used to connect/access the database values (if u r not using connectionpool)
an instance of this bean is used by the LoginServlet for checking the username and pw..
is this the right MVC architecture ?? guide me if I'm wrong !!!
Thanks,
raj

SCJP2
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're correct Raj, the CheckBean would be the Model.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sarthishari Kumar:
I want to design it in the MVC Pattern.

Go for struts, its easy. I learned in 1 day [I know its too much but my one day includes 80% in MD and 20% work ] so you will obviously take less time than me
And I am 100% sure you will get login MVC as struts example somewhere on net. And if I am not wrong you get this example as part of struts.zip from Apache
 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for ur Suggestion.
So as per what u have said, Database Connection is opened in Model(CheckBean).
If that's the Case then u need to close the Connection after validating the User.
But i thought that it would be better if Connection is opened when the Servlet is initialised so that v need not open Connection to the Database everytime there is an Access to it.
I mean, open the Connection by calling a Bean Method that opens a connection to the Database.
This call being made in the init() method of the Servlet.
Use that connection to validate the user in the same Bean.
This would avoid reopening the Connection again if another user tries to login after that.
Is it possible???
Also what is Connection Pooling??
Waiting for ur Valuable Comments.
[ April 23, 2003: Message edited by: Sarthishari Kumar ]
 
Bhushan Jawle
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree Struts will be better option as you will find you will be involved in writing similar code for each of your web application, go for Struts instead
 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for ur Replies.
Where can i get Struts!!???
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sarthishari Kumar:
But i thought that it would be better if Connection is opened when the Servlet is initialised so that v need not open Connection to the Database everytime there is an Access to it.

...and that's why you should use a connection pool (you never "close" a connection, you just release it back into the pool of available connections).

Also what is Connection Pooling??

Connection pooling means that there are a small number of open connection objects in a virtual pool from which application threads request a connection. After using the connection, the application threads return the connection object to the pool for others' to use. This helps you by removing the significant open/close overhead and by removing the responsibility of connection management in general from your application code.
Connection pools are provided/configured by the web container (or appserver) thru JNDI. See your vendor's documentation - there is almost always an example of connection pooling.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sarthishari Kumar:
Where can i get Struts!!???

http://jakarta.apache.org/struts
 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Thanx for ur Reply.
Have downloaded Struts.
Am using Sun One Studio and Tomcat for development.
I read the documentation of Struts.
It has asked to copy to files and to make changes to web.xml File.
I don't know what it is.??
Can u help me on this.
Sathish
 
Rajeev Ravindran
Ranch Hand
Posts: 455
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sarthishari Kumar:
...to make changes to web.xml File.
I don't know what it is?

web.xml is a J2EE configuration file, a "deployment descriptor" for web applications. It's placed into the WEB-INF directory and specifies things like which servlets the webapp contains, which URLs map to which servlets, which URLs are protected (require authentication/login), etc.
See the J2EE Tutorial at java.sun.com for a more detailed introduction.
 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Am using Sun One Studio for Development. Can i use Struts in Sun One itself.
It contains the Tomcat 4.0 Server embedded in it.
Where should i create the Directories for Struts.
Or i should download Stand-alone Tomcat Server and then start using Struts.
Sathish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic