JSP一些基礎知識

來源:互聯網
上載者:User

標籤:des   style   http   color   io   os   使用   ar   java   

因為jsp最後會轉換成servlet,這就有可能會覆蓋init和destory方法,但是在tomcat 6.0.29,如果要覆寫的話,如下:

 <%!    //覆蓋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. "_"的意思就是說不要覆寫。

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

  <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可以通過下面方式訪問其他範圍的屬性:

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

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

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

    如果沒有設定範圍,則會從page-->request-->session-->application由小到大順序尋找。


    JSP的三個指令:

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

    

    

    jsp中禁用scriptlet,java運算式和聲明。

  <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>

    頁面上的優先順序要高些。

JSP一些基礎知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.