Intro to JSP
Download
Report
Transcript Intro to JSP
Introduction to Java Web Development
Java Enterprise Edition
Java Web Development
Structure of a web project
Introduction to Web Applications
The first project
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 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
Java Enterprise Edition
Java Web Development
* Note--In this course only JSP & servlets will be discussed
Structure of a web project
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
in the server
Structure of a web project
in the server
Root of the project
Java classes (.class)
Java Libraries (.jar)
Configuration files
Anything web-related
- Directories
- JavaServer Pages (JSP)
- HTML
- Css files
- JavaScript Files
- Etc.
Introduction to Web Applications
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.
Servlets versus JSP
Life Cycle of a
JEE Web Application
Life Cycle of a
JEE Web Application
Criteria to develop web applications
Exercise 1
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
Results
You should get this result
Exercise 1
Analisis
The web.xml file
<?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>
Servlet
declarations
Exercise 1
Analisis
The web.xml file
. . .
<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>
Servlet
mappings
Exercise 1
Analisis
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 different 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
E g : < % @ p a ge c o n t e n t T y p e = ” t e x t / h t m l ” l a n g u a g e = ” j a v a ” % >
Attributes are:1.
Autoflush=”true/false”
2.
Buffer=”in KB”
3.
contentType=”info”
4.
errorPage=”page url”
5.
e x t e n d s = ” cl a s s n a m e ”
6.
i m p o r t = ” p a ck a g e s e p a r a t e d b y c o m m a ”
7.
info=”infotext”
8.
isELIgnored=”true/false”
9.
i s E r r o r P a g e = ” p a ge n a m e u r l ”
1 0 . i s T h r e a d S a f e = ”t r u e / f a l s e ”
11. language=”java”
1 2 . p a g e E n c o d i n g = ”I S O - 8 8 5 9 - 1 ”
1 3.
session=”true/false”
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 file=”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”
How many scripting Elements used in JSP?????
1. <%! This is a declaration %>
2. <% this is a scriptlet %>
3. <% = this is an expression %>
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”>
We are creating a login screen with java
bean which check the username and
password and return the result according to
input
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 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.e
quals("123"))
{
result=true;
public String getName() {
return name;
}
}
}
}
else
{
result=false;
}
return result;
Describing Action Elements in JSP ?????????
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 the JSP Implicit
Objects…………………….
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.