150
Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Web Technology Unit III- Server Side Technologies

By Prof. B.A.Khivsara

Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

Page 2: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to Server Side technology and TOMCAT,

Servlet: Introduction to Servlet, need and advantages, Servlet Lifecycle, Creating and testing of sample Servlet, session management.

JSP: Introduction to JSP, advantages of JSP over Servlet , elements of JSP page: directives, comments, scripting elements, actions and templates, JDBC Connectivity with JSP.

Page 3: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Servlets & Tomcat

Page 4: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 5: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

What are Servlets?

• Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

• Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Page 6: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Advantages of Servlet over CGI

• Performance is significantly better.

• Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

• Servlets are platform-independent because they are written in Java.

• servlets are trusted.

• The full functionality of the Java class libraries is available to a servlet.

Page 7: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Servlets Architecture

Page 8: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Servlets Packages

• Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification.

• Servlets can be created using the packages

• javax.servlet

• javax.servlet.http

Page 9: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Page 10: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Servlet Life Cycle

• The servlet is initialized by calling the init() method.

• The servlet calls service() method to process a client's request.

• The servlet is terminated by calling the destroy() method.

• Finally, servlet is garbage collected by the garbage collector of the JVM.

Page 11: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The init() Method

• The init method is called only once. It is called only when the servlet is created, and not called for any user requests afterwards.

• So, it is used for one-time initializations.

• When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate.

• The init() method simply creates or loads some data that will be used throughout the life of the servlet.

• The init method definition looks like this −

public void init() throws ServletException {

// Initialization code...

}

Page 12: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The service() Method

• The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

• When server receives a request for a servlet, the service() method checks the HTTP request type (GET, POST) and calls doGet, doPost, methods as appropriate.

• Here is the signature of this method −

• public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { }

Page 13: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The doGet() Method

• A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// Servlet code

}

Page 14: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The doPost() Method

• A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// Servlet code

}

Page 15: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The destroy() Method

• The destroy() method is called only once at the end of the life cycle of a servlet.

• This method gives your servlet a chance to close database connections, halt background threads, and perform other such cleanup activities.

• After the destroy() method is called, the servlet object is marked for garbage collection.

• The destroy method definition looks like this −

public void destroy() {

// Finalization code...

}

Page 16: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 17: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Architecture Diagram • First the HTTP requests coming

to the server are delegated to the servlet container.

• The servlet container loads the servlet before invoking the service() method.

• Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.

Page 18: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 19: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Requirements

Before running Servlet your machine requires following tools

• 1> Eclipse (IDE-integrated development environment)

• 2> Tomcat (Web Server)

Page 20: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 21: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Apache-Tomcat

• Apache Tomcat, often referred to as Tomcat Server, is an open-source Java Servlet Container developed by the Apache Software Foundation (ASF).

• Tomcat implements several Java EE specifications including Java Servlet, JavaServer Pages (JSP), Java EL, and WebSocket, and provides a "pure Java" HTTP web server environment in which Java code can run.

Page 22: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Tomcat- Components

Catalina

Coyote

Jasper

Cluster

High availability

Web application

Page 23: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Tomcat- Components

Catalina

• Catalina is Tomcat's servlet container.

• Catalina implements Sun Microsystems's specifications for servlet and JavaServer Pages (JSP).

• In Tomcat, a Realm element represents a "database" of usernames, passwords, and roles assigned to those users.

• Catalina to be integrated into environments where such authentication information is already being created and maintained, and then use that information to implement Container Managed Security as described in the Servlet Specification.

Page 24: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Tomcat- Components

Coyote

• Coyote is a Connector component for Tomcat that supports the HTTP 1.1 protocol as a web server.

• This allows Catalina, nominally a Java Servlet or JSP container, to also act as a plain web server that serves local files as HTTP documents.

• Coyote listens for incoming connections to the server on a specific TCP port and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client.

Page 25: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Tomcat- Components

Jasper

• Jasper is Tomcat's JSP Engine.

• Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.

• From Jasper to Jasper 2, important features were added:

• JSP Tag library pooling - Each tag markup in JSP file is handled by a tag handler class.

• Background JSP compilation - While recompiling modified JSP Java code, the older version is still available for server requests.

• Recompile JSP when included page changes - Pages can be inserted and included into a JSP at runtime

• JDT Java compiler - Jasper 2 can use the Eclipse JDT Java compiler

Page 26: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Tomcat- Components

• These components are added from Tomcat 7.0 version

• Cluster

• This component has been added to manage large applications.

• High availability

• A high-availability feature has been added to facilitate the scheduling of system upgrades (e.g. new releases, change requests) without affecting the live environment.

• Web application

• It has also added user- as well as system-based web applications enhancement to add support for deployment across the variety of environments.

Page 27: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 28: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ? (One time Requirement)

• If you are using Eclipse IDE first time, you need to configure

the tomcat server First.

• For configuring the tomcat server in eclipse IDE,

• click on servers tab at the bottom side of the IDE -> right click

on blank area -> New -> Servers -> choose tomcat then its

version -> next -> click on Browse button -> select the apache

tomcat root folder previous to bin -> next -> addAll -> Finish.

Page 29: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 30: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 31: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 32: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 33: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 34: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 35: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 36: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

How to configure tomcat server in Eclipse ?

Page 37: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 38: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Steps to run servlet in Eclipse

Create a Dynamic web project

create a servlet

add servlet-api.jar file

Run the servlet

Page 39: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Steps to run servlet in Eclipse

Create a Dynamic web project

create a servlet

add servlet-api.jar file

Run the servlet

Page 40: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create the dynamic web project

Start Eclipse and In Menu : File -> New -> Dynamic Web Project

Page 41: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create the dynamic web project

Page 42: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create the dynamic web project

Page 43: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create the dynamic web project

Page 44: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create the dynamic web project

Structure of Dynamic Web Project

Page 45: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Steps to run servlet in Eclipse

Create a Dynamic web project

create a servlet

add servlet-api.jar file

Run the servlet

Page 46: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create a servlet Right click on Src -> New -> Sevlet

Page 47: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create a servlet Give name of class

Page 48: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create a servlet

Page 49: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create a servlet Either select doGet or doPost

Page 50: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Create a servlet

Servlet is created, you can write the code now.

Page 51: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Steps to run servlet in Eclipse

Create a Dynamic web project

create a servlet

add servlet-api.jar file

Run the servlet

Page 52: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Add servlet-api.jar file

Page 53: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Add servlet-api.jar file

Page 54: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Add servlet-api.jar file In Tomcat folder goto Lib folder and select servlet-api.jar file

Page 55: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Add servlet-api.jar file

Page 56: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Steps to run servlet in Eclipse

Create a Dynamic web project

create a servlet

add servlet-api.jar file

Run the servlet

Page 57: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Run the servlet Right click on project name -> click Run As -> Run on Server

Page 58: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Run the servlet

Page 59: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Run the servlet

Page 60: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 61: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 1- To Print Hello World directly

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void init() throws ServletException { }

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h1> Hello World </h1>");

}

public void destroy() { }

}

Page 62: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 2- To Print Hello World using init method

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException

{ message = "Hello World"; }

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h1>" + message + "</h1>"); }

public void destroy() { }

}

Page 63: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Reading Form Data using Servlet

• getParameter() − You call request.getParameter() method to get the value of a form parameter.

• getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox.

• getParameterNames() − Call this method if you want a complete list of all parameters in the current request.

Page 64: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 3- To read data from HTML file and print that .

• It requires 2 files:

• 1. HTML (s1.html)File

• 2. Servlet (s2.java) File

• First Run HTML file and after clicking on submit button it will run servel(.java) file.

Page 65: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 3- To read data from HTML file and print that .

• S1.html (html code)

<html>

<form method=“post” action=“s2”>

Enter Your Name

<input type=text name=“t1”>

<br>

<input type=“submit” value=“submit”>

</form>

</body>

</html>

Page 66: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 3- To read data from HTML file and print that .

• S2.java (Servlet Code)

import java.io.*;

import javax.servlet.*;

public class s2 extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

String a= request.getParameter("t1");

PrintWriter out= response.getWriter();

out.print("<br>Your Name is: "+a);

}

}

Page 67: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 4 – (HTML code)

To read data from HTML checkbox values print that

• <!DOCTYPE html>

• <html>

• <body>

• <form method="post" action=s1>

• Hobbies

• <input type="checkbox" name="t1" value=“Cricket"> Cricket

• <input type="checkbox" name="t1" value="Music">Music

