Servlet是javaWeb的三大组件。Servlet程序,Listener监听器。Filter过滤器。
Servlet程序主要是运行在Tomcat服务器上。
Servlet程序的作用:接收客户端的请求。响应给客户端数据。
1、编写一个类去实现Servlet接口
2、实现Servlet程序中的service方法(处理业务的方法)
3、到web.xml中去配置Servlet程序的访问地址
Servlet程序的源代码:
public class HelloServlet implements Servlet {
/**
* Servlet程序接收客户端的请求,并且响应数据
* service在每请求进来 的时候,负责 处理业务
*/
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
System.out.println("HelloServlet 程序被访问了~");
}
}
web.xml中的配置:
HelloServlet
com.atguigu.servlet.HelloServlet
HelloServlet
/hello
1、先执行Servlet的构造方法
2、执行Servlet程序的init初始化方法
构造方法和init初始化方法,只是第一次访问的时候由Tomcat服务器来调用,只调用一次
3、执行Service方法 (每次访问都会被调用)
4、执行destroy方法(服务器停止,停止web工程 )
请求分为GET请求和POST请求。(一般GET请求和POST请求有可能会做不同的业务操作)
服务器如何区分 GET请求和POST请求。
/**
* Servlet程序接收客户端的请求,并且响应数据
* service在每请求进来 的时候,负责 处理业务
*/
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// System.out.println("HelloServlet 程序被访问了~");
HttpServletRequest httpRequest = (HttpServletRequest) request;
// getMethod返回是GET或POST,返回请求的方式
String method = httpRequest.getMethod();
System.out.println(method);
if ("GET".equals(method)) {
// 如果是GET请求,执行get业务
doGet();
} else if ("POST".equals(method)) {
//执行post请求的业务操作
doPost();
}
}
/**
* 这是GET请求需要做的工作
*/
public void doGet() {
System.out.println("doGet 这是GET请求");
}
/**
* 这是POST请求需要做的工作
*/
public void doPost() {
System.out.println("doPost 这是POST请求");
}
在实现的开发中,我们都是使用HttpServlet抽取类去实现Servlet程序。
1、编写一个类去继承HttpServlet类
2、重写doGet方法和doPost方法
3、到web.xml中去配置Servlet程序的访问地址
HelloServlet2类
public class HelloServlet2 extends HttpServlet {
/**
* doGet方法,是GET请求的时候,会自动的调用
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("这是GET请求,调用了doGet方法");
}
/**
* doPost方法,是POST请求的时候,会自动的调用
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("这是POST请求,调用了doPost方法");
}
}
web.xml中的配置:
abc
com.atguigu.servlet.HelloServlet2
abc
/ccc
我们在重写doGet方法或doPost方法的时候,方法体内,一定不能调用super.doGet或super.doPost代码。否则会报405错误
我们所有的类都是由tomcat服务器进行创建。我们负责使用。
ServletConfig类它封装了Servlet程序的配置信息。
a) ServletConfig类的三大作用
1、获取Servlet程序在web.xml中配置的名称
2、获取在web.xml中配置的初始化参数
3、获取ServletContext对象
public class ConfigServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
// 1、获取Servlet程序在web.xml中配置的名称
System.out.println("在web.xml中配置的servlet-name名称:" + config.getServletName());
// 2、获取在web.xml中配置的初始化参数
System.out.println("username初始化参数值:" + config.getInitParameter("username"));
System.out.println("url初始化参数值:" + config.getInitParameter("url"));
// 3、获取ServletContext对象
System.out.println( config.getServletContext() );
}
}
web.xml中的配置:
ConfigServlet
com.atguigu.servlet.ConfigServlet
username
root
url
jdbc:mysql://localhost:3306/test
ConfigServlet
/configServlet
注意:ServletConfig是在GenericServlet类中持有的引用。当我们需要在自己自定义的Servlet程序中重写init(ServletConfig)初始化方法的时候。
一定要调用super.init(config); 否则父类中的ServletConfig就不能得到初始化
1、ServletContext对象是一个接口。
2、ServletContext在一个web工程中只有一个对象实例(也是由Tomcat服务器创建)。
3、ServletContext是一个域对象。
什么是域对象?
域对象指的是可以像map一样存取数据的对象。
put setAttribute 保存数据
get getAttribute 获取数据
域指的保存和获取数据的范围。ServletContext对象它的数据操作范围是整个web工程。
ServletContext对象在web工程启动的时候创建。在web工程停止的时候销毁。
b) ServletContext类的四个作用
1、它可以获取在web.xml中配置的上下文参数
2、它可以获取到当前web工程的工程名(工程路径)
3、它可以获取web工程发布到服务器上之后。文件或目录在服务器磁盘上的绝对路径。
4、它可以像map一样存取数据。
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// b)ServletContext类的四个作用
ServletContext ctx = getServletContext();
// 1、它可以获取在web.xml中配置的上下文参数
System.out.println("上下文初始化参数password的值:"
+ ctx.getInitParameter("password"));
System.out.println("上下文初始化参数driverClass的值:"
+ ctx.getInitParameter("driverClass"));
// 2、它可以获取到当前web工程的工程名(工程路径)
// getContextPath()获取当前工程名
System.out.println(ctx.getContextPath());
// 3、它可以获取web工程发布到服务器上之后。文件或目录在服务器磁盘上的绝对路径。
// getRealPath是获取指定的路径的资源在服务器上的绝对路径。
// / 斜线,表示 根,
// / 斜线 在web工程中 表示到http://ip:port/工程名/ 映射到 代码的WebContent目录
System.out.println(ctx.getRealPath("/"));
System.out.println("WebContent/css的绝对路径是:" + ctx.getRealPath("/css"));
System.out.println("WebContent/imgs/ludashi.jpg的绝对路径是:" + ctx.getRealPath("/imgs/ludashi.jpg"));
}
在web.xml中的配置:
password
root
driverClass
com.mysql.jdbc.Driver