Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

JSP Basics

By Exforsys | on May 7, 2006 |
J2EE

JSP Basics

Java Server Pages (JSP) is a Java API in J2EE which allows dynamic creation of web pages using Java. It has the same purpose as other existing technologies like CGI or PHP. In this tutorial you will learn about Lifecycle of JSP pages, Dynamic content generation, Invoking Java code using JSP scripting elements, JavaBeans for JSP and Error Handling.



The main difference between servlets and JSPs is that servlets are Java classes and JSPs are not (they are embedded in HTML pages). This difference also specifies where you want to use servlets and where you want to use JSPs: If you generate all of your html pages dynamically and will have complex logic, you will probably use servlets. If dynamic content is very low compared to the static content in your web page, then you will probably use JSPs.

Lifecycle of JSP pages

The lifecycle of a JSP is shown in Figure-1. The most important fact about this figure is that all JSPs are managed by JSP containers and compiled into servlets before they are loaded. Similar to servlets, the same instance is used for all requests since JSP is compiled into a servlet..

Figure-1 Life cycle of a JSP

Dynamic content generation

In this section, we will create our first JSP which will write “Hello World” to the web page if no user-name parameter is given. If user-name parameter is given (e.g. Smith), then it will print “Hello < user-name >”. (e.g. Hello Smith).

The source code shown in this section can be found in HelloWorldJSP project.

The source code of HelloWorld.jsp is shown in Figure-2.

Figure-2 The source code of HelloWorld.jsp

Figure-3 shows the HelloWorld.jsp when it is run without a user-name. The result of running the same JSP with a user-name of John is shown in Figure-4. As you will notice user-name is given to the JSP as a name parameter added to the URL of the JSP.

Figure-3 HelloWorld.jsp without a user-name parameter

Figure-4 HelloWorld.jsp with a user-name parameter

Invoking Java code using JSP scripting elements

In this section we will create an example which shows how to define Java methods, how to call methods and how to call methods in standard Java libraries using JSPs. Specifically, we will define a factorial method which can return factorial of a number, call factorial method and get a random number from java.lang.Math class in “JavaIntegration.jsp“ file we created.

The source code shown in this section can be found in JavaIntegrationJSP project.

Figure-5 shows the contents of “JavaIntegration.jsp” file. The code is very easy to understand since all we do is to add Java code inside jsp tags and it has comments where they are needed. Figure-6 shows the output of the “JavaIntegration.jsp”.

Figure-5 Contents of JavaIntegration.jsp file

Figure-6 Output of JavaIntegration.jsp file

JavaBeans for JSP

We will modify “HelloWorld.jsp” developed in the “Dynamic Content Generation” section so that it will use a java beans at the background. Figure-5 shows HelloWorldJavaBean class which will be used by the HelloWorld.jsp.

The source code shown in this section can be found in HelloWorldBeanJSP project.

Figure-5 HelloWorldBean class

Figure-6 shows the contents of HelloWorld.jsp file which uses HelloWorldBean by the help of useBean, setProperty and getProperty tags.

Figure-6 HelloWorld.jsp file

Figure-7 shows the HelloWorld.jsp when it is run without a user-name. The result of running the same JSP with a user-name of John is shown in Figure-8. As you will notice user-name is given to the JSP as a name parameter added to the URL of the JSP as before.

Figure-7 HelloWorld.jsp without a user-name parameter

Figure-8 HelloWorld.jsp with a user-name parameter

Error Handling

Although there can be other error handling methods depending your development tools and jsp container, we will show an example for “error.jsp” page strategy in this section.

“error.jsp” page is a java server page where the errors are redirected when any found. So by setting a special parameter for error redirection in our jsp pages we can handle all errors in one jsp page.

The source code shown in this section can be found in ErrorPageJSP project.

Figure-9 shows the contents of the error.jsp file which will be called by index.jsp when an exception occurs. Setting isErrorPage directive to true, we explicitly state that this jsp page is for handling errors to the JSP container. This allows error.jsp to have an “exception” object automatically which can be used for error reporting.

Figure-9 Contents of error.jsp page

Figure-10 shows contents of index.jsp page. Since we tried to convert a non-integer string into an integer, we will get a NumberFormatException and the control is redirected to error.jsp page internally. The errorPage directive is set to the error.jsp since we want to be directed to error.jsp page when some error occurs.

Figure-10 Contents of index.jsp page

The output of index.jsp (which is in fact redirected to error.jsp internally) is shown in Figure-11.

Figure-11 Output of index.jsp page

Links

• Servlet Technology Homepage – http://java.sun.com/products/servlet/

« « ABC of IT Certification
XML Introduction » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • Important Features of Java – Multithreading, AWT

    July 26, 2006 - 0 Comment
  • Performance Tuning of Java Applications

    December 2, 2005 - 0 Comment
  • How To Perform Class Loading With Java

    July 8, 2006 - 0 Comment
  • J2EE Overview

    December 6, 2005 - 0 Comment
  • A Java TOC2 Class Which Can Contact Aim

    July 12, 2006 - 0 Comment
  • Servlet Basics

    December 6, 2005 - 0 Comment
  • Quickly Develop Java Programs With Tapestry

    July 12, 2006 - 0 Comment
  • How to Install and Use NetBeans for Java Development

    June 26, 2005 - 0 Comment
  • Servlets Advanced

    December 12, 2005 - 0 Comment
  • How To Run J2ME Programs on Palm Devices

    July 14, 2006 - 0 Comment
  • Why It is Important To Focus On Java Exceptions For Your Programs

    July 27, 2006 - 0 Comment
  • Important Features of Java – Multithreading, AWT

    July 26, 2006 - 0 Comment
  • What You Should Know About Java XML

    July 25, 2006 - 0 Comment
  • What You Can Do To Deal With Java’s Memory Retention Problems

    July 22, 2006 - 0 Comment
  • Java Overview

    July 17, 2006 - 0 Comment
  • How To Use Java DB as Your Client Mobile Database

    July 14, 2006 - 0 Comment
  • How To Run J2ME Programs on Palm Devices

    July 14, 2006 - 0 Comment
  • Java Virtual Machine

    July 13, 2006 - 0 Comment
  • Quickly Develop Java Programs With Tapestry

    July 12, 2006 - 0 Comment
  • A Java TOC2 Class Which Can Contact Aim

    July 12, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Why It is Important To Focus On Java Exceptions For Your Programs
  • Important Features of Java – Multithreading, AWT
  • What You Should Know About Java XML
  • What You Can Do To Deal With Java’s Memory Retention Problems
  • Java Overview

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT