aa0660 发表于 2017-12-26 06:07:57

tomcat和HTTP

tomcat和HTTP

1.tomcat的关闭和启动
  启动:sudo /opt/tomcat/bin/startup.sh
  
关闭:sudo /opt/tomcat/bin/shutdown.sh

2.tomcat的文档目录结构

2.1.tomcat的文档结构
  tomcat根目录下:
  
|-bin:存放tomcat的命令,例如启动和关闭命令
  
|-conf:存放tomcat的配置信息,其中server.xml文件是核心的配置文件。
  
|-lib:支持tomcat运行的jar包,其中还有技术支持包,如servlet,jsp等
  
|-logs:运行过程的日志信息
  
|-temp:临时目录
  
|-work:tomcat的运行目录。jsp运行时产生的临时文件就存放在这里
  
|-webapps“共享资源目录,web应用目录(注意不能以单独的文件进行共享,要以这个项目为一个整体来共享)

2.2.webapps的文档目录结构
  WebRoot根目录下:
  
静态资源(html+css+js+image+vedio)
  
|-WEB-INF:固定写法
  
|-WEB-INF/classes:(可选)固定写法,存放class字节码文件
  
|-WEB-INF/lib:(可选)固定写法,存放jar包文件
  
|-WEB-INF/web.xml:配置文件
  
注意:
  
1.WEB-INF目录里面的资源不能通过浏览器直接访问
  
2.如果希望访问到WEB-INF里面的资源,就必须把资源配置到一个叫web.xml的文件中

3.HTTP协议

3.1.GET和POST的区别
  GET方式提交:
  
1.地址栏(URI)会跟上参数数据。以?开头,多个参数之间用&分割
  
2.GET提交参数数据有限制,不超过1kb
  
3.GET方式不适合提交敏感信息,如密码
  
4.注意:浏览器直接访问的请求,默认提交方式是GET
  POST方式提交:
  
1.参数不会跟着URI后面,而是跟在请求的实体内容中,没有?开头,多个参数之间使用&分割
  
2.POST提交的参数数据没有限制
  
3.POST方式提交敏感数据

3.2.HttpServletRequest获取请求数据
  浏览器通过Http请求获取数据信息,可是如果我们想获取这个Http请求内的信息时用什么方法呢?
  
可以使用HttpServletRequest这里类,这个类是用在服务器端的。主要包含一下一个API
  
请求行:
  
request.getMethod(); 请求方式
  
request.getRequetURI()/request.getRequetURL() 请求资源
  
request.getProtocol() 请求http协议版本
  请求头:
  
request.getHeader("名称") 根据请求头获取请求值
  
request.getHeaderNames() 获取所有的请求头名称
  实体内容:
  
request.getInputStream() 获取实体内容数据
  代码示例:
/**  
* HttpServletRequest类方法的测试
  
*/
  
@WebServlet(name = "RequestServer")

  
public>  

  /**
  
   * 这个类在创建的时候,服务器就已经帮我们做了下面两件事情,所以我们只需要获取就可以了
  
   * 1.tomcat服务器接收到浏览器发送的请求数据,然后封装到HttpServetRequest对象
  
   * 2.tomcat服务器调用doGet方法,然后把request对象传入到servlet中。
  
   * @param request
  
   * @param response
  
   * @throws ServletException
  
   * @throws IOException
  
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  /**
  
         * 3.从reques对象取出请求数据
  
         */
  

  //3.1请求行(GET /index HTTP/1.1)
  RequestLine(request);
  //3.2请求头
  RequestHeader(request);
  //3.3请求的实体内容
  RequestContent(request);
  }
  

  //3.1请求行
  private void RequestLine(HttpServletRequest request) {
  //请求的http协议
  System.out.println("请求的http协议:"+request.getProtocol());
  //请求的资源
  System.out.println("请求的资源URI:"+request.getRequestURI());
  System.out.println("请求的资源URL:"+request.getRequestURL());
  //请求的方式
  System.out.println("请求的方式:"+request.getMethod());
  }
  

  //3.2请求头
  private void RequestHeader(HttpServletRequest request) {
  //根据头名称得到头内容
  System.out.println(request.getHeader("请求头:"+"Host"));
  //得到所有的请求头名称列表
  Enumeration<String> enums = request.getHeaderNames();
  while (enums.hasMoreElements()){
  String headerName = enums.nextElement();
  System.out.println(&quot;所有请求头分别是:&quot;+enums);
  }
  

  }
  

  //3.3请求的实体内容
  private void RequestContent(HttpServletRequest request) throws IOException {
  InputStream in = request.getInputStream();
  byte[] buf = new byte;
  int len = 0;
  while ((len=in.read())!=-1){
  String str = new String(buf,0,len);
  System.out.println(str);
  }
  }
  
}

