servlet笔记,配置与 http相关

时间:2023-03-09 20:18:39
servlet笔记,配置与 http相关

tomcat配置环境变量:        JAVA_HOME= 指向你的jdk的主目录(bin目录的上一层

server.xml:

<Context path="/myweb2" docBase="d:\web2"/>   可以把web资源路径添加到tomcat文件之外 ,等同web.xml配置;

   path:访问时输入的web名   docBaseweb资源的绝对路径

   reloadable  :如设为true ,tomcat 会自动更新 web应用;开销大,开发过程可以true,发布后应该为false

   upackWAR: 如果设为 true ,则自动解压,否则不自动解压.

servlet需要的两个包:

import javax.servlet.*;    import javax.servlet.http.*;  

    servlet-api.jar包引入需要配置环境变量,     CLASSPATH   变量值: E:\tomcat\apache-tomcat-6.0.20\lib\servlet-api.jar

web.xml:

映射servlet可以多层 <url-pattern>/servlet/index.html</url-pattern>     后缀名是 html,未必是真的html

使用通配符在servlet映射到URL中,两种格式:

第一种格式  *.扩展名  比如 *.do  *.ss                                  *.do,为任何访问地址都能访问url

第二种格式  以 / 开头 同时以 /* 结尾  比如  /*   /news/*

               匹配时的标准:     优先度高则优先被选择.     *.do的优先级最低

<load-on-startup>1</load-on-startup>   可以指定某个servlet自动创建,  字段数字为优先级

String encoding=this.getServletConfig().getInitParameter("encoding");     getServletConfig用于读取servlet的配置信息

为servlet配置参数

<servlet>

<servlet-name>ServletConfigTest</servlet-name>

<servlet-class>com.hsp.servlet.ServletConfigTest</servlet-class>

<!-- 这里可以给servlet配置信息,这里配置的信息,只能被该servlet 读取 -->

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</servlet>

<!-- 如果这里配置参数,可被所有servlet读取 -->

<!--

<context-param>

<param-name></param-name>

<param-value></param-value>

</context-param>

-->

http请求:

http1.0为短连接,http1.1为长连接;    长连接持续时间30s,短连接是发送完数据就断掉.

1.Accept: text/html,image/*   [告诉服务器,我可以接受 文本,网页,图片]

2.Accept-Charset: ISO-8859-1 [接受字符编码 iso-8859-1]

3.Accept-Encoding: gzip,compress [可以接受 gzip,compress压缩后数据.]

4.Accept-Language: en-us,zh-cn [浏览器支持中,英文]

5.Host: www.sohu.com:80 [我要找主机是 www.sohu.com:80]

6.If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT [ 告诉服务器,我的缓冲中有这个资源文件,该文件的时间是。。。]

7.Referer: http://www.sohu.com/index.jsp  [告诉服务器,我来自哪里,该消息头,常用于防止盗链]

8.User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)[告诉服务器,浏览器内核]

9.Cookie

10.Connection: close/Keep-Alive   [保持连接,发完数据后,我不关闭连接]

11.Date: Tue, 11 Jul 2000 18:23:51 GMT [浏览器发送该http请求的时间]

String referer=request.getHeader("Referer");

if(referer==null||!referer.startsWith("http://localhost:8088/servletPro")){

response.sendRedirect("/servletPro/Error");

return;

}

http的响应:

HTTP/1.1 200 OK

状态码                      含义

100-199         表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程

200-299         表示成功接收请求并完成整个处理过程,常用200

300-399             为完成请求,客户需要进行一步细化请求。例如:请求的资源已经移动一个新的地址,常用302,307

400-499         客户端的请求有错误 404

500-599         服务器端出现错误,常用500

Location: http://www.baidu.org/index.jsp  【让浏览器重新定位到url】
  Server:apache tomcat 【告诉浏览器我是tomcat】
  Content-Encoding: gzip 【告诉浏览器我使用 gzip】
  Content-Length: 80  【告诉浏览器会送的数据大小80节】
  Content-Language: zh-cn 【支持中文】
  Content-Type: text/html; charset=GB2312 [内容格式text/html; 编码gab2312]
  Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT 【告诉浏览器,该资源上次更新时间】
  Refresh: 1;url=http://www.baidu.com 【过多久去,刷新到 http://www.baidu.com】
  Content-Disposition: attachment; filename=aaa.zip 【告诉浏览器,有文件下载】
  Transfer-Encoding: chunked  [传输的编码]
  Set-Cookie:SS=Q0=5Lb_nQ; path=/search[后面详讲]
  Expires: -1[告诉浏览器如何缓存页面IE]
  Cache-Control: no-cache  [告诉浏览器如何缓存页面火狐]
  Pragma: no-cache   [告诉浏览器如何缓存页面]
  Connection: close/Keep-Alive   [保持连接 1.1是Keep-Alive]
  Date: Tue, 11 Jul 2000 18:23:51 GMT