zoukankan      html  css  js  c++  java
  • Servlet中的常用类以及常用方法

    A:ServletConfig:用于读取Servlet在web.xml中配置的一些信息。

    getServletName();
    getInitParameter();只能是Servlet自身下的参数设置。
    getInitParameterNames();

    B:ServletContext对象:用于操作Servlet的上下文环境的对象。

    //获取上下文路径
    context.getContextPath();

    //获取上下文初始化参数
    context.getInitParameter(name);
    context.getInitParameterNames();

    注意:servletContext中的getInit与ServletConfig中的getInit的区别。

    servletContext获取是全局的配置参数
    <context-param>
    <param-name>global_i</param-name>
    <param-value>10</param-value>
    </context-param>
    servletConfig获取是Servlet局部的配置参数。
    <servlet>
    <servlet-name>TestConfigServlet</servlet-name>
    <servlet-class>com.servlet.TestConfigServlet</servlet-class>

    <init-param>
    <param-name>xmlFile</param-name>
    <param-value>/data/userinfo.xml</param-value>
    </init-param>

    WEB程序中:
    都是用于读取WebRoot下面的路径。

    context.getRealPath(path);
    context.getResource(path);
    context.getResourceAsStream(path);

    //操作作用域对象的方法
    context.setAttribute(name, object);
    context.getAttribute(name);
    context.removeAttribute(name);

    C:ServletInputStream/ServletOutputStream

    用于接收客户端发送的二进制流和往客户端响应二进制流。

    主要用在文件上传与下载方面。


    D:ServletRequest/HttpServletRequest对象:
    // 用于获取服务端的一些信息。
    String localAddr = request.getLocalAddr();
    String localName = request.getLocalName();
    int localPort = request.getLocalPort();
    String serverName = request.getServerName();
    int serverPort = request.getServerPort();


    // 用于获取客户端的信息
    String remoteAddr = request.getRemoteAddr();
    String remoteHost = request.getRemoteHost();
    int remotePort = request.getRemotePort();
    String user = request.getRemoteUser();


    // 用于获取路径的方法
    String contextPath = request.getContextPath();
    //用于获取地址栏中?后面的内容。
    String queryString = request.getQueryString();
    String uri = request.getRequestURI();
    StringBuffer url = request.getRequestURL();

  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/ziq711/p/6252060.html
Copyright © 2011-2022 走看看