In the definition of the class in the web project

Implement the interface Servlet

package HelloDemo;

public class Demo1 implements Servlet {

public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println ( "This is a service method");

}

@Override

public void destroy() {

// TODO Auto-generated method stub

}

@Override

public ServletConfig getServletConfig() {

// TODO Auto-generated method stub

return null;

}

@Override

public String getServletInfo() {

// TODO Auto-generated method stub

return null;

}

@Override

public void init(ServletConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

Method 2: servlet general wording

Let servlet class to inherit ,. implementation class HttpServlet
Package Penalty for HelloDemo;

public class Demo2 extends HttpServlet {

@Override

protected void service(HttpServletRequest arg0, HttpServletResponse arg1) 

throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println("Demo2! ");

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// TODO Auto-generated method stub

super.doGet(req, resp);

System.out.println("this is Get!");

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// TODO Auto-generated method stub

super.doPost(req, resp);

System.out.println("this is post!");

}

}

Guess you like

Origin blog.csdn.net/weixin_34174322/article/details/90983233