Each of the JSP elements is written in a different way. The following table roughly indicates the syntax for each kind of JSP element. This is just an outline so you get the overall flavor of JSP syntax. We cover the formal specification of JSP syntax in more detail later. Remember you can use the <% syntax and the XML syntax to write most elements.
Element | <% Syntax | XML Syntax |
Output comments | <!-- visible comment --> | <!-- visible comment --> |
Hidden comments | <%-- hidden comment --%> | <%-- hidden comment --%> |
Declarations | <%! Java declarations %> | <jsp:declaration> Java language declarations </jsp:declaration> |
Expressions | <%= A Java expression %> | <jsp:expression> A Java expression </jsp:expression> |
Scriptlets | <% Java statements %> | <jsp:scriptlet> Java statements </jsp:scriptlet> |
Directives | <%@ ... %> | <jsp:directive. type ... /> |
Actions | <jsp: action ... /> or <jsp: action > ... </jsp: action > |
<%-- I am a comment --%>
This comment is known as a hidden comment because it is an actual JSP comment and does not appear in the generated servlet. The user never sees the comment and won't have any way to know of its existence.
session | Indicates whether the page requires participation in an HttpSession. By default, the value is true. You have the option of specifying the value as false (in that case, any reference to HttpSession in the page will not be resolved). |
buffer | This page attribute indicates whether the content output from the page will be buffered. By default, the page output is buffered with an implementation buffer size no smaller than 8 KB. |
autoFlush | Specifies whether the buffered output should be flushed automatically (true value) when the buffer is filled, or whether an exception should be raised (false value) to indicate buffer overflow. The default value is true. |
isThreadSafe | Indicates whether the resulting servlet is threadsafe. The JSP 2.0 specification advises against using isThreadSafebecause the generated servlet might implement the SingleThreadModel interface, which has been deprecated in the Servlet 2.4 specification. |
<%@ include file="filename" %>
The include directive is useful for including reusable content into your JSP, such as common footer and header elements.
The difference between the jsp:include action and the include directive is that the action dynamically inserts the content of the specified resource at request time, whereas the directive physically includes the content of the specified file into the translation unit at translation time.
The jsp:include action is written as:
<jsp:include page=" relativeURL " flush="true" /> |
Important: You do not terminate the expression with a semicolon.
For HTTP protocols, the error status code 500 is returned.
<error-page> <exception-code>500</exception-code> <location>/MyErrorPage.jsp</location> </error-page> |
<jsp-property-group> <url-pattern>jsp2/*.jsp</url-pattern> <page-encoding>ISO-8859-1</page-encoding> </jsp-property-group> |
Implicit objects descriptions
Object | Class | Purpose |
out | javax.servlet.jsp.JspWriter | The output stream |
request | javax.servlet.ServletRequest | Provides access to details regarding the request and requester |
response | javax.servlet.ServletResponse | Provides access to the servlet output stream and other response data |
session | javax.servlet.http.HttpSession | Supports the illusion of a client session within the HTTP protocol |
pageContext | javax.servlet.jsp.PageContext | Used extensively by the generated JSP code to access the JSP environment and user beans |
config | javax.servlet.ServletConfig | Servlet configuration information |
page | java.lang.Object | The page itself |
application | javax.servlet.ServletContext | Represents the Web application; provides access to logging methods |
exception | java.lang.Throwable | For error pages only; the exception that caused the page to be invoked |
<% response.setContentType("text/html"); %>
<% String userName = (String)session.getAttribute("username"); %>
<% java.lang.Exception e = pageContext.getException() if (e.getMessage().equals("testException") { %> |
The methods of the JspPage interface:
void jspInit();// allows user action when initialized void jspDestroy();// allows user action when destroyed public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; |
You'll most likely use this object to gain access to logging:
application.log(String); application.log(String, Throwable); |
The jsp:forward action tells the JSP container that you want to forward the request to another resource whose content will substitute for your content. The resource is specified by a relative URL and can be dynamic or static.
The jsp:forward action is written as:
<jsp:forward page=" relativeURL " /> |
Velocity has several advantages over JSP:
- Velocity's biggest advantage is that it is interpreted at run time. This means you can read templates from anywhere, even from a database, and the changes are instantly reflected.
- Velocity helps enforce a clean separation between the view layer and the model/control layers.
- Velocity, which caches templates for speed, reportedly has performance comparable or better than JSP.
- Velocity templates are not limited to HTML, and you can use it to produce any type of text output, including XML, SQL, ASCII, and PostScript.
- JSP 2.0 supports JSTL. So, JSP has the advantage of existing taglibs that make usage easy.
- In JSP 2.0, expression language makes coding JSPs simple.
- In JSP 2.0, tag files make writing custom tags easy.
- JSP inherently works with server-side code.
- JSP has long been a standard with a large following of experienced programmers.
jsp:getProperty takes two attributes - name and property. The name attribute must match the id attribute of jsp:usebean.
The jsp:setProperty takes three attributes - name, property and value. Also the jsp:setProperty must end with />.
Using * for property is legal syntax. Bean properties are associated with identically named input parameters.
<jsp:useBean
id="beanInstanceName"
scope="page | request | session | application"
{
class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{package.class | <%= expression %>}" type="package.class"
}
- Which of the following represents a correct syntax for usebean. Select the two correct answers.
- <jsp:usebean id="fruit scope ="page"/>
- <jsp:usebean id="fruit type ="String"/>
- <jsp:usebean id="fruit type ="String" beanName="Fruit"/>
- <jsp:usebean id="fruit class="Fruit" beanName="Fruit"/>
- Name the default value of the scope atribute of <jsp:usebean>.
- page
<jsp:directive.include> is the XML equivalent of include directive.
request is an instance of HttpServletRequest
Implicit object session is of type HttpSession.
- Which of the following are examples of JSP directive. Select the two correct answers.
- include
- exclude
- import
- taglibrary
- servlet
- page
<%@directive attribute="value" %>
When using the include directive, the JSP container treats the file to be included as if it was part of the original file.
Name the implicit variable available to JSP pages that may be used to access all the other implicit objects.
This pageContext object is an instance of type javax.servlet.jsp.PageContext, and provides methods like getPage(), getRequest(), etc. to access other input variables.
What are the different ways for session tracking?
Cookies, URL rewriting, HttpSession, Hidden form fields
What mechanisms are used by a Servlet Container to maintain session information?
Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information
Difference between GET and POST
In GET your entire form submission can be encapsulated in one URL, like a hyperlink. query length is limited to 255 characters, not secure, faster, quick and easy. The data is submitted as part of URL.
In POST data is submitted inside body of the HTTP request. The data is not visible on the URL and it is more secure.
What is session?
The session is an object used by a servlet to track a user’s interaction with a Web application across multiple HTTP requests. The session is stored on the server.
What is servlet mapping?
The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets.
What is servlet context ?
The servlet context is an object that contains a information about the Web application and container. Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use.
How do I include static files within a JSP page?
Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase.
How can I implement a thread-safe JSP page?
You can make your JSPs thread-safe adding the directive <%@ page isThreadSafe="false" % > within your JSP page.
How do I prevent the output of my JSP or Servlet pages from being cached by the browser? - You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
No comments:
Post a Comment