1. 程式人生 > 實用技巧 >Eclipse中JSP生成的class檔案去了哪裡?

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>

在生成.java檔案中,可以看到生成的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);
    }
  }
}