• <input type="checkbox" name="t1" value=“Dance">Dance

• <input type="submit" value="submit">

• </form>

• </body>

• </html>

Name of Servlet File

Page 68: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 4 – (servlet code)

To read data from HTML checkbox values print that

• protected void doPost(HttpServletRequest request, HttpServletResponse response)

• throws ServletException, IOException {

• response.setContentType("text/html");

• PrintWriter out=response.getWriter();

• String[] values=request.getParameterValues("t1");

• for(int i=0;i<values.length;i++)

• {

• out.println("<li>"+values[i]+"</li>");

• }

• }

• }

Page 69: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 5 – (HTML code)

To print complete list of all parameters

• <!DOCTYPE html>

• <html>

• <body>

• <form method="post" action=s1>

• Hobbies

• Enter Name <input type=“text" name="t1" > <br>

• Enter Address <input type=“text" name="t2" > <br>

• Enter Class <input type=“text" name="t3" > <br>

• <input type="submit" value="submit">

• </form>

• </body>

• </html>

Page 70: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Example 5 – (Servlet code)

To print complete list of all parameters

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Enumeration e = request.getParameterNames();

while(e.hasMoreElements())

{

Object obj = e.nextElement();

out.println((String) obj+ "<br>");

}

}

Page 71: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Out Line

Servlet Introduction

Servlet Lifecycle

Servlet Architecture

Software Requirement to run Servlet

Tomcat Introduction

Tomcat Configuration in Eclipse

Steps to run servlets in Eclipse

Sample code

Session Management

Page 72: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking (Management)

• Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.

• Http protocol is a stateless so we need to maintain state using session tracking techniques.

• Each time user requests to the server, server treats the request as the new request.

• So we need to maintain the state of an user to recognize to particular user.

• Why use Session Tracking?

• To recognize the user It is used to recognize the particular user.

Page 73: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking Techniques

Cookies

Hidden Form Field

URL Rewriting

HttpSession

Page 74: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

• A cookie is a small piece of information that is persisted between the multiple client requests.

• A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

• How Cookie works

• In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user.

Page 75: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

• Types of Cookie

• Non-persistent cookie

• Persistent cookie

• Non-persistent cookie

• It is valid for single session only. It is removed each time when user closes the browser.

• Persistent cookie

• It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout or signout.

Page 76: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

• Advantage of Cookies

• Simplest technique of maintaining the state.

• Cookies are maintained at client side.

• Disadvantage of Cookies

• It will not work if cookie is disabled from the browser.

• Only textual information can be set in Cookie object.

Page 77: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

Method Description

public void setMaxAge(int expiry) Sets the maximum age of the cookie in seconds.

public String getName() Returns the name of the cookie. The name cannot be changed after creation.

public String getValue() Returns the value of the cookie.

public void setName(String name) changes the name of the cookie.

public void setValue(String value) changes the value of the cookie.

Useful Methods of Cookie class

Page 78: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

• How to create Cookie? • Cookie ck=new Cookie("user","sonu");//creating cookie object • response.addCookie(ck);//adding cookie in the response

• How to delete Cookie?

• Cookie ck=new Cookie("user","");//deleting value of cookie • ck.setMaxAge(0);//changing the maximum age to 0 seconds • response.addCookie(ck);//adding cookie in the response

• How to get Cookies? • Cookie ck[]=request.getCookies(); • for(int i=0;i<ck.length;i++){ • out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing na

me and value of cookie • }

Page 79: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

Simple example of Servlet Cookies

Page 80: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

Simple example of Servlet Cookies

• index.html

<form action="servlet1" method="post">

Name:<input type="text" name="userName"/><br/>

<input type="submit" value="go"/>

</form>

Page 81: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

Simple example of Servlet Cookies

Servlet1.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("userName"); out.print("Welcome "+n); Cookie ck=new Cookie("uname",n);//creating cookie object response.addCookie(ck);//adding cookie in the response //creating submit button out.print("<form action='servlet2'>"); out.print("<input type='submit' value='go'>"); out.print("</form>"); out.close(); }catch(Exception e){System.out.println(e);} } }

Page 82: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Using Cookies

Simple example of Servlet Cookies Servlet2.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SecondServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Cookie ck[]=request.getCookies(); out.print("Hello "+ck[0].getValue()); out.close(); }catch(Exception e){System.out.println(e);} } }

