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

JSP在请求的时候,会先转化成Servlet(其实就是个java类),然后生成class文件,再提供服务。 那么生成的java、class文件在哪呢?

首先应该了解的是Tomcat在Eclipse的映射关系:Tomcat的服务器目录配置

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

1 Use workspace metadata

2 Use Tomcat installation

3 Use custom location

分别对应三种情况说一下,注意要把Server中发布的目录全部删除,然后clean后才能修改该配置项。(示例如下:1、2、3、4、5)

 

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

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

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

这就是Tomcat映射的目录,在这个目录中有一个work目录和一个wtpwebapps目录(wtpwebapps目录则存放对应的部署资源文件)

work目录中顺着:work\Catalina\localhost\项目名字\org\apache\jsp 就可以找到你的项目对应的java文件和class文件。

我的:D:\program2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\jsp_demo\org\apache\jsp

 

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

如果勾选的use tomcat installation,那么你的tomcat目录将被选定为tomcat所安装的目录。

 此时,观察上面的:Server path在D:\apache-tomcat-9.0.36。很好找了!

注意要先访问jsp后,才会生成.class文件。上面的Server Locations选择Use workspace metadata 也一样,下面是,选择第二个(Use Tomcat installation...)的截图演示):

另外,选择第二个选项,代表Tomcat的部署目录,这个也能解决访问http://localhost:8080时,出现404。因为,启动一个tomcat,我们自己的worspace是没有相关的内容的(选择第一个),访问的话肯定会报404错误。

 

3.如果勾选第三项,Use custom location

目录的结构与上面的类似,上面这两个都是不可以修改的,要么是workspace所在的目录,要么是tomcat的目录。

选择该项,可以自定义生成的空间。

 

JSP代码示例:

<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%@page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <%--练习 --%>
    <%--1.声明类属性 --%>
    <%!private Integer id;
    private String name;
    private static Map<String, Object> map;%>
    <%--2.声明static静态代码块 --%>
    <%!static {
        map = new HashMap<String, Object>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
    }%>
    <%--3.声明类方法 --%>
    <%!public int abc() {
        return 12;
    }%>
    <%--4.声明内部类 --%>
    <%!public static class A {
        private Integer id = 12;
        private String abc = "abc";
    }%>
<%--练习:
1.输出整型
2.输出浮点型
3.输出字符串
4.输出对象    --%>
    <%=12%><br/>
    <%=12.12%><br/>
    
    <%="我是字符串"%><br/>
    <%=map%><br/>
    <%=request.getParameter("username")%><br/>

<%--练习 --%>
<%--1.代码脚本----if 语句 --%>
    <%
    int i=13;
    if(i==12){
    %>
    <h1>哈哈哈,我来了!</h1>
    <%
    }else{
    %>
    <h1>我走了</h1>
    <%
    }
    %>
    <br/>
    
    <%--2.代码脚本----for 循环语句 --%>
    <table border="1" cellspacing="0">
    <%
    for(int j=0;j<10;j++){
    %>
    <tr>
    <td>第<%=j+1%>行</td>
    </tr>
    <%
    }
    %>
    </table>
<%--3.翻译后Java文件中_jspService方法内的代码都可以写 --%>
    <%
    
    String username=request.getParameter("username");
    System.out.println("用户名的请求参数值:"+ username);
    
    %>

</body>
</html>

JSP页面的本质是一个Servlet程序。
当我们第一次访问jsp页面时,Tomcat服务器会帮我们把jsp页面翻译成一个java源文件。并对它进行编译为.class字节码程序。
我们打开Java源文件不难发现其里面的内容:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/9.0.36
 * Generated at: 2020-07-08 11:04:29 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.Map;
import java.util.HashMap;

public final class b_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {

private Integer id;
    private String name;
    private static Map<String, Object> map;
static {
        map = new HashMap<String, Object>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
    }
public int abc() {
        return 12;
    }
public static class A {
        private Integer id = 12;
        private String abc = "abc";
    }
  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.Map");
    _jspx_imports_classes.add("java.util.HashMap");
  }

  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 {

    if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
      final java.lang.String _jspx_method = request.getMethod();
      if ("OPTIONS".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        return;
      }
      if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
        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("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t");
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.write('\r');
      out.write('\n');
      out.write('\r');
      out.write('\n');
      out.write('    ');
      out.print(12);
      out.write("<br/>\r\n");
      out.write("\t");
      out.print(12.12);
      out.write("<br/>\r\n");
      out.write("\t\r\n");
      out.write("\t");
      out.print("我是字符串");
      out.write("<br/>\r\n");
      out.write("\t");
      out.print(map);
      out.write("<br/>\r\n");
      out.write("\t");
      out.print(request.getParameter("username"));
      out.write("<br/>\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');
      out.write('\r');
      out.write('\n');
      out.write('    ');

    int i=13;
    if(i==12){
    
      out.write("\r\n");
      out.write("\t<h1>哈哈哈,我来了!</h1>\r\n");
      out.write("\t");

    }else{
    
      out.write("\r\n");
      out.write("\t<h1>我走了</h1>\r\n");
      out.write("\t");

    }
    
      out.write("\r\n");
      out.write("\t<br/>\r\n");
      out.write("\t\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("\t<table border=\"1\" cellspacing=\"0\">\r\n");
      out.write("\t");

    for(int j=0;j<10;j++){
    
      out.write("\r\n");
      out.write("\t<tr>\r\n");
      out.write("\t<td>第");
      out.print(j+1);
      out.write("行</td>\r\n");
      out.write("\t</tr>\r\n");
      out.write("\t");

    }
    
      out.write("\r\n");
      out.write("\t</table>\r\n");
      out.write('\r');
      out.write('\n');
      out.write('    ');

    
    String username=request.getParameter("username");
    System.out.println("用户名的请求参数值:"+ username);
    
    
      out.write("\r\n");
      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);
    }
  }
}

 

posted @ 2020-07-08 19:44  江河湖泊  阅读(859)  评论(0编辑  收藏  举报