3.3.如何获取GET和POST的请求参数
  GET方式:参数放在URI后面
  
POST方式:参数放在实体内容中
  
虽然位置不一样,但是都是适用于同一个api方法。
  
request.getParameter(&quot;参数名&quot;); 根据参数名获取参数值(注意,只能获取一个值的参数)
  
request.getParameterValue(&quot;参数名“);根据参数名获取参数值(可以获取多个值的参数)
  
request.getParameterNames(); 获取所有参数名称列表

3.4.请求参数的编码问题
  由于请求参数的时候,方法会把编码解码,然后再编码,这个过程会造成乱码的现象。
  
为了解决这个问题,需要在doPost和doGet方法下先重新设置编码格式,然后再读取参数。
  
注意,重新设置编码格式必须放在请求参数之前
  
修改POST方式参数编码:
  
POST方式可以一键解码
  
request.setCharacterEncoding(&quot;utf-8&quot;);
  
修改GET方式参数编码:
  
GET解码需要对每个请求重新编码
  
手动解码:String name = new String(name.getBytes(&quot;iso-8859-1&quot;),&quot;utf-8&quot;);
  示例代码:
/**  
* 测试如何获取GET和POST的请求参数
  
*/
  
@WebServlet(name = &quot;RequestArgs&quot;)

  
public>  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //接收POST方式接收的参数
  /*
  
      System.out.println(&quot;POS提交的参数&quot;);
  
      InputStream in = request.getInputStream();
  
      byte[] buf = new byte;
  
      int len = 0;
  
      while ((len=in.read())!=-1){
  
            System.out.println(new String(buf,0,len));
  
      }
  
    */
  //正确做法:统一方便的索取请求参数的方法
  request.setCharacterEncoding(&quot;utf-8&quot;);
  this.doGet(request,response);
  

  }
  

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //接收GET方式提交的参数
  /*
  
      //笨方法
  
      String value = request.getQueryString();
  
      System.out.println(value);
  
    */
  //正确做法:统一方便的索取请求参数的方法
  //根据参数名得到参数值,只能获取一个值的参数
  String name = request.getParameter(&quot;name&quot;);//返回name参数值
  //编码
  if (&quot;GET&quot;.equals(request.getMethod())){
  name = new String(name.getBytes(&quot;iso-8859-1&quot;),&quot;utf-8&quot;);
  }
  

  String passw = request.getParameter(&quot;passwd&quot;);//返回passwd参数值
  if (&quot;GET&quot;.equals(request.getMethod())){
  passw = new String(passw.getBytes(&quot;iso-8859-1&quot;),&quot;utf-8&quot;);
  }
  System.out.println(name+&quot;=&quot;+passw);
  //根据参数名获取多个同名参数
  request.getParameterValues(&quot;hobit&quot;);
  

  //得到所有的参数
  Enumeration<String> enums = request.getParameterNames();
  while (enums.hasMoreElements()){
  String paramName = enums.nextElement();
  String paramValue = request.getParameter(paramName);
  if (&quot;GET&quot;.equals(request.getMethod())){
  paramValue = new String(paramValue.getBytes(&quot;iso-8859-1&quot;),&quot;utf-8&quot;);
  }
  System.out.println(paramName+&quot;=&quot;+paramValue);
  }
  

  }
  
}

4.HTTP响应

