`
Mr.Zero
  • 浏览: 33093 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

head first 学习笔记 JSP&Servlet--2

    博客分类:
  • java
 
阅读更多
6.servletConfig 和ServletContext之间的区别
  6.1 servletconfig:
      从一个servlet被实例化后,对所有客户端在所有时候访问有效,但仅对本servlet 有效,一个servlet的servletconfig对象不能被另一个servlet访问。
  6.2 servletcontext:
      对所有servlet,所有人在所有时间都有效,这才是真正全局的对象。
  6.3 用处
     如果是整个系统的配置的话,那么就可以设置为servletContext的参数,如系统编码等
    <web-app>
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                        classpath:bme.service.xml
                        /WEB-INF/conf/global.datasource.xml
                        /WEB-INF/conf/*.service.xml /WEB-INF/conf/*.web.xml
                        /WEB-INF/console/conf/*.web.xml
                        /WEB-INF/console/conf/*.service.xml
                        /WEB-INF/frameset/conf/*.web.xml
                        /WEB-INF/frameset/conf/*.service.xml /WEB-INF/*.web.xml
                        classpath:org/codehaus/xfire/spring/xfire.xml
                        classpath:cmp/spring/cmp-spring-integration-startup.xml
                        classpath:iManager/spring/repository_service.xml
                </param-value>
            </context-param>
            ..........
         </web-app>

         若是某个单独servlet的配置项的话,可以设置为servletConfig的参数,如某个读取文件的servlet的目录地址等
         <servlet>
                <servlet-name>getatt</servlet-name>
                <servlet-class>mail.getattservlet</servlet-class>
                <init-param>
                  <param-name>abspath</param-name>
                  <param-value>/usr/mail/ax/axman/maildir/</param-value>
                </init-param>
         </servlet>
         只能这个servlet能获取这些参数,不同servlet之间不能获取
         
    实际上ServletContext的比ServletConfig的使用范围广很多, ServletConfig一般只用于获取Servlet的初始化参数,而ServletContext的作用是与Web Container交互,除了可以用于获取Web Application的全局初始化参数外,还可以获取Servlet的元数据,设置共享属性,获取属性值,获取外部资源等,另外当Web Application使用分布式部署时(在web.xml 中使用distributable元素)ServletContext的作用范围就只有单个虚拟机而不是整个Web Application

7.servlet中的幂等请求
    get,head,put是被认为幂等请求,也就是多个完全相同的请求过来时只当做一个请求来处理,非幂等的请求则是当做多个请求来处理。
    即get从服务器获得数据,不会更新数据,post会更新数据库信息。这个就是为什么在用get方式请求服务器的时候会带上一个无效的时间参数,来让服务器来将最新的数据返回。


8.getServerPort(),getLocalPort(),getRemotePort()区别
 
  8.1 getRemotePort() 获得远程客户端端口
  8.2 getServerPort() 获得请求原来发送的端口
  8.3 getLocalPort()  获得请求最后发送的端口
因为servlet会处理多个请求,每个请求会为每个线程找一个不同的端口


1. response 对象

   一般用于向客户端返回请求。设置需要返回内容类型
一个典型的读取jar包的输出返回

package com.xzm.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContentTypeServlet extends HttpServlet {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException
        {
            //设置定返回的对象类型,告诉浏览器你要返回的数据类型
            resp.setContentType("application/jar");
           
            //根据系统级的参数获取读取文件资源的方式
            ServletContext ctxContext = getServletContext();
            // getResourceAsStream()方法需要以/开头,这个表示web应用的根路径:webApp/XXweb/bookCode.jar 与WEB-INF同级
            InputStream is = ctxContext.getResourceAsStream("/bookCode.jar");
           
            int read = 0;
            byte[] buffer = new byte[1024];
           
            OutputStream os = resp.getOutputStream();
            while((read = is.read(buffer))!=-1)
            {
                os.write(buffer);
            }
           
            is.close();
            os.close();
        }

}

常见的MIME类型:

text/html
application/pdf
video/quicktime
application/java
application/jpeg
application/jar
application/octet-stream
application/x-zip

response的输出返回有两种
一种是字节响应,另一种是字符响应

字节响应:
OutputStream os = resp.getOutputStream();
           
字符响应:
PrintWriter pWriter = resp.getWriter();


response的其他常用方法

//如果响应中已经有这个值就替换,否则就新增
resp.addHeader("gg", "ff");
           
//覆盖原有的值
resp.setHeader("gg", "ff");
           
//用提供的值替换原有的值,或者在响应中添加一个新的值
resp.setIntHeader("gg", 43);

2 重定向:

重定向设置有两种方式 :绝对路径重定向、相对路径重定向

绝对路径重定向:
resp.sendRedirect("http://www.sina.com.cn");

相对路径重定向:
又可以分为两种:前面有斜杠、前面无斜杠

一个客户请求:
http://www.wickeddly.com/myApp/cool/bar.do

在跳转到这个请求中后,无斜杠进行重定向:
sendRedirect(“foo/stuff.html”); 

结果容器的解析后会变成一个完整的路径:
http://www.wickeddly.com/myApp/cool/foo/stuff.html

若是加斜杠的重定向
sendRedirect(“/foo/stuff.html”);

结果容器的解析后会变成一个完整的路径:
http://www.wickeddly.com/foo/stuff.html

myApp/cool/ 消失了

不要在提交响应之后再次调用sendRedirect方法


3 请求分配

重定向是让客户来完成工作,请求分配要求服务器上的XX来完成工作,也就是重定向是再次将路径返回给浏览器,浏览器再次请求服务器的过程,即完成了跳转。所以它的请求路径会发生变化.而请求分配给浏览器的访问路径不会发生变化

请求分配的调用方式

RequestDispatcher view = req.getRequestDispatcher("result.jsp");
            view.forward(req, resp);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics