Top Banner
43

Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Dec 23, 2015

Download

Documents

Cory Henderson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Page 2: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Java Enterprise Edition

Java Web Development

Structure of a web project

Introduction to Web Applications

The first project

Introduction to Java Web Development

Page 3: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP is a java based technology used to simplify the development of dynamic web pages. JSP is used to separate dynamic content of web page from its content with the help of HTML and JSP tags. Current version is 2.1.

What Is JSP???

Page 4: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

What the Web Architecture of JSP?

Page 5: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP Model I Architecture

Page 6: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP Model II Architecture

Page 7: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

How Many Stages in JSP life Cycle

Page 8: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

1. Page Translation stage

2. Page compilation stage

3. Loading & initialization stage

4. Request handling stage

5. Destroying stage

Five Stages Of JSP Life Cycle

Page 9: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Java Enterprise Edition

Page 10: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Java Web Development

* Note--In this course only JSP & servlets will be discussed

Page 11: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

There are two kind of structures• The structure of the web application in a server• The structure of the IDE

A web project have three main elements• The JSPs files• The java classes• The Configuration file web.xml

Structure of a web project

Page 12: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Structure of a web project in the server

Page 13: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Structure of a web project in the server

Anything web-related- Directories- JavaServer Pages (JSP)- HTML- Css files- JavaScript Files- Etc.

Java Libraries (.jar)

Java classes (.class)

Configuration files

Root of the project

Page 14: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

In a Web Application, web components provide the dynamic extension capabilities for a web server.

Web components can be Java servlets, JSP pages, or web service endpoints.

The interaction between a web client and a web application is explained and illustrated in the next slide figure. • The client sends an HTTP request to the web server. • A web server that implements Java Servlet and JavaServer Pages

technology converts the request into an HTTPServletRequest object. • This object is delivered to a web component, which can interact

with JavaBeans components or a database to generate dynamic content. • The web component can then generate an HTTPServletResponse or

it can pass the request to another web component. • Eventually a web component generates a HTTPServletResponse

object. The web server converts this object to an HTTP response and returns it to the client.

Introduction to Web Applications

Page 15: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Introduction to Web Applications

Page 16: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Servlets are Java programming language classes that dynamically process requests and construct responses. 

JSP pages are text-based documents that execute as servlets but allow a more natural approach to creating static content.

Although servlets and JSP pages can be used interchangeably, each has its own strengths.

Servlets are best suited for service-oriented applications (web service endpoints are implemented as servlets) and the control functions of a presentation-oriented application, such as dispatching requests and handling nontextual data.

JSP pages are more appropriate for generating text-based markup such as HTML, Scalable Vector Graphics (SVG), Wireless Markup Language (WML), and XML.

Introduction to Web Applications

Page 17: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Servlets versus JSP

Page 18: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Life Cycle of a JEE Web Application

Page 19: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Life Cycle of a JEE Web Application

Page 20: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Criteria to develop web applications

Page 21: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Develope the first java web application

1. Download the file:jspservlet-00.zip

2. Unzip it

3. Import from Eclipse

4. Run it

Exercise 1

Page 22: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

You should get this result

Exercise 1Results

Page 23: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

The web.xml file

Exercise 1Analisis

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" . . . version="2.5">

<servlet> <display-name>HelloWorldServlet</display-name> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class> com.example.servlets.HelloWorldServlet </servlet-class> </servlet> <servlet> <display-name>AnotherServlet</display-name> <servlet-name>AnotherServlet</servlet-name> <servlet-class> com.example.servlets.AnotherServlet </servlet-class> </servlet>

Servletdeclarations

Page 24: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

The web.xml file

Exercise 1Analisis

. . . <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AnotherServlet</servlet-name> <url-pattern>/AnotherServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>

Servletmappings

Page 25: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Exercise 1Analisis

Servlet classes(other might be here classes too)

Other resources might be here

Page 26: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP life cycle as……….

Page 27: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

What Are jsp elements an their types????

Page 28: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP page consist of elements and templates data .These elements are described in diff erent elements type are:-

Directives:- JSP directives provides the page resources and their properties. Some are as:-

1. Page directive:-Used to provide page specific properties

Eg:<%@page contentType=”text/html” language=”java” %>

Attributes are:-

1. Autoflush=”true/false”

2. Buff er=”in KB”

3. contentType=”info”

4. errorPage=”page url”

5. extends=”classname”

6. import=”package separated by comma”

7. info=”infotext”

8. isELIgnored=”true/false”

9. isErrorPage=”pagenameurl”

10. isThreadSafe=”true/false”

11. language=”java”

12. pageEncoding=”ISO-8859-1”

13. session=”true/false”

What are JSP elements?????

Page 29: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

2. Include Directives:-The include directives tells the container to add the defined resource content inline to the JSP page during the translation time .This tag used to make reusability.

Eg :-

<%@ include fi le=”header.html” %>

3. Taglib Directives:-Taglib tag is used to include external tag library in your web page.

 

Attributes are:-