Page 83: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking Techniques

Cookies

Hidden Form Field

URL Rewriting

HttpSession

Page 84: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

• In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of an user.

• In such case, we store the information in the hidden field and get it from another servlet.

• Synax

• <input type="hidden" name="uname" value=“Bhavana">

• Here, uname is the hidden field name and Bhavana is the hidden field value.

Page 85: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

• Advantage of Hidden Form Field

• It will always work whether cookie is disabled or not.

• Disadvantage of Hidden Form Field:

• It is maintained at server side.

• Extra form submission is required on each pages.

• Only textual information can be used.

Page 86: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

Example of passing username

Page 87: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

Example of passing username

• index.html

<form method="post" action=“First">

Name:<input type="text" name="user" /><br/> Password:<input type="text" name="pass" ><br/>

<input type="submit" value="submit">

</form>

Page 88: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

Example of passing username

• First.java import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class First extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String user = request.getParameter("user");

//creating a new hidden form field

out.println("<form action='Second'>");

out.println("<input type='hidden' name='user' value='"+user+"'>");

out.println("<input type='submit' value='submit' >");

out.println("</form>");

}

}

Page 89: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- Hidden Form Fields

Example of passing username

• Second.java

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Second extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //getting parameter from the hidden field String user = request.getParameter("user"); out.println("Welcome "+user); } }

Page 90: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking Techniques

Cookies

Hidden Form Field

URL Rewriting

HttpSession

Page 91: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- URL Rewriting

• If the client has disabled cookies in the browser then session management using cookie wont work.

• In that case URL Rewriting can be used as a backup. URL rewriting will always work.

• In URL rewriting, a token(parameter) is added at the end of the URL.

• The token consist of name/value pair separated by an equal(=) sign.

• For Example:

Page 92: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- URL Rewriting

• When the User clicks on the URL having parameters, the request goes to the Web Container with extra bit of information at the end of URL.

• The Web Container will fetch the extra part of the requested URL and use it for session management.

• The getParameter() method is used to get the parameter value at the server side.

• Advantage of URL Rewriting • It will always work whether cookie is disabled or not (browser

independent).

• Extra form submission is not required on each pages.

• Disadvantage of URL Rewriting • It will work only with links.

• It can send only textual information.

Page 93: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- URL Rewriting

Example

• index.html

• <form method="post" action="validate">

• Name:<input type="text" name="user" /><br/>

• Password:<input type="text" name="pass" ><br/>

• <input type="submit" value="submit">

• </form>

Page 94: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- URL Rewriting

Example

• Validate.java import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

String name = request.getParameter("user");

String pass = request.getParameter("pass");

if(pass.equals("1234")) {

response.sendRedirect("First?user_name="+name+"");

}

}

}

Page 95: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- URL Rewriting

Example

• First.java

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class First extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String user = request.getParameter("user_name"); out.println("Welcome "+user); } }

Page 96: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking Techniques

Cookies

Hidden Form Field

URL Rewriting

HttpSession

Page 97: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- HttpSession

• The servlet container uses this interface to create a session between an HTTP client and an HTTP server.

• The session persists for a specified time period, across more than one connection or page request from the user.

• You would get HttpSession object by calling the public method getSession() of HttpServletRequest, as below −

HttpSession session = request.getSession();

Page 98: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- HttpSession

• How to get the HttpSession object ?

• public HttpSession getSession():Returns the current session associated with this request, or if the request does not have a session, creates one.

• Commonly used methods of HttpSession interface

• public String getId():Returns unique identifier value.

• public long getCreationTime():Returns the time when this session was created.

• public long getLastAccessedTime():Returns the last time the client sent a request associated with this session.

• public void invalidate():Invalidates this session then unbinds any objects bound to it.

Page 99: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- HttpSession

Example Hit Count

• HTML File

<h3>Hit Count Example with HttpSession</h3>

<form method="get" action=“HitCount">

Click for Hit Count

<input type="submit" value="GET HITS">

</form>

</body>

Page 100: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- HttpSession

Example Hit Count

public class HitCount extends HttpServlet

