corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Anyone good @ Programming ??


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Anyone good @ Programming ??
chris_lee100
Member

Registered: 14th Jul 02
User status: Offline
9th Jan 04 at 20:59   View User's Profile U2U Member Reply With Quote

I have a jsp page called login.jsp which posts usernames and passwords to a page called logged.jsp and checks that the user is in the database. How do I create a session in my pages so only logged in members and/or admin can see.

Any help or links would be greatly appreciated., Chriso
m-dot
Member

Registered: 17th Aug 03
Location: Warrington
User status: Offline
9th Jan 04 at 21:20   View User's Profile U2U Member Reply With Quote

code:
On a typical web site, a visitor might visit several pages and perform several interactions.
If you are programming the site, it is very helpful to be able to associate some data with each visitor. For this purpose, "session"s can be used in JSP.

A session is an object associated with a visitor. Data can be put in the session and retrieved from it, much like a Hashtable. A different set of data is kept for each visitor to the site.

Here is a set of pages that put a user's name in the session, and display it elsewhere. Try out installing and using these.

First we have a form, let us call it GetName.html

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
The target of the form is "SaveName.jsp", which saves the user's name in the session. Note the variable "session". This is another variable that is normally made available in JSPs, just like out and request variables. (In the @page directive, you can indicate that you do not need sessions, in which case the "session" variable will not be made available.)
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp.
NextPage.jsp shows how to retrieve the saved name.

<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY>
</HTML>
If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of.
The session is kept around until a timeout period. Then it is assumed the user is no longer visiting the site, and the session is discarded.

Exercise: Add another attribute "age" to the above example
Joff
Member

Registered: 17th Oct 00
Location: Cambridgeshire
User status: Offline
9th Jan 04 at 21:44   View User's Profile U2U Member Reply With Quote

Will we be tested on this later??

Laney
Member

Registered: 6th May 03
Location: Leeds
User status: Offline
9th Jan 04 at 22:10   View User's Profile U2U Member Reply With Quote

Something along the lines of:

if($username=admin)
{
admin-only content goes here

}

else
{
stuff for everyone else
}



 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
"Progress" programming environment???? Mikorsa16v Geek Day 12 1216
23rd Jan 04 at 13:01
by Mikorsa16v
 
programming!!! topshot_2k Geek Day 0 828
21st Mar 05 at 13:36
by topshot_2k
 
Windows XP Help Andrew General Chat 16 872
28th May 05 at 11:29
by CorsaChris12
 
Any programmers out there? Cosmo Geek Day 16 1806
28th Oct 05 at 16:46
by James
 

Corsa Sport » Message Board » Off Day » Geek Day » Anyone good @ Programming ?? 28 database queries in 0.0094681 seconds