• No results found

What is JSP EL (Expression Language)?

Model-1 pattern

OST )(form

Q. What is JSP EL (Expression Language)?

One major component of JSP 2.0 is the new expression language named EL. EL is used extensively in JSTL (Java Standard Tag Library). However EL is a feature of JSP and not of JSTL. The EL is a language for accessing runtime data from various sources. JSP EL variables come from one of 2 ways:

1. Implicit variables as shown below:

Implicit variable Description Example param A collection of all

request parameters as a single string value for each parameter.

paramValues A collection of all request parameters as a

header A collection of all request headers as a single string value for each header.

headerValues A collection of all request headers as a string array value for each header.

${header['User-Agent']}

you must use the array syntax for the header, because the name includes a dash. otherwise it would be interpreted as the value of the variable expression “header.User” minus the value of the variable named “Agent”.

Parameter values, headers and cookies for the current request.

cookie A collection of all request cookies as a single

javax.servlet.http.Cookie instance value for each cookie.

<c:if test=”${ ! empty cookie.userName}”>

Welcome back

<c:out value=”${cookie.userName.value}”>

</c:if>

Defined in web.xml initParam A collection of all application init

pageContext An instance of the javax.servlet.jspPageCo ntext class.

PageContext.getRequest () Æ ${pageContext.request}

PageContext.getResponse () Æ ${pageContext.response}

PageContext.getSession() Æ ${pageContext.session}

PageContext.getServletContext() Æ

${pageContext.servletContext}

<c:if test=”${pageContext.request.method=’POST’}”>

….

</c:if>

pageScope A collection of all page scope objects.

requestScope A collection of all request scope objects.

sessionScope A collection of all session scope objects.

collections containing all objects in each specific scope. You can use these to limit the search for an object to just one scope instead of searching all scopes, which is the default if no scope is specified

applicationScope A collection of all application scope objects.

<c:out value=”${requestScope.city}” />

<c:out value=”${sessionScope.city}” />

2. Find the first of using: pageContext.findAttribute (varname) which is like getting the first of:

page.getAttribute(varname);

request.getAttribute(varname);

session.getAttribute(varname);

application.getAttribute(varname);

<c:out value=”${city}” />

Q. What is the difference between a JspWriter denoted by the “out” implicit object and the PrintWriter object obtained from response.getWriter() method?

JSPs should use the JspWriter denoted by the “out” implicit object for sending output back to the client. A JspWriter is a buffered version of the PrintWriter. Refer JspWriter API for details. JspWriter also differs from a PrintWriter by throwing java.io.IOException, which a PrintWriter does not. The advantage of throwing an exception is that if your HTTP connection is broken for some reason, your JSP won’t sit there trying to send characters to a broken connection.

Q 33: Explain hidden and output comments? SF

A 33: An output comment is a comment that is sent to the client where it is viewable in the browser’s source. CO

<!-- This is a comment which is sent to the client -->

A hidden comment documents a JSP page but does not get sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.

<%-- This comment will not be visible to the client --%>

Q 34: Is JSP variable declaration thread safe? CI FAQ

A 34: No. The declaration of variables in JSP is not thread-safe, because the declared variables end up in the generated Servlet as an instance variable, not within the body of the _jspService() method.

The following declaration is not thread safe: because these declarations end up in the generated servlet as instance variables.

<%! int a = 5 %>

The following declaration is thread safe: because the variables declared inside the scriplets end up in the generated servlet within the body of the _jspService() method as local variables.

<% int a = 5 %>

Q 35: Explain JSP URL mapping? What is URL hiding or protecting the JSP page? SF SE FAQ

A 35: As shown in the figure, the JSP resources usually reside directly or under subdirectories (e.g. myPath) of the document root, which are directly accessible to the user through the URL. If you want to protect your Web resources then hiding the JSP files behind the WEB-INF directory can protect the JSP files, css (cascading style sheets) files, Java Script files, pdf files, image files, html files etc from direct access. The request should be made to a servlet who is responsible for authenticating and authorizing the user before returning the protected JSP page or its resources.

http://<hostname:port>/<webapp name><pathname>/<resourcename>

myPage.jsp is hidden or protected. cannot be directly accessed through URL myPage.jsp is directly accessible through URL

MyApps

Q 36: What is JSTL? What are custom tags? Explain how to build custom tags? SF FAQ

A 36: JSTL stands for Java Standard Tag Library and is nothing more than a set of simple and standard tag libraries that encapsulates the core functionality commonly needed when writing dynamic JSP pages. JSTL was introduced to allow JSP programmers to code with tags rather than embedding Java code as scriptlets.

Using scriptlets Using JSTL tags

<html>

The above JSP code is hard to read and maintain.

<%@ taglib prefix=”c”

uri=”http//java.sun.com/jstl/core”>

<html>

<head><title>simple example<title></head>

<body>

<c:forEach var=”i” begin=”1” end=”5” step=”1”>

<c:out value=”${i}”> <br/>

</c:forEach>

</body>

</html>

The above JSP code consists entirely of HTML & JSTL tags (in bold).

JSTL consists of 4 tag libraries:

Description Tag Prefix

(recommended)

Example Core Tag Library – looping,

condition evaluation, basic input,

Tag Library – parse data such as number, date, currency etc

fmt <fmt:formatNumber value=”${now.time}” />

XML Tag Library – tags to

access XML elements. x <x:forEach select="$doc/books/book" var="n">

<x:out select="$n/title" />

</x:forEach>

Database Tag Library – tags to sql <sql:query var=”emps” sql=”SELECT * FROM Employee”>

access SQL databases and should be used only to create prototype programs.

Outline

Related documents