Javaweb执行流程
图解
servlet的继承关系
1,继承关系:
-------------|Servlet
------------------------|GenericServlet
---------------------------------------------|HttpServlet
-
重写doPost(HttpServletRequest request, HttpServletResponse response)方法
public class sel extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("name"); } }
<!--写好配置类后,交给前端控制器DispatcherServlet处理-->
<!-- 配置前端控制器 :统一接收请求,然后分配-->
<servlet> <servlet-name>myservlet</servlet-name> <servlet-class>com.jjk.servlet.myservlet</servlet-class> <!--加载springmvc核心配置文件 --> <!-- 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <!-- 启动就加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> 2 <url-pattern>/user</url-pattern> </servlet-mapping>
1, 前端用户发送请求,action=/user
2,在项目中,web.xml中找到<url-pattern>所对应的/user
3,在<servlet-mapping>标签中再找到所对应的<servlet-name>中的myservlet
4,再找到<servlet-name>中名字一致的servlet
5,再找到这个servlet所对应的<servlet-class> ---->com.jjk.servlet.myservlet
6,进入到所对应的类中,根据用户时使用什么方式进行提交的(method=post),因此tomcat会执行myservlet的dopost方法