Wednesday, 31 August 2011

JSP and Servlet tutorial for beginners - #2 - What is JSP?

I hope you have the necessary software [IDE and Servers] installed in your machine, if not, please take a look at this post to know how to install IDE and other stuffs and also do install them.

What is JSP?
  • JSP stands for Java Server Pages and it was designed by Sun microsystem to compete with ASP and PHP in earlier 2000.
  • Java Server Pages (JSP) technology is the Java platform technology for delivering DYNAMIC content to web clients in a portable, secure and well-defined way.
  • JSP enables the developers to put java code into jsp, this makes the development process very simple and easy.
  • Since JSP runs on Java Server, we can run it on any platform.
  • JSP - Presentation layer which displays the actual output after the java statements in JSP are processed by the server container.
Intro to JSP tags:
  • As we know that JSP is a scripting language which has its own tags to include Java content into it. Let’s have a brief look at the JSP tags.
  • There are four types of JSP tags available, they are:
    • Directives:  In directive tag we can import packages, define error handling pages, handle session information of the JSP page.
    • Declarations: In declaration tag, we can provide the definition of  the functions and variables that are going to be used in the JSP.
    • Scriptlets: In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine. [JSP engine converts JSP into Servlet]
    • Expresssions: We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream[commonly browser].
Code snippet
Following are the sample code snippet:
>>Directives<<
<%@page contentType="text/html" import="java.util.Date" %>

>>Declarations<<
<%!String retString(){
return "Hello World";
}
%>
>>Scriptlets<<
<%out.print("Welcome to JSP");%>
>>Expressions<<
<%="Today's date: "+ new Date() %>

>>Comments<<
<%-- I am sample JSP comment--%>
<%-- JSP Engine will not process me!! :) --%>

Download

Download Project



as

Sunday, 21 August 2011

JSP and Servlet tutorial for beginners - #1 - IDE and Servers Installation

Before pitching into the JSP and Servlet tutorial, we will see how to install the software needed for Java web programming. You can skip JSP and Servlet tutorial, if you have following software(s) installed in your machine already.
  • Eclipse - Integrated Development and Environment
  • Apache Tomcat - Application server
  • MySQL server - Database server

Eclipse - Integrated Development Environment(IDE) Installation:
  • Step 1: Goto Eclipse website and download Indigo package as shown below:
  • Step 2: After the window 32 or 64 bit link is clicked, it will take you to download page where you need to select mirror server[download server] as shown below:
  • Step 3: Unzip the downloaded content and clcik on eclipse exe to launch the IDE:











  • Step 4: You could see the welcome page upon clicking the eclipse.exe as shown below. Select workbench out of it to launch Java or Web project editor.











  • Step 5: Eclipse has been successfully installed. You could create a sample java program as shown below:
import java.io.*;
class MyFirstClass{
public static void main(String args[]){
System.out.println("Helllo World");
}
}

Goto File>>New>>Other>>Java>>Java Project to create a new Java Project

Create Java class under src folder of the newly created project

Have a look at below image!














Apache tomcat server Installation:
  • Step 1: Goto Apache website and download Apache server as shown below:













Step 2: Unzip the downloaded content into a folder called apache-tomcat-[version].