• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
huangui
博客园    首页    新随笔    联系   管理    订阅  订阅
重定向和请求转发

重定向

重定向的路径名需要写项目名(路径名需要写完整)

重定向是HttpServletResponse对象的方法。

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        System.out.println(username+":"+password);
        //重定向
        resp.sendRedirect("/r/success.jsp");
    }

请求转发

转发的路径名不需要写项目名(只需要写页面名字即可)

请求转发是HttpServletRequest的方法

 

 

  @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String[] hobbys=req.getParameterValues("hobbys");
        System.out.println("=================================");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));
        System.out.println("=================================");
//   请求转发
        req.getRequestDispatcher("/success.jsp").forward(req,resp);
        resp.setCharacterEncoding("utf-8");
    }//

 

posted on 2020-04-27 15:53  huangui  阅读(157)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3