Head First Servlets & JSP learning note Seventh--as a JSP

Source: Internet
Author: User

<%@.%> This is an instruction .

<%@ page ... import= "java.util.Date"%> This is the page directive , import is a property of the page directive

<%.%> This is scriptlet, Inside is pure Java code, need semicolon end

<% = ...%> this is an expression , the expression does not end with a semicolon; You must never use a method with a return type of void as an expression

The JSP is transformed by the Web container and a servlet is generated:

The contents of the scriptlet and expressions in the JSP are placed in the servlet's service () method, and therefore are local variables.

so , how do you declare the member variables and methods of the Servlet class? The answer is:JSP declaration

<%! ...%> this is the JSP declaration. For example <%! int count = 0;%>

The JSP declaration uses an exclamation point, followed by a semicolon ending.

To configure the servlet initialization parameters for the JSP:
1 <Web-app>2     <servlet>3         <Servlet-name>Mytestinit</Servlet-name>4         <Jsp-file>/testinit.jsp</Jsp-file> //This is different from the regular servlet 5         <Init-param>6             <Param-name>Email</Param-name>7             <Param-value>[Email protected]</Param-value>8         </Init-param>9     </servlet>Ten      One     <servlet-mapping>//When you define a servlet for a JSP, you must also define a servlet mapping for the JSP page A         <Servlet-name>Mytestinit</Servlet-name> -         <Url-pattern>/testinit.jsp</Url-pattern> -     </servlet-mapping> the </Web-app>
Overwrite the Jspinit () method: (This method is called by the servlet's init () method)
1  Public void Jspinit () {  //Use JSP declaration to overwrite Jspinit () method  2     servletconfig SCG =  Getservletconfig (); 3     String emailaddr = scg.getinitparameter ("email"); 4     5     ServletContext SCT = getservletcontext (); 6     Sct.setattribute ("Mail", emailaddr); 7 }
JSP adds a scope to the page scope:

The standard servlet scopes are three: request, session, application (context) scope;

JSP added page scope (PageContext)

In a JSP, you use 4 implicit objects to get and set properties: servlet in
  jsp (Implicit object) jsp (Implicit object)
Request scope (Request) Request.set Attribute ("foo", Object); request.setattribute ("foo", Object); request.getattribute ("foo");
Session scope (session) request.getsession (). SetAttribute ("foo", Object); session.setattribute ("foo", Object); session.getattribute ("foo");
Apply scope (context) getservletcontext (). SetAttribute ("foo", Object); application.setattribute ("foo", Object); application.getattribute ("foo");
page scope (pagecontext)   Not applicable pagecontext.setattribute ("foo", Object); pagecontext.getattribute ("foo");
JSPs can also be used by PageContext to get the properties of any scope, and to set the properties of any scope:

1. Set a session-scoped property: <% pagecontext.setattribute ("foo", Object, Pagecontext.session_scope); %>

2. Set an Application scope property: <% pagecontext.setattribute ("foo", Object, Pagecontext.application_scope); %>

3. Get an Application scope property: <% pagecontext.getattribute ("foo", Pagecontext.application_scope); %>

There are three types of directives:

Page directive, taglib directive, include directive

Disable script elements in JSP: (only this method)
1 <Web-app>2     <Jsp-config>3         <Jsp-property-group>4             <Url-pattern>*.jsp</Url-pattern>5             <Scripting-invalid> //This tag lets JSP prohibit the use of script elements 6 true7             </Scripting-invalid>8         </Jsp-property-group>9     </Jsp-config>Ten </Web-app>
Ignore El in JSP:

method One : Specify in Web. xml:

1 <Web-app>2     <Jsp-config>3         <Jsp-property-group>4             <Url-pattern>*.jsp</Url-pattern>5             <el-ignored> //This flag indicates that the EL is ignored 6 true7             </el-ignored>8         </Jsp-property-group>9     </Jsp-config>Ten </Web-app>

method Two : or through the page directive:

<%@ page iselignored= "true"%>

Actions-standard and non-standard actions:
1 <  page= "wickedfooter.jsp"/>  //Standard action    2<  var= "rate"  value= "/>" //non-standard action 

Head First Servlets & JSP learning note Seventh--as a JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.