Monday, December 29, 2008

SCWCD books

Books for SCWCD Exam

1)Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam (SCWCD)

2) SCWCD Exam study kit by Deshmukh (Manning Publications)

3)Sun Certified Web Component Developer Study Guide

4)Professional SCWCD Certification


1 and 2 are sufficient for clearing the SCWCD exam

How to get radio button value in JSP?

Usually we have a group of radio button which means that multiple radio buttons are present with the same name e.g.



Somewhat related question may appear in the SCWCD exam asking for the correct code to retrieve values.

This will be done as:

request.getParameterValues() which returns an array of radio buttons against the name "t1". The array is String array and the code to display all the values is:



For any queries, leave your comment

Tuesday, December 16, 2008

what is usebean standard action

The SCWCD exam will definitely contain questions on jsp:useBean and its various attributes. So it is better to have a good hand at this standard action.

When coding a JSP document (a JSP in XML format), the use of jsp:useBean becomes inevitable to create and use beans. For creating a bean, one has to use the jsp:usebean tag in the following format


which is equivalent to

com.test.TestClass test = new com.test.TestClass();

Thus now we have the test object which is a java bean reference of type com.test.TestClass and this reference points to an object of com.test.TestClass.


But what if we want the type of reference to be polymorphic. In that case the standard action becomes:

which is equivalent to:

com.test.TestClassInterface test = new com.test.TestClass();

And now consider the case when we want to use an existing javabean reference which exists in the session scope. The standard action will now become:


Here, the bean reference will be set if there exists an object which is of type com.test.TestClassInterface in the session scope. Otherwise, there will be no effect.

By default the scope is page.
The only other important point to note is that either class or type or both attributes must be present in the useBean standard action. The SCWCD exam may ask a question like:

Which attribute of useBean standard action must be present when using an already existing bean?
a) beanName
b) name
c) type
d) class

We know that when class attribute is present, a new bean instance will be created. Thus c (type) is the correct option.

For info, remember that beanName attributes refers to the serialized bean file.

sessions for SCWCD exam

The purpose of this article is to include everything that can appear in the Sun Certified Web Component Developer (SCWCD) about sessions. Just read every line carefully and any question in SCWCD exam will be a cakewalk for you.

The sessions are managed by a web server by using the session API of J2EE. The session concept is about recognizing the user. A user who has just logged in should not be displayed login screen again until his session times out, he himself logs out or in simple words the session is not destroyed.

With servlets and JSP, session can be maintained on the client side in two ways viz. cookies and URL rewriting. By default cookies is the option used by the server. If cookies are disabled on the client side, then URL rewriting is used.

Cookies are small chunk of information stored on the client. For session purpose, it is a unique id which recognizes the user. The cookie writing and reading is handled by the container without the programmer being aware of this.

As mentioned earlier, when cookies are disabled on the client’s browser URL rewriting is used. In this case, the unique id which used to be stored as cookie is now appended to the current URL being browsed by the user. The id known as session id is appended with the name jsessionid. The only work needed by the programmer in this case is to encode the dynamic URL’s created in servlets and JSP using out.wite etc.
The encoding is done using the encodeURL method of the HttpServletResponse class. The syntax of this method is: java.lang.String encodeURL (java.lang.String URL)
This method accepts the URL in string format and returns the string which has the session id appended to it.
While using the sendRedirect method to redirect the user to some page, URL encoding is required and encodeURL won’t work. In that case, use encodeRedirectURl method which has the signature as: java.lang.String encodeRedirectURL(java.lang.String URL)

When writing a web application which maintains sessions, always remember to encode URL’s using the above methods to make your application failsafe (in case cookies have been disabled by the client).

The HttpSession class provides the methods related to obtaining and setting session related information. Here we discuss what all can be obtained using session.

We can get the Last accessed time using long getLastAccessedTime() method.
int getMaxInactiveInterval() returns the time after which the session will be timed out by default.
java.lang.String getId() returns a string containing the unique identifier assigned to this session.
long getCreationTime() returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
java.lang.Object getAttribute(java.lang.String name) returns the object bound with the specified name in this session, or null if no object is bound under the name.
void invalidate() invalidates this session and unbinds any objects bound to it which means that the user will not be recognized as having already using the application.

boolean isNew() returns true if the client does not yet know about the session or if the client chooses not to join the session.

void setMaxInactiveInterval(int interval) specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

void removeAttribute(java.lang.String name) removes the object bound with the specified name from this session.


The use of above methods is necessary in the sense that the SCWCD exam may ask some question like how can you get the age of the session? Now age means the time the session was created to now. If you observer carefully, the long getCreationTime() method returns the time the session was created and long getLastAccessedTime(). Thus with simple mathematics we can calculate the age of the session.
This is the kind of question which one can see on the exam.

Do post your comments about this article.

Saturday, December 13, 2008

Context root path, servlet path and path info

Let us understand context root path, resource path/ servlet path and path info:
The root directory within the webapps directory (of servlet container) is known as the root directory of the web application. This directory also contains WEB-INF directory which contains class and library files. Every servlet request must contain the name of this directory and is known as context root path.
The resource path/ servlet path is the virtual path which is mapped using the servlet mapping in the deployment descriptor file named web.xml
Any thing extra than and after the servlet path is known as path info.
Let us take an example:
Consider the URL http://localhost:80/project/testServlet/realTest
and the servlet mapping in the deployment descriptor be

Monday, December 8, 2008

session attributes

The session object in Servlet and JSP model can be used to store objects which are needed till the time a user is logged in to the web application. As you may be aware that the time a user is using a web application (technically logged in) is known as the session.

The objects that are stored programmatically to a session are known as session attributes. A session attribute can be any object which is retrieved/ created by a resource and is also needed by other resources for the time being the user has his session active but needs to be destroyed once the user exits. Thus session attributes will be created for each user but the contents will be different for different users. These session attributes can be added/ removed/ updated by any active resource participating in a web application.

The HttpSession object provides the API for performing the addition/ updation and deletion of session attributes. Here is an example to show the addition of attributes to session.

HttpSession session = request.getSession();

session.setAttribute(“test”,new Integer(3));

For getting the above added attribute in some other servlet use:

Integer I = (Integer) session.getAttribute(“test”);

Please note the typecasting in the above expression. This is necessary because the getAttribute method returns an object of type Object and its our duty to remember what kind of object was bound to the key “test”. To be on the safer side and avoid any exceptions it is better to use the following code to get a session attribute:

Integer I = (Integer) session.getAttribute(“test”);
if (I != null)
{
}
else
{
}

For updating the above session attribute, use:

session.setAttribute(“test”, new Integer(5));

To remove a session attribute, use

session.removeAttribute(“test”);

Thursday, November 20, 2008

include directive vs include standard action

The include directive is used to include the contest of a file statically whichc means the whole contents of another file will be included in the current file and then the current file will be compiled as a single unit.

This is different from what happens in case of jsp:include standard action or c:import. In both cases, when a request from the client comes to the including page, the included page is invoked and processed as single entity by the container and the output this generated is included in the including file.

The SCWCD exam may try to confuse you using the attribute names for include directive, include standard action and import JSTL tag.
Burry it that include directive has file as attribute, include standard action has page as directive and import JSTL tag has URL as attribute. Remember it like:

Include directive is for including files present in current web application statically and hence the name “file”
Include standard action for including files present in current web application dynamically and hence the name “page”
Import JSTL tag is for including files anywhere in any application and hence the name “URL”


The other thing that exam may test you on is the purpose of all of three of them which by now should be crystal clear.