项目准备:
1.创建一个Dynamic Web Project项目,配置好Tomcat服务器,并加载项目
2.新建一个类,名为HelloServlet,并实现Servlet接口
package com.rosinante.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet implements Servlet {
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
//要输出的内容
System.out.println("Hello Servlet");
}
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
3.配置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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletDemo</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>
<!-- 向tomcat报告,应用中有这个servlet,名字叫做HelloServlet,路径为com.rosinante.servlet.HelloServlet -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.rosinante.servlet.HelloServlet</servlet-class>
</servlet>
<!-- 注册servlet的映射。ServletName:找到上面注册的具体Servlet,url-pattern:在地址栏上的path -->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/a</url-pattern>
</servlet-mapping>
</web-app>
4.启动Tomcat服务器,在浏览器地址栏输入http://localhost:8080/ServletDemo/a (ServletDemo是项目名)
5.控制台成功输出:
Servlet执行顺序分析
1.用户发送请求:
2.找到tomcat应用:
3.找到项目:
4.找到web.xml,然后在其中找到url-pattern,看有没有哪个url-pattern的内容是/a:
5.找到servlet-mapping中的那个servlet-name:
6.找到上面定义的Servlet元素中的servlet-name中的HelloServlet:
7.找到该servlet中定义的servlet-class,然后开始创建类的实例:
8.继而执行该servlet中的service方法:
9.最后输出Hello Servlet到浏览器
Servlet的通用写法
开发一个Servlet用三种方法:
- 定义一个类继承(推荐使用)javax.servlet.http.HttpServlet
- 定义一个类继承javax.servlet.GenericServlet:实现了Servlet接口,对其中一些方法,例如destory提供了空实现,对Servlet接口中的init(ServletConfig)做了实现,同时提供一个init(),并且做了空实现。
- 定义一个类实现javax.servlet.Servlet:最原始的标准,里边提供service,init,destory等方法
从上例可以看出,我们使用的是实现Servlet接口这种方式开发Servlet,Servlet接口用五个方法,而我们有时只需要实现其中几个,所以Servlet的通用写法为:
public class HelloServlet2 extends HttpServlet{
//get请求会来这个方法
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("get...");
}
//post请求会来这个方法
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("post...");
}
}