Friday, June 20, 2014

Directive attributes



I think you got an simple idea about directives in my last post. In this post I said something about attributes. This post is for understanding about JSP directive attributes.




There are few other attributes too, but these are the most common attributes that can be used in a JSP page.

The import attribute



This is very  similar to importing packages to Java file. In JSP this can be done in one line or using multiple lines. Look at the following code.

<%@ page import="java.util.Scanner, java.sql.* "%>

or

<%@ page import="java.util.Scanner" %>
<%@ page import="java.sql.*" %>


But you need not to import java.lang.*, javax.servlet.*, javax.servlet.jsp.* and javax.servlet.http.* packages. By default these packages are imported by JSP engine.



The session attribute


This is used to indicate whether the JSP page associate with HTTP session. This is boolean attribute. default value is true.

<%@ page session="false" %>



The errorPage attribute

This is used to indicate the URL of error page for relevant JSP page. In Java we handle errors with try-catch block and throw/throws clauses. This is also can be done in JSP. But JSP allows you to do it in handy way with introducing errorPage attribute. Then you can handle any error occurred in you JSP within separate JSP page.

<%@ page errorPage="myErrorPage.jsp" %>


The isErrorPage attribute

This indicates the error handling page. This is a separate page that can be use to give any message or any operation if error is occurred. 

<%@ page isErrorpage="true"  %>

Look at the following code to see how to handle errorPage in JSP.

This is the main page.


<%@page errorPage="errors.jsp" %>

<html>
<head>
 <title>JSP error test</title>
</head>
<body>

<%! int x[] = {1,2,3}; %>

<h2>Result : <%= x[5] %> </h2>
</body>
</html>


This is the errors.jsp page

<%@ page isErrorPage="true" %>

<html>
<head>
 <title>JSP error page</title>
</head>
<body>

<h2> Error has occurred </h2>
</body>
</html>

This is the way to use errorPage and isErrorPage attributes in JSP.

The language attribute

This is used to specify the language which is using in the JSP page.

<% page language="java"  %>

The extends attribute

This specify the super class that the servlet must extend.

<%@ page extends="package.class"  %>

The buffer attribute

This is used to specify the minimum size required  by the output buffer. Default size is depend on JSP engine. It is better if you can have more than 8kb.

<%@page buffer="32kb" %>

The autoFlush attribute

This attribute is used to flush buffered output if it is full. Default value is true.

<%@page autoFlush="false" %>

The info attribute

This is used to provide some description about the JSP page.

<%@page info="This is a JSP page"  %>

The contentType attribute

This is specify the MIME( Multipurpose Internet Mail Extension ) type and the character encoding of the JSP page. 

<%@page contentType="text/html ; charset=ISO-8859-1" %>


The pageEncoding attribute

This is specify the character encoding of the JSP page. Default value is ISO-8859-1.

<%@page pageEncoding="ISO-8859-1"%>





JSP element types



JSP elements can be categorized into six categories. Like other languages, this is also well defined scripting language.



   


Directives

Directives provide general information about the JSP page that you create. This can be divide into three categories.

  • Page directive
  • Include directive
  • Taglib directive


Page directive


This is used to inform about the properties of the JSP page to JSP engine. Look at these following examples.

<%@ page language="java" %> 
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page errorPage="loginError.jsp" %>
<%@ page import="java.util.* " %>


Likewise you can use many attributes with page directive. We talk more about these attributes later.


Include directive


This is used to include another file into JSP page. This tells the JSP engine to include these files.

<%@ include file="copyright.html" %>


Taglib directive


Like XML, JSP allows you to create custom tags that can be used in your JSP pages. Taglib is used to connect with tag library with a prefix.

<%@taglib uri="WEB-INF/tlds/tagLib.tld" prefix="first" %> 


You will learn further more in later posts about directives. All three directive has more attributes. There are some rules of directives.

  • A directive should be start with <%@ and end with %>
  • All tag names, attributes and their values are case sensitive.
  • The value must be enclosed with single/double quotes.( ' ' / " " ) 
  • No space between equal sign(=) and the value.

Declarations 

This is used to declare and define variables and methods in JSP page.

  • Declaration statement should be start with <%! and closed with %>
  • It can contain many declarations within a single tag.

<%! 
        int x = 10; 
        String name = "easyjavase";
%>

  • Or you can use many declaration within many declaration tags.

