方式1:extends HttpServlet 类
方式2:extends GenericServlet抽象类
方式3:implements Servlet接口
用的最多:方式1
方式1代码:
package com.msb.testservlet;
import javax.servlet.Servlet;
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;
/**
* @Author: zhaoss
*/
@WebServlet("/s01")
public class Servlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 控制台打印一句话
System.out.println("hi servlet");
// 响应一句话给浏览器
resp.getWriter().write("hi servlet");
}
}
方式2代码:
package com.msb.testservlet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @Author: zhaoss
*/
@WebServlet("/s02")
public class Servlet02 extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet02...");
}
}
方式3代码:
package com.msb.testservlet;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @Author: zhaoss
*/
@WebServlet("/s03")
public class Servlet03 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet03...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
启动服务器,依次访问Servlet测试即可。
http://localhost:8888/s/s01
http://localhost:8888/s/s02
http://localhost:8888/s/s03
Servlet中方法的其它实现方式:
父类HttpServlet的service方法中会对请求做分发处理:
所以子类 Servlet01中可以不用重写service方法,直接写doGet和doPost方法,被父类的service方法调用即可:(不推荐)
package com.msb.testservlet;
import javax.servlet.Servlet;
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;
/**
* @Author: zhaoss
*/
@WebServlet("/s01")
public class Servlet01 extends HttpServlet {
//@Override
/*protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 控制台打印一句话
System.out.println("hi servlet");
// 响应一句话给浏览器
resp.getWriter().write("hi servlet");
}*/
@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....");
this.doGet(req,resp);//无论get请求和post请求都会走同一套业务逻辑。
}
}
Servlet的对外访问路径的设置:
启动web项目后,servlet容器会读取web.xml配置文件,我们可以在web.xml中对servlet路径做配置:
web.xml中代码:
<?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-name>a</servlet-name>
<servlet-class>com.msb.testservlet.Servlet04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/s04</url-pattern>
</servlet-mapping>
</web-app>
Servlet代码:
package com.msb.testservlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: zhaoss
*/
public class Servlet04 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet04");
}
}