jsp文件的编译

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
    <%     
        //out.write(session.toString()+"  "+session.isNew());
        if(session==null){
            out.write("session not creat");
        }
        else if(session.getAttribute("name")==null||session.getAttribute("name").equals("")){
            out.write("<a href='login.jsp'>登录</a>");
        }
        else{
            out.write(session.getAttribute("name")+"你好");
        }
    %>
    /
    <a href="index.jsp" onclick="logout()">注销</a>
    <script type="text/javascript">
        function logout(){
            <% 
                session.invalidate();
            %>
        }
    </script>
</body>
</html>
index.jsp

index.jsp编译后文件为

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.63
 * Generated at: 2016-10-13 10:50:27 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.*;

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

  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 javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  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 {

    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("
");
      out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
");
      out.write("<html>
");
      out.write("<head>
");
      out.write("<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
");
      out.write("<title>Insert title here</title>
");
      out.write("
");
      out.write("</head>
");
      out.write("<body>
");
      out.write("	");
     
        //out.write(session.toString()+"  "+session.isNew());
        if(session==null){
            out.write("session not creat");
        }
        else if(session.getAttribute("name")==null||session.getAttribute("name").equals("")){
            out.write("<a href='login.jsp'>登录</a>");
        }
        else{
            out.write(session.getAttribute("name")+"你好");
        }
    
      out.write("
");
      out.write("	/
");
      out.write("	<a href="index.jsp" onclick="logout()">注销</a>
");
      out.write("	<script type="text/javascript">
");
      out.write("		function logout(){
");
      out.write("			");
 
                session.invalidate();
            
      out.write("
");
      out.write("		}
");
      out.write("	</script>
");
      out.write("</body>
");
      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);
    }
  }
}
index.java

关于里面的javascript语言,如果写在<head>标签中的话在生成java代码时,session元素会先调用invalidate()方法,然后就无session对象了,导致后面的session对象不存在产生java.lang.IllegalStateException异常。所以在JavaScript中嵌套java语言时,要满足java编译的需求。

原文地址:https://www.cnblogs.com/ylw666/p/5957578.html