You are on page 1of 6

What will be the output of the following JSP code? <html><body> <% int a = 10; %> <%!

int a = 20; %> <%! int b = 30; %> The value of b multiplied by a is <%= b * a %> </body> </html>

A) The code will not compile B) The value of b multiplied by a is 30 C) The value of b multiplied by a is 300 D) The value of b multiplied by a is 600 E) The value of b multiplied by a is 0

[Ans: C] Although the variable "a" is declared twice, the code should still compile as written. In the first declaration <% int a = 10; %>, the variable "a" will be declared as a local variable. In the second declaration <%! int a = 20; %>, the variable "a" will be declared as an instance variable, global to the translated servlet class. Upon translation, the JSP engine will translate the code similar to the following:

public class ..._jsp { int a = 20; int b = 30; public void _jspService (....) { int a = 10; out.write ("The value of b multiplied by a is "); out.print (b * a); } Since the local variable "a" has precedence over the global variable "a", the expression (b * a) evaluates as (30 *10), which equals 300.

Consider the following code snippet of servlet code: public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String dbValue = getValueFromDB (); if (dbValue == null) response.sendError (HttpServletResponse.SC_NOT_FOUND, "DB Lookup Failed"); response.sendRedirect ("Form.jsp"); } If the getValueFromDB () method returns null , which of the following statements are true? A) The code will work without any errors or exceptions B) An IllegalStateException will be thrown C) An IOException will be thrown D) A NullPointerException will be thrown

[Ans: B] When the sendError() API is called, the response is considered committed and should not be written to subsequently. Once the response is considered committed, any subsequent redirect operations will throw an IllegalStateException. In this example, the sendError() is invoked when the dbValue is null, which will cause the response to be committed. Hence, the subsequent response.sendRedirect () method will then cause an IllegalStateException to be thrown.

Let's assume you are developing a custom tag handler using the BodyTag interface. The BodyTaginterface extends the IterationTag interface by defining additional methods that allow a tag handler to evaluate its body content in a temporary buffer. Upon execution, your tag handler returns the EVAL_BODY_BUFFERED return value from the doStartTag()method. In addition, your tag handler returns the EVAL_BODY_AGAIN value two times from thedoAfterBody() method. Based on this information, how many times will the setBodyContent() method be called by the JSP container?

A) Zero B) One C) Two D) Three

[Ans: B] The BodyTag interface extends IterationTag by defining additional methods that allow a tag handler to evaluate its body content in a temporary buffer. By buffering the body content, a tag handler can view and modify the body content before sending it to the output stream. In order to configure a custom tag handler to buffer the body content, the doStartTag() method of the custom tag handler must return the EVAL_BODY_BUFFERED value. If the doStartTag ()method returns the EVAL_BODY_BUFFERED value, a BodyContent object will be automatically created by the JSP container to capture the body evaluation. The BodyContent object is a subclass of JSPWriter and buffers any data written to it. The JSP container will then call thesetBodyContent () method to pass the newly created BodyContent instance to the custom tag handler. This setBodyContent () method is invoked after the doStartTag() method returns the EVAL_BODY_BUFFERED value. However, the setBodyContent () method is only called once. Regardless of how many times thedoAfterBody () method returns the EVAL_BODY_AGAIN value, the setBodyContent() method is only called once.

Let's assume you are deploying a legacy JSP page using a JSP container that complies with the JSP 2.0 specifications. In JSP 2.0, EL (Expression Language) constructs will be interpreted by default. However, you are concerned that this may cause problems for your legacy JSP application. Which of the following can be used to configure the JSP container to ignore EL expressions?

A) <%@ page isELIgnored="false" %> B) <%@ page isELIgnored="true" %> C) <%@ include isELIgnored="false" %> D) <%@ import isELIgnored="false" %>

[ANS: B]

When upgrading a web application to JSP 2.0, EL expressions will be parsed and interpreted by default. However, the isELIgnored page directive attribute can be used to deactivate EL for entire translation units. Alternatively, the escape sequence '\$' can be used to escape EL expressions that should not be interpreted by the container. Hence, the correct answer is: <%@ page isELIgnored="true" %>.

Let's assume you have a customerBean which has an address property object. This address object has a city property. Which of the following is a valid way to set the city property of the customerBean.address object using the appropriate JSTL tags?

A) <c:set target="${customerBean.address}" property="city" value = "${city}" /> B) <c:set property="customerBean.address" value="${city}" /> C) <c:set target="${customerBean}" property="address.city" value = "${city}" /> D) <c:set var="${customerBean.address}" property="city" value = "${city}" />

[ANS: A] To set the property of a target object (JavaBean object with setter / getter methods), the proper syntax is as follows: <c:set target="target" property="propertyName" value="value" /> In this example, the target attribute evaluates to a JavaBean. The target attribute value should NOT just be the name of the bean class, but rather refer to the bean object using an EL expression: ${beanName.property}. The property attribute specifies the name of the property to be set in the target object (in this case, city). And the value attribute specifies the expression to be evaluated. The <c:set> tag follows the general rules for getting and setting values in a JavaBean. Hence, the correct answer is: <c:set target="${customerBean.address}" property="city" value = "${city}" />

