在web工程中对类的定义

实现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("这是service方法");

}

@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

}

}

方法2:servlet的通用写法

让类去继承servlet的实现类httpservlet,.
package 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!");

}

}

你可能感兴趣的:(在web工程中对类的定义)