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应用程序。

  • 相关阅读:
    CMD指令
    六种Socket I/O模型幽默讲解
    性格与职业的选择
    为什么主引导记录的内存地址是0x7C00?
    pandas数据分析第二天
    pandas数据结构和介绍第一天
    tornado options
    tornado.web.StaticFileHandler
    mysql多条更新
    pandas
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9957643.html
Copyright © 2011-2022 走看看