本专辑将介绍servlet的使用。
我们首先看一下servlet实现的三种基本方式。
1、环境约束
- idea2018.1.5
- maven-3.0.5
- jdk-8u162-windows-x64
- servlet3.0
2、前提约束
- 创建maven的web工程
3、操作步骤
- 1 加入servlet依赖
javax.servlet
jsp-api
2.0
provided
javax.servlet
javax.servlet-api
3.0.1
- 2 加入tomcat插件
servlet-test
org.apache.tomcat.maven
tomcat7-maven-plugin
/
8088
- 3 实现servlet
3.1 实现Servlet接口
在src/main/java文件夹下创建net.wanho.servlet包,在该包下创建ServletOne.java文件,内容如下:
package net.wanho.servlet;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/servletone")
public class ServletOne implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("获取页面传递过来的参数,调用下层逻辑,返回响应");
String name = req.getParameter("name");
res.getWriter().write("这是后台返回的数据:"+name);
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
3.2 继承HttpServlet类
在src/main/java文件夹下的net.wanho.servlet包中创建ServletTwo.java文件,内容如下:
package net.wanho.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;
@WebServlet("/servlettwo")
public class ServletTwo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("获取页面传递过来的参数,调用下层逻辑,返回响应");
String name = req.getParameter("name");
resp.getWriter().write("这是ServletTwo返回的数据:"+name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.3 继承GenericServlet类
在src/main/java文件夹下的net.wanho.servlet包中创建ServletTwo.java文件,内容如下:
package net.wanho.servlet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
@WebServlet("/servletthree")
public class ServletThree extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("获取页面传递过来的参数,调用下层逻辑,返回响应");
String name = req.getParameter("name");
res.getWriter().write("这是ServletThree返回的数据:"+name);
}
}
- 测试
(1)以maven自带tomcat插件方式启动项目
(2)打开浏览器,分别输入:
http://localhost:8088/servletone?name=ali
http://localhost:8088/servlettwo?name=ali
http://localhost:8088/servletthree?name=ali
则看到三个servlet返回的不同结果。 - 注意:
@WebServlet("/servletone")可以被web.xml中的以下内容代替:
one
net.wanho.servlet.ServletOne
one
/servletone
@WebServlet("/servlettwo")可以被web.xml中的以下内容代替:
two
net.wanho.servlet.ServletTwo
two
/servlettwo
@WebServlet("/servletthree")可以被web.xml中的以下内容代替:
three
net.wanho.servlet.ServletThree
three
/servletthree
前者是注解,后者是配置,是实现一件事情的两种不同方式。