Jsp页面看上去和HTML相似,但它实际上是作为Servlet运行的。
当JSP页面第一次被访问时,web容器解析jsp文件并将其转化为相应的java文件,该文件声明了一个servlet类,该类称为页面的实现类。
接着才是web容器编译该类并将其装入内存中,与其他的servlet一样执行并输出结果。
现在就聊一聊 jsp->servlet类的java文件存放位置。
我们一般开发时,使用Tomcat,其在Eclipse中提供了三种位置配置选项:
1. Use workspace metadata
2. Use Tomcat installation
3. Use custom location
在Server面板中双击Tomcat就可以进入配置界面:
如果Server Locations选择了第一项Use workspace metadata
选了上面这项,你的服务器目录和发布目录将会如下:
项目的服务器目录,即生成的字节码和java文件所在的目录。它在你的eclipse的工作目录中,比如我的工作目录是在D:\javaProject\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\项目名字\org\apache\jsp中找到自己项目,然后按照文件在项目中的具体文件夹找。 就可以找到你的项目对应的java文件和class文件(注意要访问jsp后才会出现)
如果选择的是第二项,Use Tomcat installation
那么你的项目的页面目录将被选定为tomcat所安装的目录。
如果勾选第三项,Use custom location
目录的结构与上面两种的类似,上面这两个都是不可以修改的,要么是workspace所在的目录,要么是tomcat的目录。
选择该项,可以自定义生成的空间。
JSP的生命周期
用户把工程部署到tomcat中,然后启动tomcat!此时就可以访问jsp了、
1 第一次访问JSP,会验证一下是否第一次访问,然后把JSP转化成java(Servlet),再编译成class文件。
2 生成的class文件中会自动生成几个方法:jspInit()、jspDestroy()、jspService().Tomcat仅仅在第一次请求时,调用jspInit方法,然后调用jspService进行处理。
3 之后的每个请求,都会分配一个线程调用jspService方法。
4 如果页面被销毁或者关闭,都会调用jspDestroy
由于该文件是常驻内存的,又是多线程调用,所以访问的效率和速度都会很快。
对应的生成示例:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.0.M13
* Generated at: 2017-03-28 12:35:34 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;
import java.text.DateFormat;
public final class todayDate_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = new java.util.HashSet<>();
_jspx_imports_classes.add("java.util.Date");
_jspx_imports_classes.add("java.text.DateFormat");
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=utf-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\r\n");
DateFormat dateForMat=DateFormat.getDateInstance(DateFormat.FULL);
String s=dateForMat.format(new Date());
out.write("\r\n");
out.write("今天的日期是:");
out.print( s );
out.write(">\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}