[SCWCD] Cram Sheet 4 – using JSP
April 22, 2008
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><servlet -name>test1</servlet> <jsp -file>/Test.jsp</jsp> <!--we use jsp-file instead of servlet-class--> <init -param> <param -name>email</param> <param -value>email@email.com</param> </init> <servlet -mapping> </servlet><servlet -name>test1</servlet> <url -parttern>/Test.jsp</url> <!-- If you define servlet for JSP, you must define servlet mapping for this page.-->
[SCWCD] Cram Sheet 3 – session management
April 20, 2008
Chapter 6 Session Management
Session
HttpSession ss = request.getSession();// < == return True if just created.
ss.isNew();
JSESSIONIDcookie name is used for session management. Its automatically done by Container.request.getSession(false);// Do not create new session. Only get preexisting session.request.getSession(true);// Create new session. Same as the getSession()- HttpSessionEvent or HttpSessionBindingEvent have getSession() function.
- If cookie is disabled, isNew will always return TRUE
- URL writing : if client does not support cookie, container automatically use URL rewriting for session support. It adds
";jsessionid=1234567"at the end of the URL. However you need to useresponse.encodeURL("/test.do")to print the URL orresponse.encodeRedirectURL("/test.do")for redirect getCreationTime()getLastAccessedTime()setMaxInactiveInterval(): max time to keep the session in Seconds. Negative value ==> session never timeoutgetMaxInactiveInterval()invalidate(): Ends the session. Session ID no longer exists and attributes are removed.
Session Methods
DD : Define Max Session time in minutes.
<session -config> </session><session -timeout>15</session>
For DD session-timeout value, 0 or negative means ==> Session never expires (little bit different from setMaxInactiveInterval )
Cookie
[SCWCD] Cram Sheet 2 – attribute and listeners
April 18, 2008
Chapter 5 Attribute and listeners
Servlet Init Parameter : available only for that servlet. Defined within the specific servlet.
DD
<servlet> </servlet><servlet -name>Servlet Name</servlet> <servlet -class>com.test.ServletClass</servlet> <init -param> <param -name>Email</param> <param -value>admin@email.com</param> </init>
In servlet
ServletConfig sc = getServletConfig(); String email = sc.getInitParameter("EMAIL");
- You can use getInitParameterNames() to go through all init parameters
- Also note ServletConfig will not be available in forwarding JSP. The ServletConfig init parameters must be passed to JSP as other scope’s Attributes.
Context Init Parameter: available for all the servlets in the application
Note that the <context-param> element does not reside within <servlet> element
<servlet> ... </servlet> <context -param> <param -name>AlertEmail</param> <param -value>alertall@email.com</param> </context>
In servlet
ServletContext scx = getServletContext(); String email = scx.getInitParameter("AlertEmail");
- Context Param can be accessed from any Servlet and JSP within the application









