zoukankan      html  css  js  c++  java
  • Servlet跳转

    方便自己查询,嫌低级的勿喷。。。。

    在Servlet中跳转有两种:

    1.客户端跳转

    在Servlet中要进行客户端跳转(地址栏的地址信息将发生改变),直接使用HttpServletResponse接口的sendRedirect()方法,注意此跳转只能传递session及application范围的属性,而无法传递request范围的属性。

    2.服务器端跳转

    在Servlet中没有像JSP中的<jsp:forward>指令,所以如果要执行服务器端跳转(地址栏的地址信息不发生改变),就必须依靠RequestDispatcher接口完成,此接口中提供了下面两种方法:

    No 方法 描述
    1 public void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面跳转
    2 public void include(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面包含

    使用RequestDispatcher接口的forward()方法即可完成跳转功能的实现,但是如果要想使用此接口还需要使用ServletRequest接口提供如下的方法进行实例化:

    No 方法 描述
    1 public RequestDispatcher getRequestDispatcher(String path) 取得RequestDispatcher接口实例
    package org.lxh.servletdemo ;
    import java.io.* ;
    import javax.servlet.* ;
    import javax.servlet.http.* ;
    public class ServerRedirectDemo extends HttpServlet {
        public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
            req.getSession().setAttribute("name","李兴华") ;
            req.setAttribute("info","MLDNJAVA") ;
            RequestDispatcher rd = req.getRequestDispatcher("get_info.jsp") ;    // 准备好了跳转操作
             rd.forward(req,resp) ;    // 完成跳转
        }
        public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
            this.doGet(req,resp) ;
        }
    }
  • 相关阅读:
    微服务-分布式事务解决方案
    Python cannot import name 'Line' from 'pyecharts'
    powershell 基础
    Linux SSH config
    神奇的Python代码
    GitHub 中 readme 如何添加图片
    Linux 笔记(自用)
    Anaconda 安装 TensorFlow ImportError:DLL加载失败,错误代码为-1073741795
    Ubuntu 分辨率更改 xrandr Failed to get size of gamma for output default
    Git入门教程 Git教程入门
  • 原文地址:https://www.cnblogs.com/mjsh/p/3205433.html
Copyright © 2011-2022 走看看