package com.jy.sample.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HelloWorld Servlet.
* @author JY
*/
public class FirstServlet extends HttpServlet {
/** serialVersionUID. */
private static final long serialVersionUID = 217251451801586160L;
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
// 设定内容类型为HTML网页UTF-8编码
resp.setContentType("text/html;charset=UTF-8");
// 输出页面
PrintWriter out = resp.getWriter();
out.println("<html><head>");
out.println("<title>First Servlet Hello</title>");
out.println("</head><body>");
out.println("Hello!大家好!");
out.println("</body></html>");
out.close();
}
}
web.xml 省略。
该servlet 执行过程如下:
这里使用tomcat 服务器,而tomcat也是个servlet 容器。
当tomcat启动后,servlet 容器检测到需要加载的servlet后,首先会使用java反射机制生成servlet 实例,然后调用 init方法 init(ServletConfig config)。这里有个参数 config,A servlet configuration object used by a servlet container to pass information to a servlet during initialization.Servlet实例可以使用容器为它准备的ServletConfig对象从Web应用程序的配置信息(在web.xml中配置)中获取初始化的参数信息。
当有请求服务时调用 service 方法service(ServletRequest req, ServletResponse res)。Servlet容器调用Servlet的service()方法对请求进行处理。要注意的是,在service()方法调用之前,init()方法必须成功执行。在service()方法中,通过ServletRequest对象得到客户端的相关信息和请求信息,在对请求进行处理后,调用ServletResponse对象的方法设置响应信息。对于HttpServlet类,该方法作为HTTP请求的分发器,这个方法在任何时候都不能被重载。当请求到来时,service()方法决定请求的类型(GET、POST、HEAD、OPTIONS、DELETE、PUT、TRACE),并把请求分发给相应的处理方法(doGet()、doPost()、doHead()、doOptions()、doDelete()、doPut()、doTrace())每个do方法具有和第一个service()相同的形式。我们常用的就是doGet()和doPost()方法,为了响应特定类型的HTTP请求,我们必须重载相应的do方法。如果Servlet收到一个HTTP请求而你没有重载相应的do方法,它就返回一个说明此方法对本资源不可用的标准HTTP错误。
这里有 req 和 resp 两个参数。Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest
object and passes it as an argument to the servlet's service
method.
A ServletRequest
object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest
can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest
.
Defines an object to assist a servlet in sending a response to the client. The servlet container creates a ServletResponse
object and passes it as an argument to the servlet's service
method.
To send binary data in a MIME body response, use the ServletOutputStream
returned by getOutputStream()
. To send character data, use the PrintWriter
object returned by getWriter()
. To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream
and manage the character sections manually.
The charset for the MIME body response can be specified explicitly using the setCharacterEncoding(java.lang.String)
and setContentType(java.lang.String)
methods, or implicitly using the setLocale(java.util.Locale)
method. Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setCharacterEncoding
, setContentType
, or setLocale
method must be called before getWriter
and before committing the response for the character encoding to be used.
See the Internet RFCs such as RFC 2045 for more information on MIME. Protocols such as SMTP and HTTP define profiles of MIME, and those standards are still evolving.