这篇文章主要介绍了Java servlet执行流程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、先附上代码如下
Servlet1.java
public class Servlet1 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 {
//返回到浏览器
res.getWriter().write("hello servlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
servlet1
com.rookie.bigdata.Servlet1
servlet1
/servlet1
index.jsp
2、整体流程图如下:
3、针对上面代码的详细流程如下
1、启动tomact加载应用和web.xml文件
2、http访问程序 http://localhost:8080/servlet1,最终通过web.xml文件映射为com.rookie.bigdata.Servlet1
3、实例化Servlet1对象
4、调用Servlet1中的init方法
5、调用service方法接受浏览器请求
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。