2022.1.21-1.24 Javaweb Servlet入门精讲
作者:互联网
Servlet
Servlet快速入门
代码如下:
1.pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
2.创建ServletDemo.java
package com.ittest.web;
import javax.jws.WebService;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @Description: TODO
* @author: scott
* @date: 2022年01月21日 14:33
*/
@WebServlet("/demo1")
public class ServletDemo1 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("hello1");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
注意实现接口快捷键:ctrl+i
3.在浏览器url输入:http://localhost:8080/tomcat-demo1/demo1
控制台显示结果如图,访问成功。
Servlet执行流程
Servlet生命周期
Servlet方法介绍
如果遇到在一个方法里面需要使用另一个方法的局部变量,可以采用将局部变量升级为成员变量使用。
如下所示:
@WebServlet("/demo1")
public class ServletDemo3 implements Servlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config= config;
System.out.println("hello1");
}
@Override
public ServletConfig getServletConfig() {
return config;
}
}
Servlet体系结构
报错:A child container failed during start
解决方案:把其他项目删除,是tomcat缓存造成, 重新运行tomcat即可。
ServletDemo4.java
package com.ittest.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;
/**
* @Description: TODO
* @author: scott
* @date: 2022年01月21日 15:51
*/
@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...");
}
}
a.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<form action="/tomcat-demo1/demo4" method="post">
<input name="username"><input type="submit">
</form>
</body>
</html>
运行提交后,控制台显示:
总结:
urlPttern配置
当一个目录既满足精确匹配又满足目录匹配,精确匹配的优先级更高。
任意配置"/",会覆盖掉tomcat中的DefaultServlet,当其他的url-pattern匹配不上时都会走这个Servlet。
XML配置Servlet
步骤:
1.编写Servlet类
2.在web.xml中配置该Servlet
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servler-name>demo13</servler-name>
<servler-class>com.ittest.web.ServletDemo13</servler-class>
</servlet>
<servlet-mapping>
<servlet-name>demo13</servlet-name>
<url-pattern>demo13</url-pattern>
</servlet-mapping>
</web-app>
标签:21,servlet,精讲,javax,Override,import,Servlet,public,1.24 来源: https://blog.csdn.net/qq_43416650/article/details/122620685