JSP一些基礎知識

由於jsp最後會轉換成servlet,這就有可能會覆蓋init和destory方法,可是在tomcat 6.0.29,若是要覆寫的話,以下:java

 <%!
    //覆蓋jspInit方法
    public void jspInit(){
    
    ServletConfig cfg = getServletConfig();
       String name = cfg.getInitParameter("name");
    ServletContext context = getServletContext();
       context.setAttribute("jsptest","hello");
    context.setAttribute("jsptesttwo",name);
    }
    
    public void jspDestroy(){
    //TODO
    }
     %>

不能覆寫_jspInit()和_jspDestory()方法,會hit duplicate error. "_"的意思就是說不要覆寫。express

在jspInit()中能夠像servlet中的init方法同樣拿到ServletConfig和ServletContext.給jsp配置初始化參數以下:apache

  <servlet>
  <servlet-name>testjsp</servlet-name>
  <jsp-file>/index.jsp</jsp-file>
  <init-param>
      <description>name</description>
      <param-name>name</param-name>
      <param-value>Rose</param-value>
  </init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>testjsp</servlet-name>
  <url-pattern>/index.jsp</url-pattern>
  </servlet-mapping>

    

    pageContext能夠經過下面方式訪問其餘做用域的屬性:tomcat

    pageContext.getAttribute("foo","name",PageContext.APPLICATION_SCOPE);session

    pageContext.getAttribute("foo","name",PageContext.SESSION_SCOPE);app

    pageContext.getAttribute("foo","name",PageContext.REQUEST_SCOPE);jsp

    若是沒有設置範圍,則會從page-->request-->session-->application由小到大順序查找。
url


    JSP的三個指令:spa

<%@page import="javax.servlet.jsp.jstl.core.Config"%>
<%@ taglib tagdir="" prefix="" %>
<%@ include file="" %>

    

    

    jsp中禁用scriptlet,java表達式和聲明。
code

  <jsp-config>
  <jsp-property-group>
  <url-pattern>*.jsp</url-pattern>
  <scripting-invalid>true</scripting-invalid>
  </jsp-property-group>
  </jsp-config>

    若是仍舊使用scriptlet或者java表達式這些,會hit error.

    org.apache.jasper.JasperException: /index.jsp(5,2) Scripting elements ( &lt;%!, &lt;jsp:declaration, &lt;%=,                  &lt;jsp:expression, &lt;%, &lt;jsp:scriptlet ) are disallowed here.


    在JSP中忽略EL:

    <%@ page isELIgnored="true" %>

  <jsp-config>
  <jsp-property-group>
  <url-pattern>*.jsp</url-pattern>
  <!-- <scripting-invalid>true</scripting-invalid> -->
  <el-ignored>true</el-ignored>
  </jsp-property-group>
  </jsp-config>

    頁面上的優先級要高些。

相關文章
相關標籤/搜索