退出功能与BaseServlet抽取

退出功能

  

什么叫做登录了?session中有user对象。

实现步骤:

  1. 访问servlet,将session销毁
  2. 跳转到登录页面

 

 

 

 

 

 登录成功

 

 

 

 

 

 

 

 

 退出成功

 

代码实现:

     <span id="span_username"></span>
                <a href="myfavorite.html" class="collection">我的收藏</a>
                <a href="javascript:location.href='exitServlet';">退出</a>
            </div>

 

package cn.itcast.travel.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/exitServlet")
public class ExitServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.销毁session
        request.getSession().invalidate();
            //2.跳转登录页面
        response.sendRedirect(request.getContextPath()+"/login.html");

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

BaseServlet抽取

      优化Servlet

        减少Servlet的数量,现在是一个功能一张Servlet,将其优化为一个模块一个Servlet,相当于在数据库中一张表对应一个Servlet,在Servlet中提供不同的方法,完成用户的请求 

 

 

 

 

 

 

·

 

 

谁调用我?我代表谁

 

 

 

package cn.itcast.travel.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/user/*") ///user/add/user/find
public class UserServlet extends BaseServlet {



    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("userServlet的add方法");
    }


    public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("userServlet的find方法");

    }
}

 

 

package cn.itcast.travel.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/category/*")
public class CategoryServlet extends BaseServlet {


    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CategoryServlet的add方法");
    }


    public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CategoryServlet的find方法");

    }
}

 

package cn.itcast.travel.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/baseServlet")
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*System.out.println("baseServletd servlice的方法被执行了......");*/

        //完成方法分发
        //1.获取请求路径
        String uri = req.getRequestURI();   //  /user/add
        System.out.println("请求uri:"+uri); // /user/add

        //2.获取方法名称
        String methodName = uri.substring(uri.lastIndexOf('/') + 1);
        System.out.println("方法名称:"+methodName);
        //3.获取方法对象Method
        //谁调用我?我代表谁
        System.out.println(this);//UserServlet的对象  cn.itcast.travel.web.servlet.UserServlet@5e344e00

        try {
                //获取方法
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //4.执行方法
            //暴力反射
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }
}

 

posted @ 2023-03-01 14:45  zj勇敢飞,xx永相随  阅读(17)  评论(0编辑  收藏  举报