{ public void service(HttpServletRequest req, HttpServletResponse res) throws

ServletException, IOException {

res.setContentType("text/html") ;

PrintWriter out = res.getWriter( );

HttpSession session = req.getSession();

Integer hitNumber = (Integer) session.getAttribute("rama");

if (hitNumber == null) { hitNumber = new Integer(1); }

else { hitNumber = new Integer(hitNumber.intValue()+1) ; }

session.setAttribute("rama", hitNumber); // storing the value with session object

out.println("Your Session ID: " + session.getId()); // never changes in the whole session

out.println("<br>Session Creation Time: " + new Date(session.getCreationTime()));

out.println("<br>Time of Last Access: " + new Date(session.getLastAccessedTime()));

out.println("<br>Latest Hit Count: " + hitNumber); // increments by 1 for every hit

}

Page 101: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Session Tracking- HttpSession

Example Hit Count output

Page 102: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP

Page 103: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 104: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Introduction to JSP

• JSP technology is used to create web application just like Servlet technology.

• It can be thought of as an extension to servlet because it provides more functionality than servlet such as expression language, jstl etc.

• A JSP page consists of HTML tags and JSP tags.

• The jsp pages are easier to maintain than servlet because we can separate designing and development.

• It provides some additional features such as Expression Language, Custom Tag etc.

Page 105: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 106: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Advantages of JSP

• vs. Active Server Pages (ASP)

• The advantages of JSP are twofold.

• First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use.

• Second, it is portable to other operating systems and non-Microsoft Web servers.

• vs. JavaScript

• JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.

Page 107: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Advantage of JSP over Servlet

• 1) Extension to Servlet • JSP technology is the extension to servlet technology. We can use all

the features of servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.

• 2) Easy to maintain • JSP can be easily managed because we can easily separate our

business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic.

• 3) Fast Development: No need to recompile and redeploy • If JSP page is modified, we don't need to recompile and redeploy the

project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

• 4) Less code than Servlet • In JSP, we can use a lot of tags such as action tags, jstl, custom tags

etc. that reduces the code. Moreover, we can use EL, implicit objects etc.

Page 108: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 109: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Life cycle of a JSP Page

• The JSP pages follows these phases:

• Translation of JSP Page

• Compilation of JSP Page

• Classloading (class file is loaded by the classloader)

• Instantiation (Object of the Generated Servlet is created).

• Initialization ( jspInit() method is invoked by the container).

• Reqeust processing ( _jspService() method is invoked by the container).

• Destroy ( jspDestroy() method is invoked by the container).

Page 110: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Life cycle of a JSP Page In the end a JSP becomes a Servlet JSP pages are converted into Servlet by the Web Container. The Container translates a JSP page into servlet class source(.java) file and then compiles into a Java Servlet class.

Page 111: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Compilation

• When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page.

• If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.

• The compilation process involves three steps −

• Parsing the JSP.

• Turning the JSP into a servlet.

• Compiling the servlet.

Page 112: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Initialization

• When a container loads a JSP it invokes the jspInit() method before servicing any requests.

• If you need to perform JSP-specific initialization, override the jspInit() method −

• public void jspInit(){

• // Initialization code...

• }

• Typically, initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

Page 113: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Execution

• This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.

• Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.

• The _jspService() method as follows −

• void _jspService(HttpServletRequest request, HttpServletResponse response)

