?
Solved

Prevent user from accesing a page, if not logged in.

Posted on 2002-07-16
2
Medium Priority
?
159 Views
Last Modified: 2010-04-01

I have a html page which does a POST to a servlet with userid and password. After the servlet authenticates the information against a db, I store the user object in the session.

HttpSession session = request.getSession(true);
session.setAttribute("user", globalUser);

Then I use

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("welcome.jsp");
dispatcher.forward(request,response);

to show the welcome page.

In the Welcome page,

<jsp:useBean id="user" class="com.sbali.mydb.objects.User" scope="session"/>

<TABLE BORDER=1 ALIGN="CENTER">
<TR><TD>First Name</TD>
<TD><jsp:getProperty name="user" property="firstName"/>
</TD></TR>

I display the user info.

The problem is, how do I prevent the user from accessing  welcome.jsp directly. If I hit it directly, the server instantiates the bean and now I have a User object with no property set.

I could write a scriptlet, but then I have to put the code to show the properties using out.println() instead of using xml syntax. I want to try and keep the presentation layer separate from the logic.

I am new to JSP, so I am most probably not doing things the right way.

TIA
S Bali



0
Comment
Question by:SKBali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 200 total points
ID: 7158818
Try:

if (session.getAttribute("user")!=null) {
  //valid login user
}
else {
  //invalid login user
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7185411
typically, i hide all my jsps in the WEB-INF/jsp and disallow any outside access to that directory. Therefore the only way to get at it is to go through the servlet first.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Summer 2017 Scholarship Winners have been announced!
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question