#tomcat#生成的jsp转换问题

Posted 赵六六

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#tomcat#生成的jsp转换问题相关的知识,希望对你有一定的参考价值。

转载!

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转换问题的主要内容,如果未能解决你的问题,请参考以下文章

jsp页面被tomcat引擎运行的时候组装成java片段,但是这些java片段怎么没有main方法作为程序的入口啊?

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

websphere编译jsp生成的java类放在哪个目录

jsp&el&jstl-01

tomcat 5.5 jsp生成的class文件在哪里?

JSP基础