文章目录
简介
一、快速入门
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
package com.itheima.web;
import javax.jws.WebService;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebServlet("/demo1")
public class MyHttpServlet implements Servlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("Hello World");
}
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
protected void doPost(ServletRequest req, ServletResponse res) {
}
protected void doGet(ServletRequest req, ServletResponse res) {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
二、Servlet 执行流程
三、Servlet 生命周期
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* Servlet 生命周期方法
*/
@WebServlet(urlPatterns="/demo2",loadOnStartup = 1)
public class ServletDemo2 implements Servlet {
/**
* 初始化方法
* 1. 调用时机:默认情况下,Servlet被第一次访问时,调用
* * loadOnStartup:
* 2. 调用次数:1次
* @param config
* @throws ServletException
*/
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("init...");
}
/**
* 提供服务
* 1. 调用时机:每一次Servlet被访问时,调用
* 2. 调用次数:多次
*
*
* @param req
* @param res
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("servlet hello world~");
}
/**
* 销毁方法
* 1. 调用时机:内存释放或者服务器关闭的时候,Servlet对象会被销毁,调用
* 2. 调用次数:1次
*/
@Override
public void destroy() {
System.out.println("destroy...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public ServletConfig getServletConfig() {
return null;
}
}
四、Servlet 方法介绍
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* Servlet 方法介绍
*/
@WebServlet(urlPatterns="/demo3",loadOnStartup = 1)
public class ServletDemo3 implements Servlet {
private ServletConfig config;
/**
* 初始化方法
* 1. 调用时机:默认情况下,Servlet被第一次访问时,调用
* * loadOnStartup:
* 2. 调用次数:1次
* @param config
* @throws ServletException
*/
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
System.out.println("init...");
}
@Override
public ServletConfig getServletConfig() {
return config;
}
/**
* 提供服务
* 1. 调用时机:每一次Servlet被访问时,调用
* 2. 调用次数:多次
*
*
* @param req
* @param res
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("servlet hello world~");
}
/**
* 销毁方法
* 1. 调用时机:内存释放或者服务器关闭的时候,Servlet对象会被销毁,调用
* 2. 调用次数:1次
*/
@Override
public void destroy() {
System.out.println("destroy...");
}
@Override
public String getServletInfo() {
return "";
}
}
五、Servlet 体系结构
package com.itheima.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo4")
public class ServletDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post...");
}
}
要调用dopost方法时,要在webapp中写一个html,方便查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/web-demo/demo5" method="post">
<input name="username"><input type="submit">
</form>
</body>
</html>
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class MyHttpServlet implements Servlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// 根据请求方式的不同,进行分别的处理
HttpServletRequest request = (HttpServletRequest) req;
//1. 获取请求方式
String method = request.getMethod();
//2. 判断
if("GET".equals(method)){
// get方式的处理逻辑
doGet(req,res);
}else if("POST".equals(method)){
// post方式的处理逻辑
doPost(req,res);
}
}
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
protected void doPost(ServletRequest req, ServletResponse res) {
}
protected void doGet(ServletRequest req, ServletResponse res) {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
继承HttpServlet
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebServlet("/demo5")
public class ServletDemo5 extends MyHttpServlet {
@Override
protected void doGet(ServletRequest req, ServletResponse res) {
System.out.println("get。。。。");
}
@Override
protected void doPost(ServletRequest req, ServletResponse res) {
System.out.println("post。。。。");
}
}
六、Servlet urlPattern配置
七、XML 配置方式编写 Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--
Servlet 全类名
-->
<servlet>
<servlet-name>demo13</servlet-name>
<servlet-class>com.itheima.web.ServletDemo13</servlet-class>
</servlet>
<!--
Servlet 访问路径
-->
<servlet-mapping>
<servlet-name>demo13</servlet-name>
<url-pattern>/demo13</url-pattern>
</servlet-mapping>
</web-app>