1.Servlet基础类,必须继承HttpServlet
package com.fan; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public abstract class ServletBase extends HttpServlet {
public HttpServletRequest request = null;
public HttpServletResponse response = null; public ServletBase() {
} public void init() throws ServletException {
// Put your code here
System.out.println("init");
BeginInit();
} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
this.request = request;
this.response = response;
BeginGetIn("Get");
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
this.request = request;
this.response = response;
BeginGetIn("Post");
} public abstract void BeginGetIn(String Falg); public abstract void BeginInit(); public void OutPut(String str) {
try {
if (this.response != null) {
PrintWriter out = this.response.getWriter();
out.println(str);
out.flush();
out.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}// end
2.子类
package com.fan; public class HelloWorld extends ServletBase{ @Override
public void BeginGetIn(String Falg) {
// TODO Auto-generated method stub
System.out.println(Falg);
OutPut(Falg);
} @Override
public void BeginInit() {
// TODO Auto-generated method stub
System.out.println("init2");
} }
3.配置文件web.xml
4.请求地址
http://localhost:8888/项目名/servlet/ServletBase