Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This will help in learning web development through servlets and JSP

Authors

Getting Started with Web applications development with servlets and JSP

Getting Started with Web applications development with servlets and JSP - Part 7: First JSP Page

May 31 2012 12:00AM by Vladimir Djurovic   

Starting with this section, we will dive into the world of JSP (Java Server Pages). JSP specification is created to address one of the biggest shortcomings of servlets: the need to generate HTML code inside servlet. As we have seen in previous sections, this can be really time consuming and error prone task.

JSPs represent scripting framework that is similar to XML and HTML, and allows easy embedding of JSP code inside plain HTML pages. The latest JSP specification is version 2.0, which introduced big changes into JSP world:

  • Expression Language (EL) – can be used to easily access data from JSP page and perform simple operations
  • XML-like syntax
  • New syntax for defining custom actions and tags

In this section, we will create simple JSP page and deploy it to our sample application. This page will outline basic JSP concepts. We will focus on JSP 2.0 syntax, since this is the emerging standard.

Before we go to page creation, we need to make a little change to web.xml. Since we will be using new specification, change the start tag of your web.xml to the following:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

This will enable JSP 2.0 expression language processing.

Inside your application WEB-INF folder, create file names simplepage.jspx. File contents are shown in a snippet below:

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">

    <jsp:directive.page contentType="text/html" pageEncoding="US-ASCII"/>
    <jsp:element name="text">
        <jsp:attribute name="lang">EN</jsp:attribute>
        <jsp:body>Hello World!</jsp:body>
    </jsp:element>
</jsp:root>

The meaning of each tag is as follows:

  • jsp:root – defines root of JSP page. Provides standard JSP elements definitions and namespaces of tag libraries
  • jsp:directive – defines page-wide directives, such as encoding, MIME type etc. This tag contains predefined set of attributes which can be used to define all aspects of JSP page
  • jsp:element – generates XML element dynamically. This tag can be used to generate any type of HTML contents in web pages. In this case, it will generate simple tag with attribute “EN” and value “Hello, world!”

If you now start Tomcat and direct your browser to http://localhost:8080/samplewebapp/simplepage.jspx, you should get the following output in your browser:

use of jsp code

Obviously, this is not too pretty site, so we will add some better look and more dynamic behaviour. Instead of “Hello, world”, we will say hello to user.

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">

    <jsp:directive.page contentType="text/html" pageEncoding="US-ASCII"/>

    <jsp:element name="div">
        <jsp:attribute name="id">mydiv</jsp:attribute>
        <jsp:attribute name="style">color: red; background-color: yellow</jsp:attribute>
        <jsp:body>
            Hello, ${param["user"]}!
        </jsp:body>
    </jsp:element>

</jsp:root>

If you direct your browser to http://localhost:8080/samplewebapp/simplepage.jspx?user=joe, you should get the following output:

jsp code with request parameter

As you can see, greeting is for the user named Joe. This is pulled from our request parameter (user=joe), using Expression Language (more on this in following chapters). If you look at the source of the generated page, you will notice it looks like this:

<div style="color: red; background-color: yellow" id="mydiv">
            Hello, joe!
        </div>

As you can see, it directly corresponds to jsp:element, jsp:attribute and jsp:body tags. The greeting is generated on the fly.

It is good to know that at runtime, JSPs are actually compiled into servlets by JSP engine. The first time a JSP is accessed it is compiled, and on subsequent requests, a servlet is actually run. This significantly improves performance.

This example demonstrated basic principles for developing JSP pages. More information on JSPs and expression language can be found on following links:

Find sample web application here.


Vladimir Djurovic
78 · 2% · 717
5



Submit

1  Comments  

  • Hi, this is a nice series.

    Some typo - the navigation links 'Previous', 'Next' not working...

    thanks!

    commented on May 21 2012 10:49PM
    suchi banerjee
    2701 · 0% · 3

Your Comment


Sign Up or Login to post a comment.

"Getting Started with Web applications development with servlets and JSP - Part 7: First JSP Page" rated 5 out of 5 by 5 readers
Getting Started with Web applications development with servlets and JSP - Part 7: First JSP Page , 5.0 out of 5 based on 5 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]