自己写一个MyServlet
package com.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet implements Servlet{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return servletConfig;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
ServletConfig servletConfig;
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
servletConfig=arg0;
init();
}
public void init() {
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//因为request没有getMethod()方法,所以需要他的子类
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse spe=(HttpServletResponse)response;
service(req,spe);
System.out.println(req.getMethod());
}
//重载service,当子类重写时原来父类的不会被重写
public void service(HttpServletRequest request,HttpServletResponse response) {
//对不同的请求做不同的回应
if(request.getMethod().equals("GET")) {
doGet(request,response);
}
if(request.getMethod().equals("POST")){
doPost(request,response);
}
}
//增加get和post方法供子类继承
public void doGet(HttpServletRequest request,HttpServletResponse response) {
}
public void doPost(HttpServletRequest request,HttpServletResponse response) {
}
}
然后用一个LoginServlet去继承MyServlet
package com.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends MyServlet{
@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
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
}
public void doGet(HttpServletRequest request,HttpServletResponse response) {
System.out.println("GET");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) {
System.out.println("POST");
}
}
所以
后来我们在eclipse直接去新建一个servlet的时候,IDE自动给我们完成这些事了,例如我们新建一个TestServlet,会自动继承HttpServlet
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}