servlet02_servlet的三个hashMap

来源:互联网 发布:重庆数据分析的项目 编辑:程序博客网 时间:2024/05/17 08:12

1,第一个hashMap:如何解决编码问题 (相当于过滤器) 
解决因提交方式不同的参数编码问题

resp.setContentType("text/html;charset=utf-8");PrintWriter out=resp.getWriter();String method=req.getMethod();if("post".equalsIgnoreCase(method)){    req.setCharacterEncoding("utf-8");}else if("get".equalsIgnoreCase(method)){    //得到一个参数的hashMap集合(req.getParameterNames())    Map map=req.getParameterMap();    Set setmap=map.keySet();        //再进行迭代      Iterator it=setmap.iterator();     //将迭代循环    while(it.hasNext()){              //取得迭代中的键           String key=(String)it.next();          //通过键取值        String[] values=(String[])map.get(key);        for(int i=0;i<values.length;i++){            //反编码            byte[] buff=values[i].getBytes("iso8859-1");            //编码后转码            values[i]=new String(buff,"GBk");        }    }String name=req.getParameter("uname");out.println("欢迎您:"+name);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

2,第二个hashMap:通过request得到请求头的方法

/*得到Request的一些常用请求头的方法 resp.setContentType("application/vnd.ms-excel;charset=utf-8");application/vnd.ms-excel是excel表单的方式打开,还可以设置为其他客户端浏览器所支持的格式 */resp.setContentType("text/html;charset=GBK"); req.setCharacterEncoding("GBK"); PrintWriter out=resp.getWriter();//只有HttpServletRequest在可以得到头信息 HttpServletRequest request=(HttpServletRequest)req;//得到请求头的一个枚举Enumeration enums=request.getHeaderNames(); //以表格的方式打印出来out.print("<table border='1'>");//是否有更多的元素 while(enums.hasMoreElements()){    out.print("<tr>");     //从枚举中取得键     String key=(String)enums.nextElement();    //通过键取值    String value=request.getHeader(key);      out.print("<td>"+value+"</td>"); }out.print("</table>"); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3,第三个hashMap:通过request得到所有属性的方法

PrintWriter out=resp.getWriter();       //将请求转为 Http的请求HttpServletRequest request=(HttpServletRequest)req; //得到Request中所有的属性 返回一个枚举类型      Enumeration enums=request.getAttributeNames();      //看枚举中是否有元素         while(enums.hasMoreElements()){                 //从枚举中把键取出来                 String key=(String)enums.nextElement();                 //通过键取的值                Object value=request.getAttribute(key);             out.print(key+":"+value+"<br>");        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意:会有tomcat已经设好的三个值

javax.servlet.forward.request_uri /WebTest/page1.jsp 
javax.servlet.forward.context_path /WebTest 
javax.servlet.forward.servlet_path /page1.jsp

4,得到session,application

HttpServletRequest request = (HttpServletRequest)req;String n1 = (String)request.getAttribute("name");System.out.println("request:"+n1);//得到sessionHttpSession session = request.getSession();String n2 = (String)session.getAttribute("name");System.out.println("session:"+n2);//得到application//ServletContext application = session.getServletContext();//用此方法只有再继承了httpservlet类或 GenericServlet类才有//ServletContext application = this.getServletContext();//此方法适应于现实了servlet接口的servlet类ServletContext application=            this.getServletConfig().getServletContext();String n3 = (String)application.getAttribute("name");System.out.println("application:"+n3);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

5,ServletConfig和ServletContext的区别 
ServletConfig记录着单个服务员的信息,比如说年龄,性别等 
ServletConfig只能获得自己的servlet信息配置信息 
比如:以前获取编码参数的问题等

web.xml中

<servlet>     <servlet-name>hello7</servlet-name>     <servlet-class>com.accp.servlet.Hello7</servlet-class>     <init-param>         <param-name>encoding</param-name>         <param-value>             text/html;charset=GB18030          </param-value>     </init-param>     <init-param>         <param-name>sex</param-name>         <param-value></param-value>     </init-param>     <init-param>         <param-name>age</param-name>         <param-value></param-value>     </init-param></servlet><servlet-mapping>      <servlet-name>hello7</servlet-name>      <url-pattern>/hello7</url-pattern></servlet-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Service()方法中

//ServletConfig使用,拿到Hello7这个servlet单独的信息//获取到该servlet所有参数名 Enumeration configs = this.getServletConfig().getInitParameterNames();while(configs.hasMoreElements()){    String key=(String)configs.nextElement();    //根据参数名拿参数值    String value = this.getServletConfig().getInitParameter(key);    System.out.println(key+"--"+value); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

但是我们发现以前每个servlet都要有编码问题的设置,那我们能不能配置一个公有的,让每一个Servlet都享有,其实可以配置一个公有信息 
问题是在servlet中怎么获取这些公共的信息? 
web.xml中通过下面的配置公共信息

<context-param>    <param-name>encoding</param-name>    <param-value>text/html;charset=GB18030</param-value></context-param>
  • 1
  • 2
  • 3
  • 4

ServletContext记录着整个这个门店的信息,也就是服务员工作坏境的信息。比如墙壁是刷成什么颜 色等,ServletContext保存着整个公共的配置信息

 //ServletContext使用,拿到整个公共的配置信息Enumeration contexts =this.getServletContext().getInitParameterNames();while(contexts.hasMoreElements()){    String key=(String)contexts.nextElement();    String value = this.getServletContext().getInitParameter(key);    System.out.println(key+"--"+value); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

总结:

ServletConfig :记录着单个servlet的信息(单个服务员信息:年龄 , 三围等) 
ServletContext:记录着所有servlet共享的信息(服务员上班的环境:墙壁颜色,设备的型号等)

6,利用ServletContext,读取WEB-INF目录下的客户端不可读文件 
web-inf下面的文件是不能被客户端所访问到的,其中web.xml主要描述的就是整个工程的配置 
但是实际开发的过程中,就是希望客户不能直接访问我们的资源。但是在servlet中又想获取这些资源,那么就可以通过ServletContext对象来获取:

resp.setContentType("text/html;charset=GBk");PrintWriter out=resp.getWriter();ServletContext sct=this.getServletContext();//获取到文件资源,并将它转为文件流(相对路径)InputStream in=sct.getResourceAsStream("/WEB-INF/abc.html");Reader read=new InputStreamReader(in);//转为字符流BufferedReader reader=new BufferedReader(read);//改为包装流String str=reader.readLine();//读取while(str!=null){    out.print(str);//打印    str=reader.readLine();}reader.close();read.close();in.close();