Chapter 7 using JSP

  • JSP directive : <%@ page import="java.util.*" %>
  • Scriptlet : <% %> ==> goes into _jspService()
  • Expression: <%= %> ==> No semicolons at the end of statement
  • Declaration: <%! int count=0; %> ==> Class declaration . Variables & other methods definition . You can override jspInit() here.
  • Comment: <%-- --%>

* Implicit objects can be used with in Scriptlet are:
out, request,response,session(if session is enabled) , application, config, exception(only in errorpage) , pageContext (has reference to other implicit objects), page

These implicit objects are in fact java variable names pre defined in _jspService().

*JSP converted servlet life cycle: jspInit() ==> _jspService() ==> jspDestroy()
You can override jspInit and jspDestroy, but can’t override _jspService

*If you want to define Servlet initialization for a JSP,

<servlet>
	<servlet-name>test1</servlet-name>
	<jsp-file>/Test.jsp</jsp-file> <!--we use jsp-file instead of servlet-class-->
	<init-param>
		<param-name>email</param-name>
		<param-value>email@email.com</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>test1</servlet-name>
	<url-parttern>/Test.jsp</url-pattern> <!-- If you define servlet for JSP, you must define servlet mapping for this page.-->
</servlet-mapping>


*pageContext

  1. from JspContext
    • getAttribute(String name) // get Attribute from page scope
    • getAttribute(String name, int scope) //get attribute from the specific scope
    • getAttributeNamesInScope(int scope)
    • findAttribute(String name) // find all scopes order : page->request->session->application. If found , the search is stopped.
  2. static final fields
    APPLICATION_SCOPE, PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE
  3. methods to get other scopes
    getRequest(), getServletConfig() , getServletContext() , getSession()

Other JSP directive

<%@ page import="foo.*" session="false" %>
<%@ taglib tagdir="/WEB-INF/tags/cool" prefix="cool" %>
<%@ include file="test.html" %>

Page directive attribute

  • Import : class import
  • isThreadSafe : default is true . False ==> SingleThreadModel
  • contentType: mime type text/html default
  • isELIgnored : true if you want to disable EL
  • isErrorPage : true if this is the page used as error page
  • errorPage: Define the target error page to forward when error occurs
  • session: if false, session is off and session implicit object will not be available
  • buffer: defines how buffering is handled by the implicit out object
  • autoFlush: defulat is true. Defines if buffer is flushed automatically.
  • pageEncoding : default is “ISO-8859-1″

* Disable scripting in JSP (scriptlet, declaration, Java expression )

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

*Disable EL : There are two ways

  1. First way: define in DD
    <jsp-config>
    	<jsp-property-group>
    	<url-pattern>*.jsp</url-pattern>
    	<el-ignored>true</el-ignored> <!-- here! -->
    	</jsp-property-group>
    </jsp-config>
  2. Second way : <%@ page isELIgnored="true" %>

Page directive comes first than DD

(*) Next: [SCWCD] Cram Sheet 5 - scriptless JSP



One Response to “[SCWCD] Cram Sheet 4 - using JSP”

  1.   Remember the code? » Blog Archive » [SCWCD] Cram Sheet 3 - session management Says:

    [...] Next: [SCWCD] Cram Sheet 4 - using JSP Posted by HanaDaddy Filed in java Tags: java, SCWCD [...]

Leave a Reply