Make a custom tag like so :



Create a tld file with this in  it :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd";>
<taglib>
      <tlibversion>1.0</tlibversion>
      <jspversion>1.1</jspversion>
      <shortname>Tag Libs</shortname>
      <info>ustom Tags</info>
      <tag>
            <name>checkLogin</name>
            <tagclass>com.cpaglobal.cpadirect.tag.CheckLogin</tagclass>
            <bodycontent>empty</bodycontent>
            <info>Checks the login status of a user and forward them to the
login page if not logged in.</info>
            <attribute>
                  <name>loginPage</name>
                  <required>false</required>
                  <rtexprvalue>false</rtexprvalue>
            </attribute>
            <attribute>
                  <name>default</name>
                  <required>false</required>
                  <rtexprvalue>false</rtexprvalue>
            </attribute>
      </tag>
</taglib>



Code for the custom tag :

package com.cpaglobal.cpadirect.tag;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.cpaglobal.cpadirect.applicationlogic.SessionVariables;
import com.cpaglobal.cpadirect.applicationlogic.User;


public class CheckLogin extends TagSupport {


      private String loginPage = "/JSP/Timeout.jsp";


      public void setLoginPage(String loginPage) {
            this.loginPage = loginPage;
      }

      public int doStartTag() throws JspException {
         return (SKIP_BODY);
      }

      public int doEndTag() {
            try {
                  HttpSession session = pageContext.getSession();
                  if (session != null) {
                        User user = (User)
session.getAttribute(SessionVariables.SESSION_LOGGED_ON_USER);
                        if (user == null) {
                              pageContext.forward(loginPage);
                              return (SKIP_PAGE);
                        }
                  } else {
                        pageContext.forward(loginPage);
                        return (SKIP_PAGE);
                  }
            } catch(ServletException e) {
                  new JspException(e.toString());
            } catch(IOException e) {
                  new JspException(e.toString());
            }
            return (EVAL_PAGE);
      }

}

How to use it :

<%@ taglib uri="/WEB-INF/cpa.tld" prefix="cpa"%>
<cpa:checkLogin/>

Mike




|---------+---------------------------->
|         |           Caroline Jen     |
|         |           <[EMAIL PROTECTED]|
|         |           .com>            |
|         |                            |
|         |           03/11/2003 06:23 |
|         |           AM               |
|         |           Please respond to|
|         |           "Struts Users    |
|         |           Mailing List"    |
|---------+---------------------------->
  
>------------------------------------------------------------------------------------------------------------------------------|
  |                                                                                    
                                          |
  |       To:       [EMAIL PROTECTED]                                                  
                             |
  |       cc:                                                                          
                                          |
  |       Subject:  Session Times Out                                                  
                                          |
  
>------------------------------------------------------------------------------------------------------------------------------|




I check if session expires for each action in the
application.  If the session times out, I forward the
user to index.jsp so that the user can log on again.

How do I inform the user with a message that he/she is
at the welcome page because the session has expired?

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







********************************************************************************
The information in this message is confidential and may be legally
privileged. It is intended solely for the addressee; access to this
email by anyone else is unauthorised.

If you are not the intended recipient: (1) you are kindly requested
to return a copy of this message to the sender indicating that you
have received it in error, and to destroy the received copy; and (2)
any disclosure or distribution of this message, as well as any action
taken or omitted to be taken in reliance on its content, is prohibited
and may be unlawful.
********************************************************************************


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to