4.1.状态码
  每一个HTTP响应都有一个状态码来标识服务器处理这个请求的结果是什么样的
  
常见的状态码有:
  
100~199:表示成功接收请求,要去客户端继续提交下一次请求才能完成整个处理过程
  
200~299:表示成功接收请求并完成整个处理过程,常用200表示请求完美处理,并返回
  
300~399:为完成请求,需进一步细化请求,例如请求的资源已经移动一个新的地址。常用302
  
400~499:客户端的请求有错误,常用404表示请求的资源找不到
  
500~599:服务器出现错误

4.2.HttpServletResponse修改HTTP响应信息
  HttpServletResponse对象修改响应信息:
  
响应行
  
response.setStatus() 设置状态码
  
响应头
  
response.setHeader(&quot;name&quot;,&quot;value&quot;) 设置响应头
  
实体内容
  
response.getWriter().writer(); 发送字符实体内容
  
response.getOutputStream().writer() 发送字节实体内容
  
代码示例:
/**  
*测试 HttpServletResponse对象修改响应信息
  
*/
  
@WebServlet(name = &quot;ResponseDemo&quot;)

  
public>  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  

  }
  

  /**
  
   * 在调用doGet方法之前,服务器已经做了一下操作
  
   * 1.tomcat服务器把请求信息封装到HttpServervletRequest对象,且把响应信息封装到HttpServletResponse
  
   * 2.tomcat服务器调用doGet方法,传入request,response
  
   * @param request
  
   * @param response
  
   * @throws ServletException
  
   * @throws IOException
  
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  

  /**
  
         * 3.通过response对象改变响应信息
  
         */
  //3.1.响应行
  
//      response.setStatus(404);//只修改状态码,不改变页面
  
//      response.sendError(404);//发送404状态码+404错误页面
  

  //3.2.响应头
  response.setHeader(&quot;server&quot;,&quot;JBos&quot;);//改变服务器类型
  

  //3.3.实体内容(浏览器直接能够看到的内容就是实体内容)
  //方法1:
  response.getWriter().write(&quot;hello World&quot;);//字符内容
  //方法2:
  response.getOutputStream().write(&quot;hello World&quot;.getBytes());//字节内容
  }
  /**
  
   * 4.tomcat服务器把response对象内容自动转换成响应格式内容,再发送给浏览器解析
  
   */
  
}

4.3.案例分析
  请求重定向
/**  
* 请求重定向
  
*/
  
@WebServlet(name = &quot;RodirectDemo&quot;)

  
public>  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  /**
  
         * 需求:跳转到adv.html
  
         * 使用请求重定向:发送一个302状态码+localtion的响应头
  
         */
  //方法1:
  response.setStatus(302);
  response.setHeader(&quot;location&quot;,&quot;/adv.html&quot;);
  //方法2:
  response.sendRedirect(&quot;/adv.html&quot;);
  

  }
  
}
  定时刷新
/**  
* 定时刷新
  
*/
  
@WebServlet(name = &quot;RefreshDemo&quot;)

  
public>  

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  /**
  
         * 定时刷新
  
         * 原理:浏览器认识refresh头,得到refresh头之后重新请求当前资源
  
         */
  //每隔1秒刷新一次
  response.setHeader(&quot;refresh&quot;,&quot;1&quot;);
  //每隔3秒跳转到另外的资源
  response.setHeader(&quot;refresh&quot;, &quot;3;url=/adv.html&quot;);
  }
  
}
  content-Type作用
  
Content-Type: text/html;--表示服务器发送给浏览器的数据类型及内容编码
/**  
* content-Type作用
  
*/
  
@WebServlet(name = &quot;ContentTypeDemo&quot;)

  
public>  

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  /**
  
         * 1.服务器发送给浏览器的数据类型
  
         */
  response.setContentType(&quot;text/html&quot;);
  //等价于上面,推荐使用上面的
  response.setHeader(&quot;content-type&quot;,&quot;text/html&quot;);
  response.setContentType(&quot;image/jpg&quot;);
  }
  
}
页: [1]
查看完整版本: tomcat和HTTP