Chapter 8 scriptless JSP

Accessing the JavaBean object using Standard Action (<jsp: >)

<jsp:useBean id="person" class="foo.Person" scope="request" />
<jsp:getProperty name="person" property="name" />

*jsp:useBean action tries to find the attribute name in the defined scope when there is any attribute match id name, it creates a new object using the class info defined in the class attribute.

*The useBean’s id attribute and the getProperty’s name should match.
getProperty’s property attribute is for getter and setter.

*setProperty is to set the member of the java Bean object.
<jsp:setProperty name="person" property="name" value="Fred" />

* if setProperty tag is used within useBean tag body, then it is used only conditionally when the bean object is created

<jsp:useBean id="person" class="foo.Person" scope="page" >
	<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>

Read the rest of this entry »

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>

Read the rest of this entry »

Chapter 6 Session Management

Session

HttpSession ss = request.getSession();
ss.isNew();
// <== return True if just created.

  • JSESSIONID cookie 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 use response.encodeURL("/test.do") to print the URL or response.encodeRedirectURL("/test.do") for redirect

Session Methods

  • getCreationTime()
  • getLastAccessedTime()
  • setMaxInactiveInterval() : max time to keep the session in Seconds. Negative value ==> session never timeout
  • getMaxInactiveInterval()
  • invalidate() : Ends the session. Session ID no longer exists and attributes are removed.

DD : Define Max Session time in minutes.

<session-config>
	<session-timeout>15</session-timeout> 
</session-config>

For DD session-timeout value, 0 or negative means ==> Session never expires (little bit different from setMaxInactiveInterval )

Cookie

Read the rest of this entry »