The code address
spring
thinks that the blogger can also give a Star
1.
SpringMVC SpringMVC is based on Servlet to achieve encapsulation. Servlet is a technology provided by Sun company for developing dynamic web resources. Usually we also call the java program that implements the servlet interface as Servlet.
2. Create a webapp project
File—>new project
must add this Maven Property (archetypeCatalog = internal) otherwise it may be imported all the time when opening the project, and the import time is too long.
There is no java package found in our main directory, we need to create it ourselves. But it needs to be configured after creation (if it is not configured, the package is not a resource directory, and java files cannot be created). (The configuration is as follows) The
first point also said that it is based on servlet packaging, so you need to introduce related jar packages. There are two forms. The first is directly imported by maven, and the other is the tomcat.
Let's use tomcat to introduce it directly.
First of all, add tomcat
after startup , find your own tomcat, and add the configuration
in the lower right corner. It also needs to be configured.
Then add the jar package
that comes with tomcat, we will find that there are two more jar packages here,
and then you can use your own tomcat to run.
The first step of our project is to create our own servlet.
Create MyServlet.java
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 龙小虬
* @date 2021/3/9 15:23
*/
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("this is new Servlet");
}
}
Then configure the filter information in web.xml to
add configuration:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Just run it.
Let's think about it again, is the servlet thread safe?
Let's verify it.
Modify the MyServlet.java code:
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 龙小虬
* @date 2021/3/9 15:23
*/
public class MyServlet extends HttpServlet {
int count = 0;
public MyServlet(){
System.out.println("MyServlet无参构造执行。。。");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
count++;
resp.getWriter().print("this is new Servlet,count:"+count);
}
}
Visit http://localhost:8080/SpringMVC_01_war/a twice, and the results are as follows:
Here we can see that this filter is a singleton, and it was created only once. The count on the right will also increase as the number of visits increases. As mentioned in the spring source code, singletons have thread safety issues. To solve thread safety issues, locks and so on can be used.
Let's introduce several ways to configure filters without using web.xml.
- Use @WebServlet
to add @WebServlet annotation directly to the filter.
package com.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 龙小虬
* @date 2021/3/9 15:23
*/
@WebServlet("/")
public class MyServlet extends HttpServlet {
int count = 0;
public MyServlet(){
System.out.println("MyServlet无参构造执行。。。");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
count++;
resp.getWriter().print("this is new Servlet,count:"+count);
}
}
- Implement ServletContainerInitializer and
create MyHandlersTypes.java
package com.config;
/**
* @author 龙小虬
* @date 2021/3/9 16:41
*/
public class MyHandlersTypes {
}
Create MyServletContainerInitializer.java
package com.config;
import com.servlet.MyServlet;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.HandlesTypes;
import java.util.Set;
/**
* @author 龙小虬
* @date 2021/3/9 16:29
*/
@HandlesTypes(MyHandlersTypes.class)
public class MyServletContainerInitializer implements ServletContainerInitializer {
/**
* 容器初始化加载一些操作,手动加载监听器、过滤器,第三方依赖信息
* @param set 存储继承了MyHandlersTypes类所有子类的Class信息(注意只包含子类)
* @param servletContext
* @throws ServletException
*/
@Override
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
for (Class<?> s:set) {
System.out.println(s);
}
// 手动加入过滤器
ServletRegistration.Dynamic myServlet = servletContext.addServlet("myServlet", new MyServlet());
// 添加过滤器的拦截目标
myServlet.addMapping("/");
}
}
Create PayUtils.java and MemberUtils.java respectively
package com.config;
/**
* @author 龙小虬
* @date 2021/3/9 16:42
*/
public class MemberUtils extends MyHandlersTypes{
}
package com.config;
/**
* @author 龙小虬
* @date 2021/3/9 16:42
*/
public class PayUtils extends MyHandlersTypes{
}
The operation result will find that the configuration is not successful.
For ServletContainerInitializer is very special. He needs us to add a configuration (resources/META-INF/servicesjavax.servlet.ServletContainerInitializer)
to add the full path of MyServletContainerInitializer.java (com.config.MyServletContainerInitializer) internally. Just run it again.
The above configuration uses xml configuration, manual registration is also used, and a complex configuration of ServletContainerInitializer is used instead of xml configuration, but we will find that most of them are now using SpringBoot, why we do not have web.xml when we use SpringBoot What? Let's find out.