zoukankan      html  css  js  c++  java
  • ServletContext

     
     
     
     
    1). 可以由  SerlvetConfig 获取:
     
    1. ServletContext servletContext = servletConfig.getServletContext();
     
     
     
    2). 该对象代表当前 WEB 应用: 可以认为 SerlvetContext 是当前 WEB 应用的一个大管家. 可以从中获取到当前 WEB 应用的各个方面的信息.
     
    ①. 获取当前 WEB 应用的初始化参数
     
    设置初始化参数: 可以为所有的 Servlet 所获取, 而 Servlet 的初始化参数只用那个 Serlvet 可以获取. 
    1. <!--配置当前 WEB 应用的初始化参数-->
    2. <context-param>
    3. <param-name>driver</param-name>
    4. <param-value>com.mysql.jdbc.Driver</param-value>
    5. </context-param
    6. >
     
     
    方法:
     
    getInitParameter
    getInitParameterNames
     
    代码:
     
    1. ServletContext servletContext = servletConfig.getServletContext();
    2. String driver = servletContext.getInitParameter("driver");
    3. System.out.println("driver:"+ driver);
    4. Enumeration<String> names2 = servletContext.getInitParameterNames();
    5. while(names2.hasMoreElements()){
    6. String name = names2.nextElement();
    7. System.out.println("-->"+ name); 
    8. }
     
     
    ②. 获取当前 WEB 应用的某一个文件在服务器上的绝对路径, 而不是部署前的路径
     
    getRealPath(String path);
     
    代码:
     
    1. String realPath = servletContext.getRealPath("/note.txt");
    2. System.out.println(realPath);
     
     
    ③. 获取当前 WEB 应用的名称: 
     
    getContextPath()
     
    代码:
     
    String contextPath = servletContext.getContextPath();
    System.out.println(contextPath); 
     
    ④. 获取当前 WEB 应用的某一个文件对应的输入流. 
     
    getResourceAsStream(String path): path 的 / 为当前 WEB 应用的根目录. 
     
    代码:
     
    InputStream is2 = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
     
    ⑤. 和 attribute 相关的几个方法:
     
    9. GET 请求和 POST 请求:
     
    1). 使用GET方式传递参数:
     
    ①. 在浏览器地址栏中输入某个URL地址或单击网页上的一个超链接时,浏览器发出的HTTP请求消息的请求方式为GET。 
    ②. 如果网页中的<form>表单元素的 method 属性被设置为了“GET”,浏览器提交这个FORM表单时生成的HTTP请求消息的请求方式也为GET。 
    ③. 使用GET请求方式给WEB服务器传递参数的格式:  
     
    http://www.atguigu.com/counter.jsp?name=lc&password=123
     
    ④. 使用GET方式传送的数据量一般限制在 1KB 以下。 
     
    2). 使用 POST 方式传递参数:
     
    ①. POST 请求方式主要用于向 WEB 服务器端程序提交 FORM 表单中的数据: form 表单的 method 置为 POST
    ②. POST 方式将各个表单字段元素及其数据作为 HTTP 消息的实体内容发送给 WEB 服务器,传送的数据量要比使用GET方式传送的数据量大得多。  
     
    POST /counter.jsp HTTP/1.1
    referer: http://localhost:8080/Register.html
    content-type: application/x-www-form-urlencoded
    host: localhost:8080
    content-length: 43
     
    name=zhangsan&password=123              --请求体中传递参数. 
     
    10. 如何在 Serlvet 中获取请求信息:
     
    1). Servlet 的 service() 方法用于应答请求: 因为每次请求都会调用 service() 方法
     
    public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException
     
    ServletRequest: 封装了请求信息. 可以从中获取到任何的请求信息.
    ServletResponse: 封装了响应信息, 如果想给用户什么响应, 具体可以使用该接口的方法实现. 
     
    这两个接口的实现类都是服务器给予实现的, 并在服务器调用 service 方法时传入. 
     
    2). ServletRequest: 封装了请求信息. 可以从中获取到任何的请求信息.
     
    ①. 获取请求参数: 
     
    > String getParameter(String name): 根据请求参数的名字, 返回参数值. 
    若请求参数有多个值(例如 checkbox), 该方法只能获取到第一个提交的值. 
     
    > String[] getParameterValues(String name): 根据请求参数的名字, 返回请求参数对应的字符串数组. 
     
    > Enumeration getParameterNames(): 返回参数名对应的 Enumeration 对象, 
    类似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法. 
     
    > Map getParameterMap(): 返回请求参数的键值对: key: 参数名,  value: 参数值, String 数组类型. 
     
    ②. 获取请求的 URI:
     
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
     
    String requestURI = httpServletRequest.getRequestURI();
    System.out.println(requestURI); //  /day_29/loginServlet
     
    ③. 获取请求方式: 
     
    String method = httpServletRequest.getMethod();
    System.out.println(method); //GET
     
    ④. 若是一个 GET 请求, 获取请求参数对应的那个字符串, 即 ? 后的那个字符串. 
     
    String queryString = httpServletRequest.getQueryString();
    System.out.println(queryString); //user=atguigu&password=123456&interesting=game&interesting=party&interesting=shopping
     
    ⑤. 获取请求的 Serlvet 的映射路径 
      
       String servletPath = httpServletRequest.getServletPath();
       System.out.println(servletPath);  //  /loginServlet
       
    ⑥. 和 attribute 相关的几个方法:   
     
    3). HttpServletRequest: 是 SerlvetRequest 的子接口. 针对于 HTTP 请求所定义. 里边包含了大量获取 HTTP 请求相关的方法. 
     
    4). ServletResponse: 封装了响应信息, 如果想给用户什么响应, 具体可以使用该接口的方法实现. 
     
    ①. *getWriter(): 返回 PrintWriter 对象. 调用该对象的 print() 方法, 将把 print() 中的参数直接打印
    到客户的浏览器上. 
     
    ②. 设置响应的内容类型: response.setContentType("application/msword");
     
    ③. void sendRedirect(String location): 请求的重定向. (此方法为 HttpServletResponse 中定义.)





  • 相关阅读:
    移动端小tips
    是否该放弃东莞的工作
    读书有感-learn html5 and javascript for ios
    eNSP多路由实现互联互通(华为路由E口直连)
    iTextSharp操作表格排版问题
    我今天开通博客
    12年的女程序员重新回归
    前端css样式规划
    前端神器-webstorm2017
    远程桌面资料共享
  • 原文地址:https://www.cnblogs.com/nearor/p/4525411.html
Copyright © 2011-2022 走看看