Home > Archive > Java Help > February 2007 > Testing for a User in a Session (JSP/Servlet)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Testing for a User in a Session (JSP/Servlet)
|
|
| llama64 2007-02-21, 7:16 pm |
| Hi all,
I'm really struggling trying to figure out how best to approach a
problem I have.
My project is a simple web application that requires user
authentication. To accomplish this, the first page is a login JSP
that forwards a user's credentials to a loginCheck servlet. Upon
approval, it forwards to the main JSP of the app.
If a user has already logged in (User bean object created and in the
Session), I want it to redirect all requests to the main JSP rather
then the login JSP.
I created a tag to handle this but I ran into an endless loop. Basic
logic for checking:
UserBean user = (UserBean)
pageContext.getSession().getAttribute("user");
try
{
if (user != null)
{
RequestDispatcher dis =
pageContext.getRequest().getRequestDispatcher("welcome.jsp");
dis.forward(pageContext.getRequest(),
pageContext.getResponse());
}
} catch (Exception e)
{
e.printStackTrace();
}
return (SKIP_BODY);
Can anyone help me with the logic here? I want this tag to be usable
on all pages of the system, but I want to avoid the endless
redirecting that occurs when you get logged in and load "welcome.jsp".
Thanks for your help!
| |
|
| llama64 wrote:
> My project is a simple web application that requires user
> authentication. To accomplish this, the first page is a login JSP
> that forwards a user's credentials to a loginCheck servlet. Upon
> approval, it forwards to the main JSP of the app.
>
> If a user has already logged in (User bean object created and in the
> Session), I want it to redirect all requests to the main JSP rather
> then the login JSP.
>
> I created a tag to handle this but I ran into an endless loop. ...
> Can anyone help me with the logic here? I want this tag to be usable
> on all pages of the system, but I want to avoid the endless
> redirecting that occurs when you get logged in and load "welcome.jsp".
I suggest looking into the Model-View-Controller paradigm for Web
applications. In this model, JSPs never handle navigation directly, they send
a request for navigation to a controller servlet, which uses a dispatch table
or similar mechanism to match the request, the permission and the target view.
So your code would move out of the tag library, where it is hard to debug,
into the controller servlet, where it is much easier.
This is the basis of the Struts framework.
- Lew
| |
| llama64 2007-02-21, 7:16 pm |
| On Feb 21, 8:46 am, Lew <l...@nospam.lewscanon.com> wrote:
> llama64 wrote:
>
>
>
> I suggest looking into the Model-View-Controller paradigm for Web
> applications. In this model, JSPs never handle navigation directly, they send
> a request for navigation to a controller servlet, which uses a dispatch table
> or similar mechanism to match the request, the permission and the target view.
>
> So your code would move out of the tag library, where it is hard to debug,
> into the controller servlet, where it is much easier.
>
> This is the basis of the Struts framework.
>
> - Lew
Thanks Lew!
I'm "sorta" following the MVC as much as possible, but I'm still in
the process of learning all of this Java technology.
If I understand you correctly:
- initial request comes in, a servlet is given the request first
(rather then a index.html/jsp file).
- The servlet then checks the conditions I need (user is logged in),
and forwards the request to the proper page.
All requests get intercepted by a servlet to make decisions. Am I on
the right track?
| |
|
| llama64 wrote:
> Thanks Lew!
>
> I'm "sorta" following the MVC as much as possible, but I'm still in
> the process of learning all of this Java technology.
>
> If I understand you correctly:
> - initial request comes in, a servlet is given the request first
> (rather then a index.html/jsp file).
> - The servlet then checks the conditions I need (user is logged in),
> and forwards the request to the proper page.
>
> All requests get intercepted by a servlet to make decisions. Am I on
> the right track?
Yes.
There was an article in JavaPro Online (not the print edition) entitled "Who
Needs Struts?" that I wrote a year or two ago. I don't know if you can still
find it on there, but it might help get you started. (It might require a free
(as in beer) signup first.)
Typically there is a single controller servlet that mediates all requests. It
will parse out enough of the request parameters to figure out which business
logic class (usefully, a JavaBean) to invoke, then it passes the request to
the logic. Based on a result code it then selects a new JSP to display, one
which might be able to refer to the logic bean or values that it generated.
That is how results get to show up in the next screen.
It's a little like Swing, where JTable is the visual display component for a
TableModel, which is a non-visual data object.
There are variation on this idea. Wikipedia, Sun and Google are good places to
research it, and Struts is a decent framework if your project is large enough
to justify it.
- Lew
|
|
|
|
|