Tuesday, 18 April 2017

JSP directives

  1. JSP directives
1.    page directive
2.    Attributes of page directive
The jsp directives are messages that tells the web container how to translate a JSP page into the corresponding servlet.
There are three types of directives:
  • page directive
  • include directive
  • taglib directive

Syntax of JSP Directive

1.    <%@ directive attribute="value" %>  

JSP page directive

The page directive defines attributes that apply to an entire JSP page.

Syntax of JSP page directive

1.    <%@ page attribute="value" %>  

Attributes of JSP page directive

·         import
·         contentType
·         extends
·         info
·         buffer
·         language
·         isELIgnored
·         isThreadSafe
·         autoFlush
·         session
·         pageEncoding
·         errorPage
·         isErrorPage

1)import

The import attribute is used to import class,interface or all the members of a package.It is similar to import keyword in java class or interface.

Example of import attribute

1.    <html>  
2.    <body>  
3.      
4.    <%@ page import="java.util.Date" %>  
5.    Today is: <%= new Date() %>  
6.      
7.    </body>  
8.    </html>  

2)contentType

The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is "text/html;charset=ISO-8859-1".

Example of contentType attribute

1.    <html>  
2.    <body>  
3.      
4.    <%@ page contentType=application/msword %>  
5.    Today is: <%= new java.util.Date() %>  
6.      
7.    </body>  
8.    </html>  

3)extends

The extends attribute defines the parent class that will be inherited by the generated servlet.It is rarely used.

4)info

This attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.

Example of info attribute

1.    <html>  
2.    <body>  
3.      
4.    <%@ page info="composed by Sonoo Jaiswal" %>  
5.    Today is: <%= new java.util.Date() %>  
6.      
7.    </body>  
8.    </html>  
The web container will create a method getServletInfo() in the resulting servlet.For example:
1.    public String getServletInfo() {  
2.      return "composed by Sonoo Jaiswal";   
3.    }  

5)buffer

The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page.The default size of the buffer is 8Kb.

Example of buffer attribute

1.    <html>  
2.    <body>  
3.      
4.    <%@ page buffer="16kb" %>  
5.    Today is: <%= new java.util.Date() %>  
6.      
7.    </body>  
8.    </html>  

6)language

The language attribute specifies the scripting language used in the JSP page. The default value is "java".

7)isELIgnored

We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its value is false i.e. Expression Language is enabled by default. We see Expression Language later.
1.    <%@ page isELIgnored="true" %>//Now EL will be ignored  

8)isThreadSafe

Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP page, you can use isThreadSafe attribute of page directive.The value of isThreadSafe value is true.If you make it false, the web container will serialize the multiple requests, i.e. it will wait until the JSP finishes responding to a request before passing another request to it.If you make the value of isThreadSafe attribute like:
<%@ page isThreadSafe="false" %>
The web container in such a case, will generate the servlet as:
1.    public class SimplePage_jsp extends HttpJspBase   
2.      implements SingleThreadModel{  
3.    .......  
4.    }  

9)errorPage

The errorPage attribute is used to define the error page, if exception occurs in the current page, it will be redirected to the error page.

Example of errorPage attribute

1.    //index.jsp  
2.    <html>  
3.    <body>  
4.      
5.    <%@ page errorPage="myerrorpage.jsp" %>  
6.      
7.     <%= 100/0 %>  
8.      
9.    </body>  
10. </html>  

10)isErrorPage

The isErrorPage attribute is used to declare that the current page is the error page.

Note: The exception object can only be used in the error page.

Example of isErrorPage attribute

1.    //myerrorpage.jsp  
2.    <html>  
3.    <body>  
4.      
5.    <%@ page isErrorPage="true" %>  
6.      
7.     Sorry an exception occured!<br>  
8.    The exception is: <%= exception %>  
9.      
10. </body>  
11. </html>  

Include directive

The include directive is used to include the contents of any resource it may be jsp file, html file or text file. The include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource).

Advantage of Include directive

Code Reusability

Syntax of include directive

1.    <%@ include file="resourceName" %>  

Example of include directive

In this example, we are including the content of the header.html file. To run this example you must create an header.html file.
1.    <html>  
2.    <body>  
3.      
4.    <%@ include file="header.html" %>  
5.      
6.    Today is: <%= java.util.Calendar.getInstance().getTime() %>  
7.      
8.    </body>  
9.    </html>  


JSP Taglib directive

The JSP taglib directive is used to define a tag library that defines many tags. We use the TLD (Tag Library Descriptor) file to define the tags. In the custom tag section we will use this tag so it will be better to learn it in custom tag.

Syntax JSP Taglib directive

1.    <%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>  

Example of JSP Taglib directive

In this example, we are using our tag named currentDate. To use this tag we must specify the taglib directive so the container may get information about the tag.
1.    <html>  
2.    <body>  
3.      
4.    <%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>  
5.      
6.    <mytag:currentdate>  
7.      
8.    </body>  
9.    </html>  
10.   
11. </mytag:currentdate>  

Hidden Fields:
Hidden field is part of HTML, but hidden field can be used with JSP. Hidden field can carry hidden data from one page to another action page. Value of hidden field can be get from request.getParameter method. Hidden field can be set from java data types e.g. String, int, float.
Page1.jsp
 
<%@ page  language="java" %>
<%
 String hiddenFieldValue="MyHiddenValue";
