less than 1 minute read

Create a struts action - LogoutAction.java
 package strutstest.actions;  
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @version 1.0
* @author
*/
public class LogoutAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
try {
// do something here
request.getSession().invalidate();
} catch (Exception e) {
// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
// Forward control to the appropriate 'failure' URI (change name as
// desired)
forward = mapping.findForward("failure");
} else {
// Forward control to the appropriate 'success' URI (change name as
// desired)
forward = mapping.findForward("success");
}
// Finish with
return (forward);
}
}

Add the action to struts-config.xml
 <action path="/logout" type="strutstest.actions.LogoutAction">  
<forward name="success" path="/logout.jsp">
</forward>
<forward name="failure" path="/error.jsp">
</forward>
</action>

Add logout link to jsp
 <html:link target="_self" page="/logout.do">logout</html:link>  

Tags:

Updated:

Comments