编写Servlet
建立Servlet
这个类继承javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServlet;
public class WelcomeServlet extends HttpServlet {
}
重写HttpServlet的doGet()方法
package xxx.xxx.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//WelcomeServlet.java
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=GB18030");
PrintWriter out = response.getWriter();
out.println("欢迎来到本系统!");
}
}
配置Servlet
<servlet>
<!-- servlet-name必须一样 -->
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>xxx.xxx.servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<!-- url-pattern可以自己改,不一定一样 -->
<url-pattern>/servlets/WelcomeServlet</url-pattern>
</servlet-mapping>
配置好的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Servlet_1000</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- servlet-name必须一样 -->
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>xxx.xxx.servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<!-- url-pattern可以自己改,不一定一样 -->
<url-pattern>/servlets/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
部署Servlet
测试Servlet
Servlet运行机制
将WelcomeServlet.java改为如下:
public class WelcomeServlet extends HttpServlet {
public WelcomeServlet() {
System.out.println("创建对象!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("调用doGet()函数!");
response.setContentType("text/html;charset=GB18030");
PrintWriter out = response.getWriter();
out.println("欢迎来到本系统!");
}
}
重新部署项目,第一次访问WelcomeServlet,系统实例化WelcomeServlet对象,控制台打印如下:
第二次访问WelcomeServlet,控制台打印如下:
解释:第一次访问运行了构造函数和doGet()函数,第二次访问只运行了doGet()函数。
结论:两次访问只创建了一个Servlet对象。
Servlet生命周期
- init():实例化Servlet。
- doGet()/doPost()/service()方法:客户端对Servlet发送一个请求过来,服务器将会开启一个线程,该线程用service()方法,service()方法会根据收到的客户端的请求类型来决定是调用doGet()还是doPost()。但是一般情况下不用覆盖service()方法,使用doGet()与doPost()方法,一样可以达到处理的目的。
- destroy()方法:Servlet消亡。
Servlet与JSP内置对象
- 获得out对象:
response.setContentType("text/html;charset=GB18030");//设置中文编码
PrintWriter out = response.getWriter();
- 获得request和response对象:request参数就是request对象,response参数就是response对象。
- 获得session对象:
HttpSession session = request.getSession();
- 获得application对象:
ServletContext application = this.getServletContext();
设置欢迎页面
<welcome-file-list>
<!-- 所要设置的欢迎页面 -->
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
在Servlet中读取参数
设置参数
配置好的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Servlet_1000</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 设置全局参数 -->
<context-param>
<param-name>encoding</param-name>
<param-value>GB18030</param-value>
</context-param>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>xxx.xxx.servlets.InitServlet</servlet-class>
<!-- 设置局部参数 -->
<init-param>
<param-name>driverClassName</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/servlets/InitServlet</url-pattern>
</servlet-mapping>
<servlet>
<!-- servlet-name必须一样 -->
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>xxx.xxx.servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<!-- url-pattern可以自己改,不一定一样 -->
<url-pattern>/servlets/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
获取参数
InitServlet.java:
package xxx.xxx.servlets;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//获取全局参数
ServletContext application = this.getServletContext();
String encoding = application.getInitParameter("encoding");
System.out.println("encoding参数是:"+encoding);
//获取局部参数
String driverClassName = this.getInitParameter("driverClassName");
System.out.println("driverClassName参数是:"+driverClassName);
}
}