<%! int x = 10;  %>
  <%! String name = "easyjavase";  %>


    Scriptlet

    Scriptlets are simple java codes that can be used in the JSP page. It can contain any Java code. Normally we use scriptlets to embed HTML code to JSP page. It starts with <% and closed with %>. Look at the following example.

    <%
    out.print("<html>");
    out.print("<body>");
    out.print("<title>" + "This is the title" + "</title>");
    
    out.print("Your IP address is " + request.getRemoteAddr());
    
    out.print("</body>");
    out.print("</html>");
    
    %>
    
    

    Expressions

    This is used to express Java expressions in JSP pages. You can use scriptlets for some codes without using JSP expressions. But it is easy to use expressions instead of using only scriptlets.

    • It starts with <%= and end with %>.
    • Expressions are not ended with semicolon.
    • You cannot define anything inside an expression.

    <%@page language="java"%>
    <html>
        <body>
            
            <%!int count = 0; %>
            
            Welcome !
            <br>
            You are the <%= count++ %> visitor;
            
        </body>
    </html>
    
    
    

    Actions



    Actions are commands sent to the JSP engine to perform certain task. There are six predefined JSP actions.


    • jsp:include
    • jsp:forward
    • jsp:useBean
    • jsp:setProperty
    • jsp:getProperty
    • jsp:plugin
    • Custom tags

    include / forward

    These two actions are use to enable other web compornants in JSP pages.

    useBean / setProperty / getProperty

    These things are used to use JavaBeans in JSP pages.

    plugin

    This is used to embed client side components into JSP pages such as Applets.

    custom tags

    You can create user defined actions called Custom tags. 


    Comments

    <%-- JSP comments    --%>
    <%  //Java comments   %>




      

    Thursday, June 19, 2014

    How to install and configure Apache Tomcat Server


    I know that all you have successfully installed Java into your machines. In this blog I am using Apache Tomcat server as default web server in JSP. Here you can see how to install Apache Tomcat server into your machine.

    How to Install Apache Tomcat server


    In these days latest version of Apache Tomcat is 8.0.8  You can download the latest version on following link. http://tomcat.apache.org/download-80.cgi

    Then you can download the file that you want to install in your machine. I am using Windows 8.1 64bit Operating system. Then I downloaded the following marked file.


    How to Configure Apache Tomcat server

    Set JAVA_HOME

    First of all you need to create JAVA_HOME environment variable. You can do it like we did when installing java into your machine. You need to provide JDK installed directory for the variable value. (C:\Program Files\Java\jdk1.8.0_05).

    Then you have to configure the server. You may have downloaded zip file now. You have to unzip and save it on any directory you like. (try to use simple path like D: or E:)

    Then you need to change some settings to work on it. Go to to the apache-tomcat-8.0.8 and config folder. Then you will be able to see four XML pages.

    Configure server.xml file

    This is used to set the TCP port numbers. Find following code and change the port number between 1024 to 65535 skipping assigned port numbers in your machine(Open cmd and type netstat -a to check your port numbers in windows). Default port number is 8080 for Apache tomcat server.




    Configure web.xml file

    change the code as follows. 


    Configure context.xml file

    You need to add attribute to context tag as follows.


    Configure tomcat-users.xml file

    This is used to setup username and password of the user of Apache server. Do it as follows. I use root as the username and 1234 as the password.


    Then you are totally configured to use Apache Tomcat Server. Then you need to start the server.

    How to start the server ?

    • Go to Command Prompt.
    • Go to the directory that you have saved Apache Tomcat Server.
    • Locate the bin folder.
    • I have saved my server in D directory. Look at my cmd. 

    • Now I am in bin folder.
    • Then type startup and enter to execute.
    • Then you will see some changes in cmd and another window like CMD called Tomcat.



    Now you successfully started Apache Tomcat server. You can go to your browser and type localhost:port_number to open home page of Apache Tomcat server.

    Ex: localhost:8080



    How to shutdown the server ?


    Type shutdown in cmd and enter.


    Where to save your projects ?

    In Apache Tomcat folder, you can see a folder called webapps. You can save your projects in this folder. You can create your own folders in this webapps folder.

    How to run your jsp files ?


    I have created folder called "JSP" in webapps folder. Within this folder I have saved JSP file called "test.jsp". This is the way that you can access to this jsp file. 

                                      http://localhost:8080/JSP/test.jsp

    Save following code as your first JSP program and run it to see your IP address.

    <html>
    <head>
    <title>JPS test</title>
    </head>
    <body>
    
    <%
    
     out.print("Your IP address " + request.getRemoteAddr());
    
    %>
    
    </body>
    </html>
    








    Introduction to JSP



    Java is the most popular programming language in Information Technology field. There are many Java related options ( COBRA, JDBC, JNDI, JMSTM, JAXPTM, JTATM ) that can be used to create distributed business solutions. JSP (Java Server Pages) is another valuable addition of Java that is used to create server pages. You can use all of these technologies to create powerful software solutions. 


    What is JSP ?



    JSP is use to create web pages with dynamic content. You know that pure HTML contains static content. If you  are using JSP, you can create highly dynamic web pages with the help of other very important Java technologies. 

    You can create dynamic web pages using other technologies instead of using JSP.

    • ASP (Active Server Pages)
    • PHP
    • ColdFusion

    What are the advantages of JSP ?

    • JSP pages always compile before go to the server.
    • Allows user to create custom elements.
    • It easily integrate with Java EE APIs.

      1. JDBC (Java Database Connectivity)
      2. JNDI (Java Naming and Directory Interface)
      3. JMS (Java Message Service)
      4. EJB (Enterprise Java Beans)
      5. JAXP (Java API for XML Processing)
      6. JTA ( Java Transaction API )
      7. JavaMail