%>
<html>
<head>
<title>Hidden fields in JSP</title>
</head>
<body>
<form name="frm" action="page2.jsp">
<input type="hidden" name="hdField" value="<%=hiddenFieldValue%>" />
<input type="text" name="txtField" />
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
page2.jsp
<%@ page language="java" %>
<%
 String getHiddenValue=request.getParameter("hdField");
 System.out.println("Hidden field Value :"+getHiddenValue);
%>



Displaying applet in JSP (jsp:plugin action tag)

  1. Displaying applet in JSP
  2. Syntax of jsp:plugin action tag
  3. Example of displaying applet in JSP
The jsp:plugin action tag is used to embed applet in the jsp file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.

Syntax of jsp:plugin action tag

 
<jsp:plugin type= "applet | bean" code= "nameOfClassFile" 
codebase= "directoryNameOfClassFile"
</jsp:plugin>
 

Example of displaying applet in JSP

In this example, we are simply displaying applet in jsp using the jsp:plugin tag. You must have MouseDrag.class file (an applet class file) in the current folder where jsp file resides. You may simply download this program that contains index.jsp, MouseDrag.java and MouseDrag.class files to run this application.

index.jsp

 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Mouse Drag</title>
    </head>
    <body bgcolor="khaki">
<h1>Mouse Drag Example</h1>
 <jsp:plugin align="middle" height="500" width="500"
     type="applet"  code="MouseDrag.class" name="clock" codebase="."/>
    </body>
</html>
 

Retrieving a Query String !

An include action executes the included JSP page and appends the generated output onto its own output stream. Request parameters parsed from the URL's query string are available not only to the main JSP page but to all included JSP pages as well. It is possible to temporarily override a request parameter or to temporarily introduce a new request parameter when calling a JSP page. This is done by using the jsp:param action.
In this example, param1 is specified in the query string and is automatically made available to the callee JSP page. param2 is also specified in the query string but is overridden by the caller. Notice that param2 reverts to its original value after the call. param3 is a new request parameter created by the caller. Notice that param3 is only available to the callee and when the callee returns, param3 no longer exists. Here is the caller JSP page:








If the example is called with the URL:
http://hostname.com?param1=a¶m2=b
the output would be:








Troubleshooting !

Troubleshooting is a form of problem solving most often applied to repair of failed products or processes. It is a logical, systematic search for the source of a problem so that it can be solved, and so the product or process can be made operational again. Troubleshooting is needed to develop and maintain complex systems where the symptoms of a problem can have many possible causes. Troubleshooting is used in many fields such as engineering, system administration, electronics, automotive repair, and diagnostic medicine. Troubleshooting requires identification of the malfunction(s) or symptoms within a system. Then, experience is commonly used to generate possible causes of the symptoms. Determining which cause is most likely is often a process of elimination - eliminating potential causes of a problem. Finally, troubleshooting requires confirmation that the solution restores the product or process to its working state.
In general, troubleshooting is the identification of, or diagnosis of "trouble" in a [system] caused by a failure of some kind. The problem is initially described as symptoms of malfunction, and troubleshooting is the process of determining the causes of these symptoms.
A system can be described in terms of its expected, desired or intended behavior (usually, for artificial systems, its purpose). Events or inputs to the system are expected to generate specific results or outputs. (For example selecting the "print" option from various computer applications is intended to result in a hardcopy emerging from some specific device). Any unexpected or undesirable behavior is a symptom. Troubleshooting is the process of isolating the specific cause or causes of the symptom. Frequently the symptom is a failure of the product or process to produce any results. (Nothing was printed, for example).
Every web page resides inside a browser window which can be considered as an object.
A Document object represents the HTML document that is displayed in that window. The Document object has various properties that refer to other objects which allow access to and modification of document content.
The way that document content is accessed and modified is called the Document Object Model, or DOM. The Objects are organized in a hierarchy. This hierarchical structure applies to the organization of objects in a Web document.
  • Window object: Top of the hierarchy. It is the outmost element of the object hierarchy.
  • Document object: Each HTML document that gets loaded into a window becomes a document object. The document contains the content of the page.
  • Form object: Everything enclosed in the <form>...</form> tags sets the form object.
  • Form control elements: The form object contains all the elements defined for that object such as text fields, buttons, radio buttons, and checkboxes.

There are several DOMs in existence. The following sections explain each of these DOMs in detail and describe how you can use them to access and modify document content.
  • The Legacy DOM: This is the model which was introduced in early versions of JavaScript language. It is well supported by all browsers, but allows access only to certain key portions of documents, such as forms, form elements, and images.
  • The W3C DOM: This document object model allows access and modification of all document content and is standardized by the World Wide Web Consortium (W3C). This model is supported by almost all the modern browsers.
  • The IE4 DOM: This document object model was introduced in Version 4 of Microsoft's Internet Explorer browser. IE 5 and later versions include support for most basic W3C DOM features.
DOM compatibility
If you want to write a script that uses the W3C DOM when it is available, and otherwise uses the IE 4 DOM if it is available, you can use a capability-testing approach that first checks for the existence of a method or property to determine whether the browser has the capability you desire. For example:

if (document.getElementById) {

  // If the W3C method exists, use it

}

else if (document.all) {

  // If the all[] array exists, use it

}

else {

  // Otherwise use the legacy DOM

}

No comments:
Write comments