Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: session


Message #1 by "Brandon Ng" <nodnarb_33@y...> on Tue, 28 Aug 2001 08:44:27
Hi Brandon,

Here is some idea that may help u.

Generally u need to manage sessions (session for each
use logged in) in web-besed applications.

So it is good idea to have your own session manager.

I will give you some sample code to explain this.

public class SessionManager
{
    private static SessionManager m_sessionMgr = null;
    private Hashtable m_htSessionList = null;

    public static SessionManager getInstance() 
    {
        if (m_sessionMgr  == null)
            m_sessionMgr  = new SessionManager();
        return m_sessionMgr ;    
    }
    protected SessionManager()
    {
        m_htSessionList = new Hashtable();    
    }  

    public Session login(String userId, String   
password) 
    {
        //isValidUser has code to check username
        //and password against database or LDAP
        if(isValidUser(userId.trim(),password.trim()))
        {
            Session session = new Session(userId);
            addSession(session);
            return session;
        }
        throw exception //indicating invalid login
    }
    
    public void removeSession(String userId)
    {
        m_htSessionList.remove(userId);
    }
    
    private void addSession (Session session)
    {
       
m_htSessionList.put(session.getName(),session); 
    }
    
    public Session getSession (String userName)
        throws BillingException
    {
        Session session = 
(Session)m_htSessionList.get(userName);
        if(session == null)
            throw exception //session not found
        else
            return session;
    }
}

Here SessionManager is a singleton object shared
throught the application.

Session is a object which u can use to manage other
resources like database connections and this Session
object will act as a factory for creating other
objects used in the application. 
logout() method will be in session and it will take
care of disposing the objects created for that
particular session. After logout the sesion object
will be removed from the Hashtable in SessionManager
object.

Now here is the answer for second part of your
question.

In ur login jsp, get username and password and submit
this info to validator jsp.
which will have code similar to folloeing:

<%
try
{
	SessionManager sessionMgr 
SessionManager.getInstance();
	String userName = request.getParameter("username");
	String password = request.getParameter("password");
	billing.client.Session bSession 
sessionMgr.login(userName,password);
	session.putValue("MY_SESSION",bSession);
%>
	<jsp:forward page="home.jsp"/>
<%
}
catch(LoginException e) //invalid login exception
{
	System.out.println(e.getMessage());
	session.putValue("LOGIN_EXCEPTION",e);
%>
	<jsp:forward page="errorpage.jsp">
		<jsp:param name="backlink" value="login.jsp" />
	</jsp:forward>	
<%
}
%>

I hope I have answered your question. If you have any
doubt, contact me on parag_dhumale_007@y...

Parag


> 
> Subject: session
> From: "Brandon Ng" <nodnarb_33@y...>
> Date: Tue, 28 Aug 2001 08:44:27
> X-Message-Number: 1
> 
> how to create a session for a user after he log in?
> how to ensure that user have to log in before he can
> view certain pages?

  Return to Index