javaweb开发(三):servlet入门
-
入门
-
编写UserServelt
# 继承HttpServlet类,重写它的方法
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> hello world </div>");
}
}
- 配置web.xml
<!-- servlet-mapping中通过/userServlet找到userServlet,在servlet标签中通过userServlet找到对应的类 -->
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>com.ychen.servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
-
启动项目测试
-
配置如下
-
重启测试
-
在UserServlet中重写doPost方法
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> hello doPost </div>");
}
-
重启测试
-
当然也可以重写其他方法,右键页面
-
servlet生命周期
实例化->使⽤构造⽅法创建对象
初始化->执⾏init⽅法:Servlet 的⽣命期中,仅执⾏⼀次 init() ⽅法,它是在服务器装⼊
Servlet 时执⾏的,即第⼀次访问这个Servlet才执⾏
服务->执⾏service⽅法,service() ⽅法是 Servlet 的核⼼。每当⼀个客户请求⼀个HttpServlet 对
象,该对象的service() ⽅法就要被调⽤
销毁-> 执⾏destroy⽅法,destroy() ⽅法仅执⾏⼀次,即在服务器停⽌且卸装 Servlet 时执⾏该⽅法
- 查看源码
# 自己创建servlet时继承HttpServlet
public class UserServlet extends HttpServlet
# HttpServlet继承了GenericServlet
public abstract class HttpServlet extends GenericServlet
# GenericServlet实现了Servlet
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
# 查看Servlet接口
public interface Servlet {
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
void destroy();
}
- 在GenericServlet中实现了Servlet接口的service方法
- HttpServlet中实现了GenericServlet的service方法
- 查看该方法
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 拿到请求的所有信息
String method = req.getMethod();
long lastModified;
// 判断是否是get请求,如果是则会执行this.doGet(req, resp)方法
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
// 同样的道理,判断是否是post请求,如果是则执行doPost方法
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
-
最后我们自己写的servlet继承了HttpServlet,最终执行的则是我们自己的servlet中对应的方法
-
验证servlet生命周期
# 将UserServlet修改为如下
public class UserServlet extends HttpServlet {
// ++ System.out.println("doGet方法执行了");
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet方法执行了");
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> hello world </div>");
}
// ++ servlet销毁的方法
@Override
public void destroy() {
System.out.println("servlet destroy执行了");
}
// ++ servlet初始化的方法
@Override
public void init() throws ServletException {
System.out.println("servlet init执行了");
}
}
-
执行doGet方法
-
控制台打印如下
-
再执行几次doGet方法,控制台打印如下,init方法却没有执行了,
说明第⼀次访问这个Servlet才执⾏init方法
-
最后关闭服务,执行销毁的方法
-
servlet新版配置
# 注释web.xml中的配置,在自定义的servlet中添加注解
@WebServlet( "/userServlet/find")
-
测试通过
-
配置注解如下
@WebServlet(name = "userServlet",urlPatterns = {"/user1","/user2","/test"})
- 测试