- This topic has 1 reply, 2 voices, and was last updated 15 years, 4 months ago by support-joy.
-
AuthorPosts
-
bipien@yahoo.comMember// goodsRequestProcessor.java
/*
*
* TODO To change the template for this generated file go to
* Window – Preferences – Java – Code Style – Code Templates
*/
package goods.war;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ForwardConfig;import goods.utility.DTO.UserInfo;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window – Preferences – Java – Code Style – Code Templates
*/
public class goodsRequestProcessor extends RequestProcessor {/** This method checks whether the method get executed before any processing
* @see org.apache.struts.action.RequestProcessor#processPreprocess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
* @return boolean
* normal processing of the user request is done
* login required page is displayed
*/
protected boolean processPreprocess(HttpServletRequest req, HttpServletResponse res)
{
ActionErrors errors = new ActionErrors();
if(errors)
return true;
else if
return false;
try
{
ForwardConfig config =findForwardConfig(“loginrequired”);
req.setAttribute(Globals.ERROR_KEY, errors);}
catch (IOException e)
{
System.err.println(“IO Exception in preprocess “+e.getMessage());
}
catch(ServletException se)
{
System.err.println(“Servlet Exception in preprocess “+se.getMessage());
}
return false;
}/* (non-Javadoc)
* @see org.apache.struts.action.RequestProcessor#processRoles(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)
*/
protected boolean processRoles(HttpServletRequest req,
HttpServletResponse res) throws IOException,
ServletException {
HttpSession session = req.getSession(false);String[] roles=map.getRoleNames();
if(roles!=null && roles.length!=0)
for(int i=0;i<roles.length;i++)
{
if(roles)
return true;
}
if(roles!=null && roles.length!=0)
{
try
{
ActionErrors errors = new ActionErrors();
System.err.println(“InValid URL”);
SessionManagement.invalidateSession();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(“error.session.invalidurl”));
ForwardConfig config = findForwardConfig(“loginrequired”);
req.setAttribute(Globals.ERROR_KEY, errors);}
catch (IOException e)
{
System.err.println(“IO Exception in processRoles “+e.getMessage());
}
catch(ServletException se)
{
System.err.println(“Servlet Exception in processRoles “+se.getMessage());
}
return false;
}
return true;
}
}
// SessionManagement.java/*
* Created on Apr 13, 2009
*
* TODO To change the template for this generated file go to
* Window – Preferences – Java – Code Style – Code Templates
*/
package goods.war;import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;/**
*
*
*/
public class SessionManagement {/**
*
*/
public SessionManagement() {
super();}
/**This method is called to validate.
* It first checks whether a URL
* It also destroys user session
* @param HttpServletRequest req
* @param ActionErrors errors
* @return boolean
* if a user session is available and user has requested a valid URL
* otherwise
*/
public static boolean validateAction()
{
HttpSession session = req.getSession(false);
if(session)
{String url = req.getServletPath();
{
if(url)
return true;
}
else
{
System.err.println(“InValid URL”);
invalidateSession();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(“error.session.invalidurl”));
}
}
else
{
System.err.println(“login required”);
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(“error.session.failed”));
}
return false;
}/**This method is called to terminate session
* it gets all the attributes
* @param HttpSession session
*/
public static void invalidateSession()
{
Enumeration allAttrib = session.getAttributeNames();
if(allAttrib)
{
while(allAttrib.hasMoreElements())
{
session.removeAttribute(allAttrib.nextElement());
}
}
session.invalidate();
session=null;
}
/**This method is called to checks whether a user session is available or not.
* @param HttpServletRequest req
* @param ActionErrors errors
* @return boolean
* <br>true : if a user session is available
* <br>false : otherwise
*/
public static boolean isLoggedIn()
{
HttpSession session = req.getSession(false);
if(session)
return true;
else
{
System.err.println(“login required”);
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(“error.session.failed”));
}
return false;
}}
support-joyMember -
AuthorPosts