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

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

 

转载自:https://www.cnblogs.com/xing901022/p/4352999.html

大家都知道,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>
<span style="color: #008000;" data-filtered="filtered">&lt;!--</span><span style="color: #008000;" data-filtered="filtered"> 客户端可见 </span><span style="color: #008000;" data-filtered="filtered">--&gt;</span>
<span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">--</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> 客户端不可见 </span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">--</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span>
<span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">!
    </span><span style="background-color: #f5f5f5; color: #0000ff;" data-filtered="filtered">String</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> s </span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">=</span> <span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">xingoo</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">;</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">//</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">声明变量
    </span><span style="background-color: #f5f5f5; color: #0000ff;" data-filtered="filtered">int</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> add(</span><span style="background-color: #f5f5f5; color: #0000ff;" data-filtered="filtered">int</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> a,</span><span style="background-color: #f5f5f5; color: #0000ff;" data-filtered="filtered">int</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> b){</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">//</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">声明函数
        return a</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">b;
    }
</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span><span style="color: #000000;" data-filtered="filtered">
s=</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">=</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">s </span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span><span style="color: #0000ff;" data-filtered="filtered">&lt;</span><span style="color: #800000;" data-filtered="filtered">br</span><span style="color: #0000ff;" data-filtered="filtered">&gt;</span> <span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">--</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">表达式不以分号结束 </span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">--</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span><span style="color: #000000;" data-filtered="filtered">
1+2=</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">=</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">add(</span><span style="background-color: #f5f5f5; color: #800080;" data-filtered="filtered">1</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">,</span><span style="background-color: #f5f5f5; color: #800080;" data-filtered="filtered">2</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">) </span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span><span style="color: #0000ff;" data-filtered="filtered">&lt;</span><span style="color: #800000;" data-filtered="filtered">br</span><span style="color: #0000ff;" data-filtered="filtered">&gt;</span>
<span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">&lt;%</span>
    <span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">//</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">单行注释 ————————————————快捷键ctrl</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+/</span>
    <span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">/*</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> 多行注释 ____________快捷键ctrl</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">shift</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+/</span> <span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">*/</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered"> 
    out.println(</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">s=</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">s</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">+</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">&lt;br&gt;</span><span style="background-color: #f5f5f5; color: #800000;" data-filtered="filtered">"</span><span style="background-color: #f5f5f5; color: #000000;" data-filtered="filtered">);
</span><span style="background-color: #ffff00; color: #000000;" data-filtered="filtered">%&gt;</span>

</body>
</html>

View Code

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

  在生成.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 </span>= "xingoo";<span style="color: #008000;" data-filtered="filtered">//</span><span style="color: #008000;" data-filtered="filtered">声明变量</span>
    <span style="color: #0000ff;" data-filtered="filtered">int</span> add(<span style="color: #0000ff;" data-filtered="filtered">int</span> a,<span style="color: #0000ff;" data-filtered="filtered">int</span> b){<span style="color: #008000;" data-filtered="filtered">//</span><span style="color: #008000;" data-filtered="filtered">声明函数</span>
        <span style="color: #0000ff;" data-filtered="filtered">return</span> a+<span style="color: #000000;" data-filtered="filtered">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 </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
HttpSession session </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
ServletContext application </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
ServletConfig config </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
JspWriter out </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
Object page </span>= <span style="color: #0000ff;" data-filtered="filtered">this</span><span style="color: #000000;" data-filtered="filtered">;
JspWriter _jspx_out </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;
PageContext _jspx_page_context </span>= <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">;




</span><span style="color: #0000ff;" data-filtered="filtered">try</span><span style="color: #000000;" data-filtered="filtered"> {
  response.setContentType(</span>"text/html; charset=utf-8"<span style="color: #000000;" data-filtered="filtered">);
  pageContext </span>= _jspxFactory.getPageContext(<span style="color: #0000ff;" data-filtered="filtered">this</span><span style="color: #000000;" data-filtered="filtered">, request, response,
              </span><span style="color: #0000ff;" data-filtered="filtered">null</span>, <span style="color: #0000ff;" data-filtered="filtered">true</span>, 8192, <span style="color: #0000ff;" data-filtered="filtered">true</span><span style="color: #000000;" data-filtered="filtered">);
  _jspx_page_context </span>=<span style="color: #000000;" data-filtered="filtered"> pageContext;
  application </span>=<span style="color: #000000;" data-filtered="filtered"> pageContext.getServletContext();
  config </span>=<span style="color: #000000;" data-filtered="filtered"> pageContext.getServletConfig();
  session </span>=<span style="color: #000000;" data-filtered="filtered"> pageContext.getSession();
  out </span>=<span style="color: #000000;" data-filtered="filtered"> pageContext.getOut();
  _jspx_out </span>=<span style="color: #000000;" data-filtered="filtered"> out;


  out.write(</span>'\r'<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>'\n'<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>'    '<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;html&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;head&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;title&gt;Insert title here&lt;/title&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;/head&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;body&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t&lt;h1&gt;JSP&lt;/h1&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t&lt;!-- 客户端可见 --&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>'\r'<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>'\n'<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>'    '<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\ts="<span style="color: #000000;" data-filtered="filtered">);
  out.print(s );
  out.write(</span>"&lt;br&gt; "<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t1+2="<span style="color: #000000;" data-filtered="filtered">);
  out.print(add(</span>1,2<span style="color: #000000;" data-filtered="filtered">) );
  out.write(</span>"&lt;br&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"\t"<span style="color: #000000;" data-filtered="filtered">);


    </span><span style="color: #008000;" data-filtered="filtered">//</span><span style="color: #008000;" data-filtered="filtered">单行注释 ————————————————快捷键ctrl+/</span>
    <span style="color: #008000;" data-filtered="filtered">/*</span><span style="color: #008000;" data-filtered="filtered"> 多行注释 ____________快捷键ctrl+shift+/ </span><span style="color: #008000;" data-filtered="filtered">*/</span><span style="color: #000000;" data-filtered="filtered"> 
    out.println(</span>"s="+s+"&lt;br&gt;"<span style="color: #000000;" data-filtered="filtered">);

  out.write(</span>"\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;/body&gt;\r\n"<span style="color: #000000;" data-filtered="filtered">);
  out.write(</span>"&lt;/html&gt;"<span style="color: #000000;" data-filtered="filtered">);
} </span><span style="color: #0000ff;" data-filtered="filtered">catch</span><span style="color: #000000;" data-filtered="filtered"> (Throwable t) {
  </span><span style="color: #0000ff;" data-filtered="filtered">if</span> (!(t <span style="color: #0000ff;" data-filtered="filtered">instanceof</span><span style="color: #000000;" data-filtered="filtered"> SkipPageException)){
    out </span>=<span style="color: #000000;" data-filtered="filtered"> _jspx_out;
    </span><span style="color: #0000ff;" data-filtered="filtered">if</span> (out != <span style="color: #0000ff;" data-filtered="filtered">null</span> &amp;&amp; out.getBufferSize() != 0<span style="color: #000000;" data-filtered="filtered">)
      </span><span style="color: #0000ff;" data-filtered="filtered">try</span> { out.clearBuffer(); } <span style="color: #0000ff;" data-filtered="filtered">catch</span><span style="color: #000000;" data-filtered="filtered"> (java.io.IOException e) {}
    </span><span style="color: #0000ff;" data-filtered="filtered">if</span> (_jspx_page_context != <span style="color: #0000ff;" data-filtered="filtered">null</span><span style="color: #000000;" data-filtered="filtered">) _jspx_page_context.handlePageException(t);
    </span><span style="color: #0000ff;" data-filtered="filtered">else</span><span style="color: #000000;" data-filtered="filtered"> log(t.getMessage(), t);
  }
} </span><span style="color: #0000ff;" data-filtered="filtered">finally</span><span style="color: #000000;" data-filtered="filtered"> {
  _jspxFactory.releasePageContext(_jspx_page_context);
}

}
}

View Code

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

 

作者:xingoo
Github: https://github.com/xinghalo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: Java
1
1
« 上一篇: Eclipse配置Tomcat,访问404错误
» 下一篇: JSP中文乱码问题
	</div>
	<div class="postDesc">posted @ <span id="post-date">2015-03-20 10:24</span> <a href="http://www.cnblogs.com/xing901022/">xingoo</a> 阅读(<span id="post_view_count">11623</span>) 评论(<span id="post_comment_count">6</span>)  <a href="https://i.cnblogs.com/EditPosts.aspx?postid=4352999" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(4352999);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=128528,cb_entryId=4352999,cb_blogApp=currentBlogApp,cb_blogUserGuid='08d856da-3501-e211-aa3f-842b2b196315',cb_entryCreatedDate='2015/3/20 10:24:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值