通过百度可以知道:Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。
可以访问tomcat官方给的资料http://tomcat.apache.org/whichversion.html
###综合上面的版本关系,就可以选择正确的版本来开发Servlet,不会出现因版本问题产生的错误了(建议Servlet3.0以上) ###
public class FirstServlet extends HttpServlet {
//Servlet第一次被访问的时候初始化,init()方法被执行,并且只会初始化一次
@Override
public void init() throws ServletException {
super.init();
System.out.println("初始化方法调用了");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("这是第四个Servlet");
//request里面封装了关于请求的所有东西
System.out.println(request.getContextPath());
System.out.println(request.getRequestURI());
System.out.println(request.getMethod());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
public void destroy() {
super.destroy();
System.out.println("销毁方法调用了");
}
}
FirstServlet
com.maniy.web.servlet.FirstServlet
FirstServlet
/firstServlet
//loadOnStartup的数字表示启动顺序,越小越先初始化
@WebServlet(name = "FourServlet",urlPatterns={"/four.html"},loadOnStartup =2)
public class FourServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("这是第四个Servlet");
System.out.println(request.getContextPath());
System.out.println(request.getRequestURI());
System.out.println(request.getMethod());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}