用户验证,错误异常处理,这个看似小事,但有时候处理也比较麻烦,随时有可能处理不好!
一,用户验证处理
- 通过自定义标签直接添加在需要用户验证的页面
- 通过覆盖struts 的RequestProcessor的processRole(),从而获得用户验证功能.
- 通过编写一个Action验证基类,子类继承而获得用户验证功能.
1)自定义标签实现验证
java 代码
- public final class CheckLogin extends TagSupport{
- public int doStartTag() throws JspException{
- return (SKIP_BODY);
- }
- public int doEndTag() extends JspException{
- HttpSession session=pageContext.getSession();
- String userName=sesssion.getAttribute("USERNAME");
- if(userName==null){
- pageContext.forward("xxxxx.jsp") ; //错误页面
- return (SKIP_PAGE);
- }
- return (EVAL_PAGE);
- }
2)struts 的RequestProcessor的processRole(),从而获得用户验证功能
java 代码
- protected boolean processRoles (ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException{
- HttpSession session=request.getSession();
- String userName=session.getAttribute("USERNAME");
- if(userName==null){
- RequestDispatcher rd=request.getDispathcher("XXXX.jsp");//返回错误页面
- rd.forward(request,response);
- return false;
- }
- return true;
- }
3)编写一个Action验证基类,子类继承而获得用户验证功能.
java 代码
- public abstract class CheckBaseAction extends Action{
- public final ActionForward execute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- HttpSession session=request.getSession();
- String userName=session.getAttribute("USERNAME");
- if(userName==null){
- return (mapping.findForward("login"));
- }
- return doExecute(mapping,request,response);
- }
- public abstract ActionForward doExecute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException ;
- }
第三种灵活性比前2种强点,但与struts耦合比较强,或者可以使用另一种filter来实现用户检验也不错的选择!
二.异常处理
- 通过Servlet异常处理,只要简单地在Web.xml里配置异常处理类型和返回页面
- 通过Struts的异常处理机制,在struts-config.xml配置局部异常和全局异常
- 通过编写一个Action 异常基类,子类继承而获得异常处理功能
1) Servlet异常处理
xml 代码
- <error-page>
- <error-code>404error-code>
- <location>XXX.jsplocation>
- error-page>
- <error-page>
- <exception-type>java.lang.NullPointerExceptionexception-type>
- <location>XXX.jsplocation>
- error-page>
2)Struts的异常处理机制,在struts-config.xml配置局部异常和全局异常
xml 代码
- <global-exceptions>
- <exception type="java.lang.exception"
- key="action.errors.exception"
- path="/error.jsp"/>
- global-exceptions>
- <action-mappings>
- <action path="/Login"
- type="cn.com.sqicoo.LoginAction"
- name="LoginForm"
- input="/login.jsp">
- <exception type="java.lang.exception"
- key="action.errors.exception" >
- <forward name="success" path="/login_success.jsp"/>
- action>
- action-mappings>
3)编写一个Action 异常基类,子类继承而获得异常处理功能
java 代码
- public abstract class ErrorBaseAction extends Action{
- public final ActionForward execute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- try{
- return doExecute(mapping,form,request,response);
- }catch(Exception ex){
- return doException(ex,mapping,form,request,response);
- }
- }
- public ActionForward doException(Exception ex,
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- // 异常处理
- return mapping.findForward("XXX");//返回页面
- }
- public abstract ActionForward doExecute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException;
- }