在Servlet技术中,HttpServlet可谓是一个核心类,在熟练运用的同时,我们还要透彻的分析其底层的工作原理,这样对于我们的实际中的开发,和日后的学习,都有非常大的帮助。HttpServlet是一个继承了GenericServlet的抽象类。主要是应用于Http协议的请求和响应。当容器接收到一个针对HttpServlet对象的请求时,调用该对象的方法顺序如下:
1.公共的service();
2.在公共的[color=red]service(ServletRequest req, ServletResponse res)[/color]方法中,首先将参数类型转换为HttpServletRequest和HttpServletResponse,然后再调用受保护的[color=red]service(HttpServletRequest req, HttpServletResponse resp)[/color]方法,并将转换后的参数传递进去。
*注意,我用红体标注的这两个方法,在HttpServlet中有两个service()方法,其中一个是最先调用的公共的[color=red]public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException[/color],然以,在参数转换完以后会调用[color=red]protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException[/color]。千万不要弄混!!
3.在执行受保护的service()方法时,HttpServletRequest对象会先调用getMethod()方法,来获得你提交请求的方式(例如,POST,GET),通过判断后会调用响应的doXXX()方法。在方法中的请求方式的判断其实就是对字符串的判断,下面我贴出部分源代码,你会发现,servlet的源代码中,不外乎就是java的基础代码,所以,Java基础非常重要。
[color=blue]公共的service():[/color]
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
}
catch(ClassCastException e)
{
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
static Class class$(String x0)
{
return Class.forName(x0);
ClassNotFoundException x1;
x1;
throw new NoClassDefFoundError(x1.getMessage());
}
}
[color=blue]受保护的service():[/color]
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
[color=red]String method = req.getMethod();[/color] [color=green]//获得请求的方式[/color]
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
[color=green]//进行对请求方式的判断[/color]
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
doPost(req, resp);
else
if(method.equals("PUT"))
doPut(req, resp);
else
if(method.equals("DELETE"))
doDelete(req, resp);
else
if(method.equals("OPTIONS"))
doOptions(req, resp);
else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
这就是HttpServlet在容器中运行时的基本步骤,随后我还会发其他的关于serlvet的文章,JSP和Servlet是J2EE的核心,熟练的掌握和运用这两项技术,我们才能子今后的学习与工作中更加的得心应手。
1.公共的service();
2.在公共的[color=red]service(ServletRequest req, ServletResponse res)[/color]方法中,首先将参数类型转换为HttpServletRequest和HttpServletResponse,然后再调用受保护的[color=red]service(HttpServletRequest req, HttpServletResponse resp)[/color]方法,并将转换后的参数传递进去。
*注意,我用红体标注的这两个方法,在HttpServlet中有两个service()方法,其中一个是最先调用的公共的[color=red]public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException[/color],然以,在参数转换完以后会调用[color=red]protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException[/color]。千万不要弄混!!
3.在执行受保护的service()方法时,HttpServletRequest对象会先调用getMethod()方法,来获得你提交请求的方式(例如,POST,GET),通过判断后会调用响应的doXXX()方法。在方法中的请求方式的判断其实就是对字符串的判断,下面我贴出部分源代码,你会发现,servlet的源代码中,不外乎就是java的基础代码,所以,Java基础非常重要。
[color=blue]公共的service():[/color]
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
}
catch(ClassCastException e)
{
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
static Class class$(String x0)
{
return Class.forName(x0);
ClassNotFoundException x1;
x1;
throw new NoClassDefFoundError(x1.getMessage());
}
}
[color=blue]受保护的service():[/color]
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
[color=red]String method = req.getMethod();[/color] [color=green]//获得请求的方式[/color]
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
[color=green]//进行对请求方式的判断[/color]
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
doPost(req, resp);
else
if(method.equals("PUT"))
doPut(req, resp);
else
if(method.equals("DELETE"))
doDelete(req, resp);
else
if(method.equals("OPTIONS"))
doOptions(req, resp);
else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
这就是HttpServlet在容器中运行时的基本步骤,随后我还会发其他的关于serlvet的文章,JSP和Servlet是J2EE的核心,熟练的掌握和运用这两项技术,我们才能子今后的学习与工作中更加的得心应手。