Over a million developers have joined DZone.

OCEJWCD 6 Tutorial: Introduction to JavaServer Pages (JSP)

DZone 's Guide to

OCEJWCD 6 Tutorial: Introduction to JavaServer Pages (JSP)

· Java Zone ·
Free Resource
The 1Z0-899 Java EE 6 Web Component Developer Certified Expert Exam focus more on business tier components.  This tutorial is one of the certification exam topics which gives attention into JavaServer Pages (JSP). 
Disclaimer: The following contents are taken from free resources available on internet and the main aim of this tutorial is to study JSP based on OCEJWCD 6 exam objectives.

About JavaServer Pages(JSP)

JavaServer Pages(JSP) allows to create web components that has been either dynamic or static web components of Java EE technologies. JavaServer Pages(JSP) used to combine HTML and Java codes. JSP contains Java code that can directly embedded into a html page by giving some JSP-specific tag. Java Server Pages can develop dynamically generated web pages based on HTML or XML. JSP just like PHP, but it uses the Java language. JSP file is created as ".jsp" extension and all the HTML and Java codes are to put inside .jsp file. This .jsp can be run to create a dynamic web page. In this JSP page all the Java codes are to put inside “ <%” and “ %>” tags.
In runtime JavaServer Pages(JSP) run as a servlet, If a client request the JSP page, it that receives a request and return a Java object that is representing the response page. A JavaServer Pages(JSP) page contains two types of text, Static content has been build by following format such as HTML or XML. JSP elements used to create dynamic content.

Describe why Servlets are not the whole solution:

  • Development on the Servlet requires to know many high level concepts, so difficult to learn and use.
  • Developing on the complex business logic makes the application more difficult to understand. So it is very difficult for enhancement and bug fixing.
  • Important thing is, to run a servlet on the server it requires Java Run time environment.
  • The servlet technology require more steps to develop, servlet require too long time for development.
  • CGI script produces an entirely new process that takes a lot of time to execute, whereas a servlet creates only a new thread. 
  • Too much overhead in development cycle
  • Limited productivity.
  • Complicated syntax.

Describe essentials of JSPs

  • Coding JSPs very simple compare with coding on Java servlets.
  • Very simple to create and enables a clean separation of business logic from presentation.
  • No need to compile and register it in deployment descriptor web.xml. 
  • This greatly simplifies the creation and maintenance of web contents.
  • Reuse of components and tag libraries - The dynamic contents can be provided by re-usable components such as JavaBean, Enterprise JavaBean (EJB) and tag libraries.
  • Java's power and portability.
  • Easy to create a modern web development style.
  • Simple syntax.
  • Simplify development-packaging-deployment process.

Understand the fundamentals and reasons for MVC architecture

A single request will result in multiple substantially different looking results.
You have a large development team with different team members doing the Web development and the business logic.
You perform complicated data processing, but have a relatively fixed layout.

MVC Flow of Control

Implementing MVC with RequestDispatcher

  1. Define beans to represent result data. 
    • Ordinary Java classes with at least one getPath method
  2. Use a servlet to handle requests
    • Servlet reads request parameters, checks for missing and malformed data, calls business logic, etc.
  3. Obtain bean instances
    • The servlet invokes business logic (application-specific code) or data-access code to obtain the results.
  4. Store the bean in the request, session, or servlet context
    • The servlet calls setAttribute on the request, session, or servlet context objects to store a reference to the beans that represent the results of the request.
  5. Forward the request to a JSP page.
    • The servlet determines which JSP page is appropriate to the situation and uses the forward method of RequestDispatcher to transfer control to that page.
  6. Extract the data from the beans. 
    • Either way, JSP page does not create or modify bean; it merely extracts and displays data that servlet created.  

Request Forwarding Example

1. RequestDispatcher dispatcher = request.getRequestDispatcher(address);
2. dispatcher.forward(request, response);

Where we Use MVC (Model 2) approach:

  • One submission will result in more than one basic look.
  • Several pages have substantial common processing.
  • Your application is moderately complex

OCEJWCD 6 Resources

EPractize Labs OCEJWCD 6 Training Lab 


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}