• { • // Service handling code... • }

• The _jspService() method of a JSP is invoked on request basis. • This is responsible for generating the response for that request and this

method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc.

Page 114: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Cleanup

• The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.

• The jspDestroy() method is the JSP equivalent of the destroy method for servlets.

• The jspDestroy() method has the following form −

• public void jspDestroy() {

• // Your cleanup code goes here.

• }

Page 115: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 116: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

• A JSP page looks similar to an HTML page, but a JSP page also has Java code in it.

• We can put any regular Java Code in a JSP file using a scriplet tag which

• start with <%

• and ends with %>.

• JSP pages are used to develop dynamic responses.

Page 117: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

Open Eclipse, Click on New → Dynamic Web Project

Page 118: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

Give a name to your project and click on OK

Page 119: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

You will see a new project created in Project Explorer

Page 120: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page To create a new JSP file right click on Web Content directory, New → JSP file

Page 121: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

Give a name to your JSP file and click Finish.

Page 122: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

Write something in your JSP file. The complete HTML and the JSP code,

goes inside the <body> tag, just like HTML pages.

Page 123: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

To run your project, right click on Project, select Run As → Run on Server

Page 124: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

To start the server, Choose existing server name and click on finish

Page 125: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Creating a JSP Page

See the Output in your browser.

Page 126: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 127: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Elements of JSP

Scripting Element Example

Comment <%-- comment --%>

Directive <%@ directive %>

Declaration <%! declarations %>

Scriptlet <% scriplets %>

Expression <%= expression %>

Page 128: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Comments

• JSP comment marks text or statements that the JSP container should ignore.

• Syntax of the JSP comments −

• <%-- This is JSP comment --%>

• Example shows the JSP Comments − <html>

<body>

<h2>A Test of Comments</h2>

<%-- This comment will not be visible in the page source --%>

</body>

</html>

Page 129: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Directives

• A JSP directive affects the overall structure of the servlet class. It usually has the following form −

• <%@ directive attribute="value" %>

• There are three types of directive tag −

Page 130: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

The Scriptlet • A scriptlet can contain any number of JAVA language statements,

variable or method declarations, or expressions that are valid in the page scripting language.

• syntax of Scriptlet − <% code fragment %>

• first example for JSP −

<html>

<head><title>Hello World</title></head>

<body>

Hello World!<br/>

<%

out.println("Your IP address is " + request.getRemoteAddr());

%>

</body>

</html>

Page 131: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Declarations

• A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.

• Syntax for JSP Declarations −

• <%! declaration; %>

• Example for JSP Declarations −

• <%! int i = 0; %>

• <%! int a, b, c; %>

• <%! Circle a = new Circle(2.0); %>

Page 132: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Expression

• A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.

• you cannot use a semicolon to end an expression.

• Syntax of JSP Expression −

• <%= expression %>

• Example shows a JSP Expression − <html>

<body>

<%= "Welcome "+request.getParameter("uname") %>

</body>

</html>

Page 133: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Actions

• JSP actions use to control the behavior of the servlet engine.

• You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.

• Syntax

<jsp:action_name attribute = "value" />

Page 134: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Actions

S.No. Syntax & Purpose

1 jsp:include - Includes a file at the time the page is requested.

2 jsp:useBean - Finds or instantiates a JavaBean.

3 jsp:setProperty - Sets the property of a JavaBean.

4 jsp:getProperty - Inserts the property of a JavaBean into the output.

5 jsp:forward - Forwards the requester to a new page.

6 jsp:plugin - Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.

7 jsp:element - Defines XML elements dynamically.

8 jsp:attribute - Defines dynamically-defined XML element's attribute.

9 jsp:body - Defines dynamically-defined XML element's body.

10 jsp:text - Used to write template text in JSP pages and documents.

Page 135: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Templates

• If a Website has multiple pages with identical formats, which is common, even simple layout changes require modifications to all of the pages.

• To minimize the impact of layout changes, we need a mechanism for including layout in addition to content; that way, both layout and content can vary without modifying files that use them.

• That mechanism is JSP templates. (Same like CSS in HTML)

Page 136: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Templates

• Templates are JSP files that include parameterized content. The templates have a set of custom tags:

• template:get,

• template:put,

• template:insert.

• Example

• <template:get name='header'/>

• <template:get name='content'/>

• <template:get name='footer'/>

• <template:insert template='/articleTemplate.jsp'>

• <template:put name='header' content='/header.html' />

• <template:put name='sidebar' content='/sidebar.jsp' />

• <template:put name='content' content='/introduction.html'/>

• <template:put name='footer' content='/footer.html' />

• </template:insert>

Page 137: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

Outline

Introduction to JSP

Advantages of JSP over Servlet

JSP Life cycle

JSP Creation in Eclipse

Elements of JSP page: directives, comments, scripting elements, actions and templates

JDBC Connectivity with JSP.

Page 138: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC

• Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers to connect with the database.

• Before JDBC, ODBC API was the database API to connect and execute query with the database.

• But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured).

• That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).

Page 139: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Driver

• JDBC Driver is a software component that enables java application to interact with the database.

• There are 4 types of JDBC drivers:

1. JDBC-ODBC bridge driver

2. Native-API driver (partially java driver)

3. Network Protocol driver (fully java driver)

4. Thin driver (fully java driver)

Page 140: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP

• Software Requirement

• 1 MySQL

• 2 MySQL Connector (Jar file)

• 3 Apache Tomcat Server

• 4 Eclipse

• Important Note:

• Copy MySQL Connector (Jar file) into Tomcat’s Bin and Lib folder

• Before running program right click on project name -> Build path ->

Configure build path -> Libraries -> Add External Jar file -> Mysql .jar

file -> ok

Page 141: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Create table and add rows in database(mysql)

• Create the Employee table in the db1 database as follows − −

• mysql> Create database db1;

• mysql> use db1;

• mysql> create table emp

• (

• id int,

• name varchar (255)

• );

• Query OK, 0 rows affected (0.08 sec)

Page 142: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Create table and add rows in database(mysql)

• Insert Data

• mysql> INSERT INTO emp VALUES (100, 'Zara'');

• mysql> INSERT INTO Employees VALUES (101, 'Mamta');

Page 143: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JSP Code in Eclipse

• File->New->Web->Dynamic Web Project->Give Project Name

• Right click on Project name->new->package->Give package name For Writing JSP Code

• project-> Right click->new->JSP file-> Give file name with .jsp

• Extension

• In Body tag write java code in script let tag <% %>

• Right click on .jsp file -> Run -> Run as Server

Page 144: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 1- JSP Code for to select data from emp table

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

<%@ page import ="javax.sql.*" %>

<%

Class.forName("com.mysql.jdbc.Driver");

java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");

Statement st= con.createStatement();

ResultSet rs=st.executeQuery("select * from emp");

out.print("<h2 align=center>Employee Database</h2>");

out.print("<table border=1 align=center>");

out.print("<tr><th> ENo</th> <th>Ename</th></tr>");

Page 145: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 1- JSP Code for to select data from emp table

while(rs.next()) { out.print("<tr align=center>"); out.print("<td>"); out.print(rs.getInt(1)); out.print("</td>"); out.print("<td>"); out.print(rs.getString(2)); out.print("</td>"); out.print("</tr>"); } %>

Page 146: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 2- JSP Code for insert data in emp table and display the inserted data from table

• It requires 2 files

• 1. S1.html (Html file to take values from user)

• 2. s2.jsp (jsp file to insert data in database and to display the inserted data)

Page 147: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 2- JSP Code for insert data in emp table and display the inserted data from table

• S1.html <html>

<body>

<form method="post" action=“s2.jsp">

Enter Employee No<input type="text" name="t1"><br>

Enter Empoyee Name <input type="text" name="t2"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

Page 148: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 2- JSP Code for insert data in emp table and display the inserted data from table

S2.jsp

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

<%@ page import ="javax.sql.*" %>

<%

Class.forName("com.mysql.jdbc.Driver");

java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/db1","root","root");

Statement st= con.createStatement();

int id= Integer.parseInt(request.getParameter("t1"));

String name=request.getParameter("t2");

st.executeUpdate("insert into emp values("+id+",'"+name+"')");

Page 149: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

JDBC Connectivity with JSP- Example 2- JSP Code for insert data in emp table and display the inserted data from table

ResultSet rs=st.executeQuery("select * from emp"); out.print("<h2 align=center>Employee Database</h2>"); out.print("<table border=1 align=center>"); out.print("<tr><th> ENo</th><th>Ename</th></tr>"); while(rs.next()) { out.print("<tr align=center>"); out.print("<td>"); out.print(rs.getInt(1)); out.print("</td>"); out.print("<td>"); out.print(rs.getString(2)); out.print("</td>"); out.print("</tr>"); } %>

Page 150: Server Side Technologies - Prof. Bhavana Khivsara...Web Technology Unit III- Server Side Technologies By Prof. B.A.Khivsara Note: The material to prepare this presentation has been

References

• https://www.javatpoint.com/cookies-in-servlet

• https://www.javatpoint.com/hidden-form-field-in-session-tracking

• https://www.studytonight.com/servlet/hidden-form-field.php

• https://www.studytonight.com/servlet/url-rewriting-for-session-management.php

• https://www.tutorialspoint.com/jsp/jsp_overview.htm

• https://www.javatpoint.com/jsp-tutorial

• https://www.studytonight.com/jsp/introduction-to-jsp.php

• https://www.studytonight.com/jsp/creating-a-jsp-page.php

• https://www.studytonight.com/jsp/jsp-scripting-element.php

• https://www.javatpoint.com/java-jdbc