javax.servlet.http包中的类和接口继承自javax.servlet包中的类和接口,并添加了协议相关的特别方法,从而为基于Http协议的servlet开发提供了一些方便.
javax.servlet.http包中的httpservlet类继承自javax.servlet包中的GenericServlet类,我们日常的servlet的开发通常都继承自httpservlet类.
httpservlet中的六个doXXX方法
根据用户不同的请求方法,httpservlet将调用不同的方法来响应客户端的请求,其中最主要的的方法是:doGet和doPost方法.
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class RegisterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>The GET method</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("The servlet has received a GET. " + "Now, click the button below."); out.println("<BR>"); out.println("<FORM METHOD=POST>"); out.println("<INPUT TYPE=SUBMIT VALUE=Submit>"); out.println("</FORM>"); out.println("</BODY>"); out.println("</HTML>"); } public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>The POST method</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("The servlet has received a POST. Thank you."); out.println("</BODY>"); out.println("</HTML>"); } }
HttpServletRequest和HttpServletResponse接口
这两个接口都继承自javax.servlet包中的ServletRequest和ServletResponse接口,并实现了协议特别的方法.
比如getQueryString(),getRemoteHost(),setCookie()等等.
getParameter()方法和getParameterValues()方法
这两个方法用来获取用户请求的数据,其中getParameter方法用来获取单个参数值,而getParameterValues用来获取多个参数值,主要用在表单的多选框等场合.
sendRedirect()与forward()的区别
这两个方法都用来重定向到其他的资源,但存在本质的区别.其中sendRedirect()方法是通过发送一个Location头给客户端通知客户端重新请求新的资源.该方法需要在服务器端和客户端之间有一个网络Round-Trip,而forward()方法不需要和客户端进行交互,直接在服务器段执行,而且保留原来的httpservletrequest和httpservletresponse对象.
HttpServlet中需要注意的编码问题
如果在HttpServletResponse的响应流中存在html tag,我们需要对这些tag进行处理以得到正确的结果.通过我们可以开发一个通用的编码函数进行这项工作,下面的示例代码:
public static String encodeHtmlTag(String tag) { if (tag==null) return null; int length = tag.length(); StringBuffer encodedTag = new StringBuffer(2 * length); for (int i=0; i<length; i++) { char c = tag.charAt(i); if (c=='<') encodedTag.append("<"); else if (c=='>') encodedTag.append(">"); else if (c=='&') encodedTag.append("&"); else if (c=='"') encodedTag.append("""); else if (c==' ') encodedTag.append(" "); else encodedTag.append(c); } return encodedTag.toString(); }如何Buffer一个Servlet的响应通常我们会Buffer输出HttpServletResponse流以提高流的输出效率,HttpServletResponse默认已经开启了Buffer的功能,默认的Buffer大小是8192个字符,你可以通过HttpServletResponse的setBufferSize方法来设置这个大小,但有一点是必须要注意的是:必须在任何输出流之前设置Buffer的大小.ServletContext的getRequestDispatcher与HttpServletRequest的getRequestDispatcher的区别
Use the getRequestDispatcher method of the javax.servlet.ServletContext interface, passing a String containing the path to the other resource. The path is relative to the root of the ServletContext.
Use the getRequestDispatcher method of the javax.servlet.ServletRequest interface, passing a String containing the path to the other resource. The path is relative to the current HTTP request.
Use the getNamedDispatcher method of the javax.servlet.ServletContext interface, passing a String containing the name of the other resource.