zoukankan      html  css  js  c++  java
  • Servlet接口

    ServletRequest接口

    ServletRequest的对象用于向Servlet提供客户端请求信息,如内容类型,内容长度,参数名称和值,标题信息,属性等。
    

      

    RequestDispatcher接口

    RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
    //servlet2 is the url-pattern of the second servlet  
    rd.forward(request, response);//method may be include or forward
    

    Servlet重定向

    HttpServletResponse接口的sendRedirect()方法可以用于将响应重定向到另一个资源,资源可能是servlet,jsp或html文件。
    
    它接受相对和绝对URL。
    
    它在客户端工作,因为它使用浏览器的URL栏来发出另一个请求。 所以,它可以在服务器内部和外部工作
    

    response.sendRedirect("http://www.yiibai.com");
    
    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String keyword=req.getParameter("keyword");
            resp.sendRedirect(("http://www.yiibai.com/search.php?kw=" + keyword));
        }
    

    Servlet ServletConfig配置信息(单个Servlet可以获到初始化的参数)

     

        getServletConfig()方法的示例

     

    ServletConfig config=getServletConfig();  
    //Now we can call the methods of ServletConfig interface
    

    为servlet提供初始化参数的语法

    servlet的init-param子元素用于指定servlet的初始化参数。

     

    <web-app>
      <servlet>
        ......
        <init-param>  
          <param-name>parameter_name</param-name>
          <param-value>parameter_value</param-value>
        </init-param>
        ......
      </servlet>
    </web-app>
    

     

    获取所有初始化参数的ServletConfig示例

    ServletContext(所有Servlet都可以获到的初始化的参数)

    //We can get the ServletContext object from ServletConfig object  
    ServletContext application=getServletConfig().getServletContext();  
    
    //Another convenient way to get the ServletContext object  
    ServletContext application=getServletContext();
    

      

    <web-app>  
     ......  
      <context-param>  
        <param-name>parameter_name</param-name>  
        <param-value>parameter_value</param-value>  
      </context-param>
     ......  
    </web-app>
    

    Servlet属性设置

    ServletConfig和ServletContext的区别

    servletconfig对象引用单个servlet,而servletcontext对象引用整个Web应用程序。

  • 相关阅读:
    ceph pool 管理
    python 创建一个实例:步骤二 添加行为方法,编写方法
    python 创建一个实例:步骤一 编写一个构造函数
    Ceph集群rbd-mirror A、B区域备份实施方案
    python 函数中的递归、lambda 、map reduce 等详解
    reduce python 的用法
    python 搜集参数的共有项和所有项
    argument python 参数 举例
    The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)
    hdu 4614 Vases and Flowers(线段树+二分)
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9957643.html
Copyright © 2011-2022 走看看