处理请求
Servlet的真正工作是处理客户端请求。 Servlet API提供了两个重要的接口javax.servlet.ServletRequest和javax.servlet.http.HttpServletRequest来封装客户端请求
这些接口的实现向servlet提供了有关客户机请求的重要信息。
HttpServletRequest接口
HttpServletRequest接口添加了与HTTP协议相关的方法。
In this example, we will show how a parameter is passed to a Servlet in a request object from HTML page.
使用Request对象传递参数案例
response.setContentType("text/html;charset=UTF-8");//设置响应的方式和编码
request.setCharacterEncoding("UTF-8");//设置请求的编码
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
out.println("<h2>welcome," + name + "</h2>");
处理响应
Servlet API提供了两个重要的接口ServletResponse和HttpServletResponse来帮助将响应发送到客户端。
HttpServletResponse接口添加了与HTTP响应相关的方法。
Request Dispatcher
Servlet中的请求调度员(Request Dispatcher)
RequestDispatcher接口定义了一个对象,该对象从客户端接收请求并将其分派到资源(如servlet、JSP、HTML文件)
forward
它将请求从一个servlet转发到另一个资源(如servlet、JSP、HTML文件)。
include
在响应中包含资源的内容(如servlet、JSP、HTML文件)。
Difference between forward() vs include() method(下面的这一段讲解的非常的清楚)
To understand the difference between these two methods, lets take an example: Suppose you have two pages X and Y. In page X you have an include tag, this means that the control will be in the page X till it encounters include tag, after that the control will be transferred to page Y. At the end of the processing of page Y, the control will return back to the page X starting just after the include tag and remain in X till the end.
In this case the final response to the client will be send by page X.
Now, we are taking the same example with forward. We have same pages X and Y. In page X, we have forward tag. In this case the control will be in page X till it encounters forward, after this the control will be transferred to page Y. The main difference here is that the control will not return back to X, it will be in page Y till the end of it.
sendRedirect
sendRedirect()方法将响应(Response)重定向(Redirect)到另一个资源。 此方法实际上使客户端(浏览器)创建一个新请求以获取资源。 客户端可以在浏览器中看到新的URL。
sendRedirect()接受相对URL,因此它可以在服务器内部或外部获取资源。
重定向和请求分派之间的主要区别在于
重定向使客户端(浏览器)创建一个新的请求来获取资源,用户可以看到新的URL
而Dispatcher请求分派是在同一个请求中获取资源,URL不会变化。
Also, another very important difference is that, sendRedirect() works on response object while request dispatch work on request object.
response.sendRedirect("https://www.baidu.com");//跳转到百度主页
ServletConfig
当Web容器初始化servlet时,它为servlet创建一个ServletConfig对象。(ServletConfig针对的是某一个servlet而言)
When the Web Container initializes a servlet, it creates a ServletConfig object for the servlet. ServletConfig object is used to pass information to a servlet during initialization by getting configuration information from web.xml(Deployment Descriptor).
如何在web.xml内部配置Servlet参数,初始化Servlet
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.fuck.HelloWorldServlet</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>zhandonghong@gmail.com</param-value>
</init-param>
</servlet>
在servlet类中获取Servlet的配置参数
ServletConfig servletConfig=getServletConfig();
out.println(servletConfig.getInitParameter("email"));
ServletContext
For every Web application a ServletContext object is created by the web container
ServletContent的作用范围比ServletConfig要小(ServletContent针对的是所有Servlet或者jsp而言)
ServletContext对象用于从Deployment Descriptor(web.xml)获取配置信息,该信息对于Web应用程序中的任何Servlet或JSP都可用。
How Context Parameter is Initialized inside web.xml
如何获得ServletContext的对象
ServletContext servletContext = getServletContext();
out.println(servletContext.getInitParameter("driverName"));
Attribute
怎么理解不同范围内的属性?
属性的范围:
- request
- session
- application
request.setAttribute("you","fuck");//请求范围级别属性
out.println(request.getAttribute("you"));
out.println("<br>");
ServletContext servletContext = getServletContext();
servletContext.setAttribute("name","vae");//应用程序范围级别属性
out.println(servletContext.getAttribute("name"));