1.Uri=”url of taglibrary”

2.tagDir=”tag library directory”

3.prefix=”like object for library”

What are JSP elements?????

Page 30: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

1. <%! This is a declaration %>

2. <% this is a scriptlet %>

3. <% = this is an expression %>

How many scripting Elements used in JSP?????

Page 31: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Now I am Describing Action Elements in JSP ?????????

Page 32: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Action elements are certain directives that are given to the container perform certain task during the execution of a JSP page.

Describing Action Elements in JSP ?????????

Page 33: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

1. <JSP:useBean>:-

The useBean tag is used to create instance for specified java bean for JSP page.

Attributes of this tag:-

Id => what the object name you want to specifiy for this bean.

Scope => whats the scope of bean object page,request,session or application.

Class => case sensitive name of bean class

beanName => a valid bean name no include the bean classname

type => optional attribute java class has been defined.

Describing Action Elements in JSP ?????????

Page 34: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

1. <JSP:getProperty> :-

this action used to access the defines property to bean object.But it is important to declare bean object using useBean tag.

Attributes are:-

1. name :- The name of object where to retrieve object properties.

2. property: - Name of property to get.

Eg:-

<jsp:getProperty name=”beanname”property=”propertyname”>

Describing Action Elements in JSP ?????????

Page 35: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

1. <JSP:getProperty> :-

this action used to access the defines property to bean object.But it is important to declare bean object using useBean tag.

Attributes are:-

1. name :- The name of object where to retrieve object properties.

2. property: - Name of property to get.

Eg:-

<jsp:getProperty name=”beanname”property=”propertyname”>

Describing Action Elements in JSP ?????????

Page 36: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

. <JSP:setProperty> :-

This action element is used to set the property of bean object specified by usebean tag.

Attributes are:-

1.Name :- name of bean which have to specify value

2.Property:-

name of property value to be set we can also use * to specify all the property of bean.but in this case you have to specify the name of components as bean property.

3.Param:- The name of the request parameter whose value you want to give to a bean property.this name basically come from web form.

4.Value:- the value to be assign to this property.

Eg:-

<jsp:setProperty name=”beanname” property=”propertyname” value=”tobeset” >

=”propertyname”>

Describing Action Elements in JSP ?????????

Page 37: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

We are creating a login screen with java bean which check the username and password and return the result according to input

Now Understand this using Example

Page 38: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Index.jsp<%-- Document : index Created on : Apr 2, 2012, 12:23:12 PM Author : BARDHAN--%> <%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form action="check.jsp" method="POST"> Enter name <input type="text" name="name" /><br> Enter Password <input type="password" name="password" /> <br/> <input type="submit" value="LOGIN" /> <input type="reset" value="reset" /> </form> </body></html>

Page 39: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Check.jsp Page:-<%-- Document : check Created on : Apr 2, 2012, 12:29:55 PM Author : BARDHAN--%> <%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <jsp:useBean id="login" class="beans.login" scope="page"> <jsp:setProperty name="login" property="*" /> </jsp:useBean> Your Name:<jsp:getProperty name="login" property="name" /> <br> Your Password is:<jsp:getProperty name="login" property="password" /> <%boolean ba=login.getResult(); if(ba) { response.sendRedirect("welcome.jsp"); } else{ out.println("This is not right username or password"); } %> </body></html>

Page 40: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

Bean.login bean:-/* * To change this template, choose Tools | Templates * and open the template in the editor. */package beans; /** * * @author BARDHAN */public class login { private String name; private String password; private boolean result;  public String getName() { return name; }

  public void setName(String name) { this.name = name; }  public String getPassword() { return password; }  public void setPassword(String password) { this.password = password; } public boolean getResult() { if(this.name.equals("sanju")&&password.equals("123")) { result=true; } else { result=false; } return result; } }

Page 41: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

4. .<JSP:param>:-

This action element is used to pass the parameter to other action elements. For example

<jsp:forward page=”hello.jsp”>

<jsp:param name=”id” value=”12” />

</jsp:forward>

Describing Action Elements in JSP ?????????

Page 42: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

5.<jsp:include> :-

This action element is used to include any jsp file in your web page.

<jsp:include page=”header.jsp” flush=”true|false” />

6.<jsp:forward> :-

Used to forward request control to next page .For example

<jsp:forward page=”next.jsp” ></jsp:forward>

 

7.<jsp:expression>:- work like expression script

8.<jsp:scriptlet> :- work like as script element

9.<jsp:declaration> :-work as declaration element

Describing Action Elements in JSP ?????????

Page 43: Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.

JSP Implicit objects are described as follows

1.Application :- Used as Servlet Context object in servlets

2.Config:- Used as ServletConfig object in servlets

3.Out :- JSPWriter object to print any thing on jsp page

4.Page:- work like as Object class object

5.pageContext:- PageConext for JSP page

6.request: work like as ServletRequest object

7.response:- work like as ServletResponse Object in Servlets

8.session :- like as httpSession.

Describing the JSP Implicit Objects…………………….