码迷,mamicode.com
首页 > Web开发 > 详细

#tomcat#生成的jsp转换问题

时间:2018-03-22 17:31:25      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:run   exception   编码方式   分享   pre   inf   type   spin   web   

Eclipse中JSP生成的class文件去了哪里?

 

 

大家都知道,JSP在请求的时候,会先转化成Servlet(其实就是个java类),然后生成class文件,再提供服务。

那么生成的java、class文件在哪呢?Eclipse中根本找不到呀!

  首先应该了解的是Tomcat在Eclipse的映射关系,参考前一篇博文所述:Tomcat的服务器目录配置

  可以了解到,Tomcat在Eclipse中提供了三种位置配置选项:

  1 Use workspace metadata

  2 Use Tomcat installation

  3 Use custom location

  分别对应三种情况说一下,注意要把Server中发布的目录全部删除,然后clean后才能修改该配置项。

  如果你在Eclipse中双击Server配置选项,在Server Location中分别选了如下的选项:

技术分享图片

  如果Server Locations选择了第一项Use workspace metadata

  选了上面这项,你的服务器目录和发布目录将会如下:

技术分享图片

  服务器目录,即生成的字节码和java文件所在的目录。它在你的eclipse的工作目录中,比如我的工作目录是在F://workspace,那么在该目录下就可以看到这个.metadata目录了。

技术分享图片

  参考上面的配置目录F:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0

  这就是Tomcat映射的目录,在这个目录中有一个work目录和一个wtpwebapps目录

  work目录中顺着:work\Catalina\localhost\项目名字\org\apache\jsp 就可以找到你的项目对应的java文件和class文件(注意要访问jsp后才会出现!)

  wtpwebapps目录则存放对应的部署资源文件

  如果选择的是第二项,Use Tomcat installation

  如果勾选的use tomcat installation,那么你的tomcat目录将被选定为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

  由于该文件是常驻内存的,又是多线程调用,所以访问的效率和速度都会很快。

技术分享图片

  按照前面所述的方法,就可以看到生成的文件结构了。

  

  按照前面所述的方法,就可以看到生成的文件结构了。

  为了展示,这里给出一个样例的JSP

技术分享图片

<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%--
language 脚本使用的语言
import 加载类文件
contentType 编码方式
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>JSP</h1>

<!-- 客户端可见 -->
<%-- 客户端不可见 --%>
<%!
String s = "xingoo";//声明变量
int add(int a,int b){//声明函数
return a+b;
}
%>
s=<%=s %><br> <%--表达式不以分号结束 --%>
1+2=<%=add(1,2) %><br>
<%
//单行注释 ————————————————快捷键ctrl+/
/* 多行注释 ____________快捷键ctrl+shift+/ */
out.println("s="+s+"<br>");
%>
</body>
</html>

  里面含有一些注释,变量声明和打印输出等等。

  在生成.java文件中,可以看到生成的java文件:

技术分享图片
技术分享图片
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.io.*;

public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


        String s = "xingoo";//声明变量
        int add(int a,int b){//声明函数
            return a+b;
        }
    
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    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‘);
      out.write(‘\n‘);
      out.write(‘    ‘);
      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=utf-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t<h1>JSP</h1>\r\n");
      out.write("\t\r\n");
      out.write("\t<!-- 客户端可见 -->\r\n");
      out.write("\t");
      out.write(‘\r‘);
      out.write(‘\n‘);
      out.write(‘    ‘);
      out.write("\r\n");
      out.write("\ts=");
      out.print(s );
      out.write("<br> ");
      out.write("\r\n");
      out.write("\t1+2=");
      out.print(add(1,2) );
      out.write("<br>\r\n");
      out.write("\t");

        //单行注释 ————————————————快捷键ctrl+/
        /* 多行注释 ____________快捷键ctrl+shift+/ */ 
        out.println("s="+s+"<br>");
    
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else log(t.getMessage(), t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
技术分享图片

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.io.*;

public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {


String s = "xingoo";//声明变量
int add(int a,int b){//声明函数
return a+b;
}

private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

private static java.util.List _jspx_dependants;

private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;

public Object getDependants() {
return _jspx_dependants;
}

public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}

public void _jspDestroy() {
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
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‘);
out.write(‘\n‘);
out.write(‘ ‘);
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=utf-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<h1>JSP</h1>\r\n");
out.write("\t\r\n");
out.write("\t<!-- 客户端可见 -->\r\n");
out.write("\t");
out.write(‘\r‘);
out.write(‘\n‘);
out.write(‘ ‘);
out.write("\r\n");
out.write("\ts=");
out.print(s );
out.write("<br> ");
out.write("\r\n");
out.write("\t1+2=");
out.print(add(1,2) );
out.write("<br>\r\n");
out.write("\t");

//单行注释 ————————————————快捷键ctrl+/
/* 多行注释 ____________快捷键ctrl+shift+/ */
out.println("s="+s+"<br>");

out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else log(t.getMessage(), t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

  声明注释,都可以很详细的看到。

技术分享图片
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.io.*;

public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


        String s = "xingoo";//声明变量
        int add(int a,int b){//声明函数
            return a+b;
        }
    
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    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‘);
      out.write(‘\n‘);
      out.write(‘    ‘);
      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=utf-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t<h1>JSP</h1>\r\n");
      out.write("\t\r\n");
      out.write("\t<!-- 客户端可见 -->\r\n");
      out.write("\t");
      out.write(‘\r‘);
      out.write(‘\n‘);
      out.write(‘    ‘);
      out.write("\r\n");
      out.write("\ts=");
      out.print(s );
      out.write("<br> ");
      out.write("\r\n");
      out.write("\t1+2=");
      out.print(add(1,2) );
      out.write("<br>\r\n");
      out.write("\t");

        //单行注释 ————————————————快捷键ctrl+/
        /* 多行注释 ____________快捷键ctrl+shift+/ */ 
        out.println("s="+s+"<br>");
    
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else log(t.getMessage(), t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
技术分享图片

#tomcat#生成的jsp转换问题

标签:run   exception   编码方式   分享   pre   inf   type   spin   web   

原文地址:https://www.cnblogs.com/tnt-33/p/8624536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!