在服务器里,jsp文件转换成java类,即是servlet类,本文解析下详情。
servlet类首次调用时,首先调用init方法(只调用一次),再调用service方法服务请求,卸载调用destroy方法。
javax.servlet.Servlet
package javax.servlet;
import java.io.IOException;
public abstract interface Servlet{
public abstract void init(ServletConfig paramServletConfig)throws ServletException;
public abstract ServletConfig getServletConfig();
public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)
throws ServletException, IOException;
public abstract String getServletInfo();
public abstract void destroy();
}javax.servlet.GenericServlet
package javax.servlet;
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
…
public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)
throws ServletException, IOException;
…
}
javax.servlet.http.HttpServlet
package javax.servlet.http;
public abstract class HttpServlet extends GenericServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{ … }
protected void doHead(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void doPut(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{…}
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
//根据http协议请求不同的method,调用对应的方法…
}
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);
}
}
org.apache.jasper.runtime.HttpJspBase
package org.apache.jasper.runtime;
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage {
//…
public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException;
public final void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
_jspService(request, response);
}
//…
}
例如,index.jsp转成index_jsp.java
public class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent{
//…
public void _jspService(HttpServletRequest request, HttpServletResponse response){
//将jsp文件中的内容调用out.write(…)返回给客户端
}
//…
}
..
本文详细解析了JSP文件转换为Servlet类的过程,并深入讲解了Servlet类的生命周期方法:init、service和destroy。通过具体代码示例,展示了如何将JSP页面映射为Servlet类,并在不同请求类型下执行相应的服务处理。
1543

被折叠的 条评论
为什么被折叠?



