黑马android74期基础是Servlet service方法的一个参_第1页
黑马android74期基础是Servlet service方法的一个参_第2页
黑马android74期基础是Servlet service方法的一个参_第3页
黑马android74期基础是Servlet service方法的一个参_第4页
黑马android74期基础是Servlet service方法的一个参_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

responseservice()response对象可以完成对客户端的response==载时,那么可以使用response.getOutputStream()。response.getOutputStream()IllegalStateException异UTF-8编码的!UTF-8来解读响应content-type头来解读响应数据。8KBhtmlutf-8response的utf-8response.setCharaceterEncoding(“utf-8”);; 8")当你 时,你会发现浏览器地址栏中的URL会变成 200302表示重定向。所以完成重定向的第一步就是设置302。URL,所以完成重Location头,指定第二个请求的URL地址。publicpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOExceptionresponse.setHeader("Location",}}上面代码的作用是:当AServlet后,会通知浏览器重定向到传智主页。客户端浏览器解析publicpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{ }}publicclassAServletextendsHttpServletpublicvoidpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{ }}重定向的URL地址为:重定向就不要再使用response.getWriter()response.getOutputStream()requestServlet.service()service()service()request对象来获封装了请求正文数据,如果是GETrequest是一个域对象,可以把它当成MaprequestrequestJavaWebServletContext就是域对象,它在整个ServletContext对象。request其中一个,request可以在一个请求中共享数据。requestServletServlet就么会覆盖上一次的值,这一特性与Map相同;ObjectgetAttribute(Stringname)request中的数据,当前在获取之前需要先去才行,例如:StringvalueString)request.getAttribute(“xxx”);xxx的域属性;voidremoveAttribute(Stringname)requestname指定的域URL相关的方法。东西在中很有用;StringgetCharacterEncoding()setCharacterEncoding(),那么返回nullISO-8859-1编码;POST请求中的参数有效! StringBuffergetRequestURL():返回请求URL路径,例如:,System.System.out.println("request.getContentLength():"+System.out.println("request.getContentType():"+request.getContentType());System.out.println("request.getContextPath():"+request.getContextPath());System.out.println("request.getMethod():"+request.getMethod());System.out.println("request.getLocale():"+request.getLocale());System.out.println("request.getQueryString():"+request.getQueryString());System.out.println("request.getRequestURI():"+request.getRequestURI());System.out.println("request.getRequestURL():"+request.getRequestURL());System.out.println("request.getServletPath():"+request.getServletPath());System.out.println("request.getRemoteAddr():"+request.getRemoteAddr());System.out.println("request.getRemoteHost():"+request.getRemoteHost());System.out.println("request.getRemotePort():"+request.getRemotePort());System.out.println("request.getScheme():"+request.getScheme());System.out.println("request.getServerName():"+request.getServerName());System.out.println("request.getServerPort():"+Stringip=request.getRemoteAddr();if(ip.equals("")){Stringip=request.getRemoteAddr();if(ip.equals("")){response.getWriter().print("您的IP已 }else}超:一定是GET请求GETPOSTGET1KPOST<form<form o/ParamServlet"参数1:<inputtype="text参数2:<inputtype="text<inputtype="submitvalue="提交<a 下面是使用requestpublicpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{Stringv1=request.getParameter("p1");Stringv2=request.getParameter("p2");System.out.println("p1="+v1);System.out.println("p2="+v2);}publicpublicvoiddoPost(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{Stringv1=request.getParameter("p1");Stringv2=request.getParameter("p2");System.out.println("p1="+v1);System.out.println("p2="+v2);}publicvoiddoGet(HttpServletRequestrequest,publicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOExceptionString[]names=request.getParameterValues("name");}<a参数1:<input参数1:<inputtype="text参数2:<inputtype="text<inputtype="submitvalue="提交o/ParamServlet"<formpublicvoiddoPost(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{Enumerationnames=request.getParameterNames();while(names.hasMoreElements()){ }}Map<String,String[]> p=for(Stringname:par p.keySet()){String[]values=par System.outMap<String,String[]> p=for(Stringname:par p.keySet()){String[]values=par System.out.println(name+":"+}p2:[v2,p1:[v1,ServletServlet1来处Servlet1Servlet2来继续处理这个请求。AServletpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{RequestDispatcherrd=request.getRequestDispatcher("/BServlet");rd.forward(request,response);}}publicclassBServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{}}publicclassBServletextendspublicclassBServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsepublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{RequestDispatcherrd=request.getRequestDispatcher("/BServlet");rd.include(request,response);}}}}throwsServletException,IOExceptionresponse.getWriter()response.getOutputStream()向客户端输出,这一工作应该由BServlet来完成;如果是使用请求包含,那么没有这个限制;ServletJSPAServletBServletGETPOST,重定向的第二个请求一定是超ClassClassLoader绝对路径:<a客户端路径:<a绝对路径:<a客户端路径:<a相对路径:<a <form <inputtype="submitvalue="表单<form <inputtype="submitvalue="表单<form<input<inputtype="submitvalue="表单2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终的页面为;33:不以“/a.html所有路径,即最终的路径为:;publicclasspublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{ }}假设AServlet的路径为:publicclassAServletextendsHttpServletpublicclassAServletextendsHttpServletpublicrequest,throwsServletException,IOException}}假设AServlet的路径为:<formaction="/<a 改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解! response.sendRedirect(request.getContextPath()+ 题的处理方案是使response.sendRedirect(request.getContextPath()+publicclassAServletextendspublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{}}假设AServlet的路径为:publicclassAServletextendsHttpServletpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException{}}假设AServlet的路径为:publicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestpublicclassAServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOExceptionStringpath1=this.getServletContext().getRealPath("a.txt");Stringpath2=this.getServletContext().getRealPath("/a.txt");}}path1path2是相同的结果:Classpackage;importpublicpackage;importpublicclassDemopublicvoidfun1()InputStreamin=}publicvoidfun2()InputStreamin=}} 类在 ClassLoaderpublicclassDemopublicvoidpublicclassDemopublicvoidfun1(){InputStreamin=}publicvoidfun2(){InputStreamin=}} ,即 ::<!DOCTYPE<!DOCTYPE<metahttp-equiv="content-type"content="text/html;charset=UTF-<form名称:<inputtype="text<inputtype="submitvalue="提交<a GETGETrequest.getParameter()获取的数据是被服务ISO-8859-1UTF-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论