java enterprise edition java web development structure of a web project introduction to web...

43

Upload: cory-henderson

Post on 23-Dec-2015

227 views

Category:

Documents


6 download

TRANSCRIPT

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???

What the Web Architecture of JSP?

JSP Model I Architecture

JSP Model II Architecture

How Many Stages in JSP life Cycle

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

Java Enterprise Edition

Java Web Development

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

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

Structure of a web project in the server

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

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

Introduction to Web Applications

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

Servlets versus JSP

Life Cycle of a JEE Web Application

Life Cycle of a JEE Web Application

Criteria to develop web applications

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

You should get this result

Exercise 1Results

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

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

Exercise 1Analisis

Servlet classes(other might be here classes too)

Other resources might be here

JSP life cycle as……….

What Are jsp elements an their types????

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?????

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?????

1. <%! This is a declaration %>

2. <% this is a scriptlet %>

3. <% = this is an expression %>

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

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

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 ?????????

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 ?????????

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 ?????????

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 ?????????

. <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 ?????????

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

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>

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>

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; } }

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 ?????????

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 ?????????

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…………………….