Let's assume you are developing a web application. One of the requirements of this application is to design a flexible framework so that services can be easily added and removed without affecting existing components. For instance, services such as logging, authentication, debugging, compressing and encoding output, etc.. should be "pluggable" without requiring changes to the core application code. Which of the following design patterns can be used in this situation?

A) Intercepting Filter B) Transfer Object C) Business Delegate D) Data Access Object

[Ans: A] The key to solving this problem in a flexible manner is to have a simple mechanism for adding and removing processing components, in which each components completes a specific action. Specifically, the Intercepting Filter design pattern can be used when pre-processing and post-processing of a client Web request and response are required. Pluggable filters can be created to intercept all incoming and outgoing services, and process common services in a standard manner.

As the filters intercept incoming and outgoing requests, the filters can decorate the main processing logic with a variety of common services, such as security, authentication, logging, encoding, etc. As these filters may be added or removed declaratively through the deployment configuration file, little changes are required from the core application source code to dynamically add and remove services.

Let's assume you are writing a JSP page using EL expressions. You have an variable declared asjava.util.Map customerMap. You need to retrieve the customer info that is stored in this map. Which of the following methods is invoked by the JSP engine upon execution of the following EL expression: ${customerMap[CustomerA]}?

A) customerMap.get (pageContext.findAttribute ("CustomerA")); B) customerMap.get("CustomerA") C) customerMap.remove ("CustomerA"); D) customerMap.getProperty ("CustomerA"); E) customerMap.getCustomerA ();

[Ans: A] Expressions with syntax '${identifier[subexpression]}' are considered EL expressions. If theidentifier is a MAP, the following rules apply: The subexpression is regarded as one of the map's keys. The expression evaluates to identifier.get (subexpression). Specifically,${customerMap["CustomerA"]} and ${customerMap[CustomerA]} are NOT functionally equivalent. Please note that the former specifies a STRING for the key, whereas the latter specifies an IDENTIFIER for the key. When an IDENTIFIER is used as the key, the PageContext.findAttribute method is invoked by the JSP engine, which searches all FOUR JSP scopes (page, request, session, and application scopes) for a scoped variable with a matching name.On the other hand, if a STRING is specified, the JSP engine invokes the get() method on the map itself. In this example, an IDENTIFIER is specified for the key, rather than a String. Hence, the JSP engine will invoke the PageContext.findAttribute () method, instead of directly invoking the map'sget() method.

What will be the output of the following JSP page? <html><body> <% a = 100; %> <% int a = 200; %> <%! int a = 300; %> a = <%= a %>, <%= this.a %> </body></html>

A) a = 200, 100 B) a = 300, 100 C) a = 100, 200 D) a = 200, 300 E) The code will not compile as written

[Ans: A]
The JSP page will be translated into servlet code similar to the following:

public class ...._jsp extends HttpJspBase { int a = 300; public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { out.write("<html>"); out.write("<body>"); a = 100; int a = 200; out.write("a = "); out.print( a); out.write(", "); out.print( this.a); out.write("</body>"); out.write("</html>"); } } The <%! int a = 300; %> declaration will create a class-level instance variable "a", and initialize the variable to 300. The scriplet <% a = 100; %> will change the value of the class-level instance variable from 300 to 100. The second scriplet, <% int a = 200 %>, will create a local variable "a" in the _jspService () method and set the initial value to be 200. The first expression <%= a %> refers to the local variable "a", which is 200. The second expression <%= this.a %> uses the keyword this and refers to the class-level instance variable "a". The class-level instance variable "a" was set to 100 by the scriplet <% a = 100; %>. Hence, the correct answer is "a=200, 100"

Consider the following code: public class MyHttpSessionListener implements HttpSessionListener { public void sessionCreated (HttpSessionEvent e) { System.out.log ("Session Created"); } public void sessionDestroyed (HttpSessionEvent e) { HttpSession session = e.getSession (); String name = (String) session.getAttribute ("username"); } }

Which of the following statements best describes the code?

A) A compilation error will occur as the HttpSessionListener class is not properly implemented B) No errors or exceptions will occur as the code is written correctly C) An IllegalStateException will be thrown when the code is executed D) None of the above

[Ans: C]
The HttpSessionListener interface only defines the sessionCreated () and sessionDestroyed () method calls. Hence, the code properly implements the interface correctly. So a compilation error should NOT occur. However, at code execution, an IllegalStateException will be thrown when the session.getAttribute ("name") is called. By the time the servlet container calls the sessionDestroyed () method, the session has already been invalidated. Therefore a call to getAttribute () will throw an IllegalStateException.

You might also like