重写HttpServlet
Posted liu-ya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重写HttpServlet相关的知识,希望对你有一定的参考价值。
自己写一个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");
}
}
以上是关于重写HttpServlet的主要内容,如果未能解决你的问题,请参考以下文章
HttpServlet---getLastModified与缓存
JavaWeb基础 HttpServlet 浏览器405 继承而且没重写的doGetdoPost()却被调用