一、SevletContext主要功能:
1. 操作Application级变量:getAttribute,getAttributeNames,setAttribute,removeAttribute
2. 获取相对于Web应用根目录物理地址:getRealPath
3.获取Web应用相对根目录:getContextPath,如果全部web url全部路径为:http://localhost:8080/WebAppName/...,则getContextPath则返回 /WebAppName
4.获取RequestDispather:getRequestDispatcher,getNamedDispatcher
5.获取版本相关属性
二、RequestDispatcher:
1.include:将RequestDispatcher对应的输出包含到当前输出流中
2.foward:将请求转向到RequestDispather中,返回的输出仅仅包含RequestDispather对应的输出,但是在forward之前不能够调用flush操作,否则返回flush之间的输出
下面是一个简单的例子来显示着两者之间的区别:
FirstServlet.java,如下所示:
public class FirstServlet extends HttpServlet { @Override public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String method="include"; if(request.getParameter("method")!=null) method = request.getParameter("method"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("Method:"+method+"</br>"); out.println("The begin message from First Servlet!</br>"); //out.flush(); RequestDispatcher rd = request.getRequestDispatcher("/servlet/Second"); if(method.equals("include")) rd.include(request,response); else rd.forward(request,response); out.println("The end message from FirstServlet!</br>"); out.println("</body></html>"); } }
SecondServlet.java,如下:
public class SecondServlet extends HttpServlet{ @Override public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ PrintWriter out = response.getWriter(); out.println("Message from SecondServlet!</br>"); } }
部署好后,在浏览器中输入http://localhost:8080/demosite/servlet/First?method=include,则浏览器中返回如下信息:
Method:include
The begin message from First Servlet!
Message from SecondServlet!
The end message from FirstServlet!
再在浏览器中输入http://localhost:8080/demosite/servlet/First?method=forward,则浏览器中返回如下信息:
Message from SecondServlet!</br>
如果去掉FirstServlet中的//out.flush();注释,则http://localhost:8080/demosite/servlet/First?method=forward返回:
Method:forward
The begin message from First Servlet!
三、HttpServletRequest:
1.操作Request级别共享数据:getAttribute,getAttributeNames,setAttribute,removeAttribute
2.获取GET/POST模式传递参数:getParameter,getParameterValues,getParameterNames,getParameterMap
3.获取请求Header信息:getDateHeader,getHeader,getHeaders,getIntHeader,getHeaderNames
4.获取客户端Cookie: getCookies
5.获取Get模式查询字符串:getQueryString,例如http:/localhost/servlet/service?method=aaa则返回method=aaaa
6.获取Session数:getSession,getRequestSessionID,isRequestedSessionIdValid,isRequestedSessionIdFromCookie,isRequestedSessionIdFromURL
7.获取路径相关操作:getPathInfo,getPathTranslated,getContextPath,getRequestURI,getRequestURL,getServletPath
8.获取请求文本编码:getCharacterEncoding
四、HttpServletResponse:
1.获取输出流及writer:ServletOutputStream,getWriter
2.设置输出类型及编码:setContentType,setCharacterEncoding,getCharacterEncoding,示例如下:
setContextType("text/html;charset=gb2312");
setCharacterEncoding("gb2312");
3.重定向操作:sendRedirect,encodeURL,encodeRedirectURL
sendRedirect与RequestDispatcher区别具体参考后续文章
encodeURL与encodeRedirectURL主要处理会话ID传递:首先判断是否启用会话,如果没有则直接返回参数,如果有则检验客户浏览器是否禁用了cookie,如果没有禁用,则直接返回参数,如果禁用,则会在参数URL中天际jsessionid=xxxxxx的参数用来保存会话ID,两者具体的区别不是太清楚
4.设置返回html头数据:containsHeader,setxxxHeader等
5.设置cookie:setCookie
6.直接返回错误信息:sendError
7.设置状态:setStatus