Vous êtes sur la page 1sur 38

By

javawithease

Java EE Containers

Java Sevlet 3.0


Java Server Pages 2.2
Java Server Faces 2.0
Enterprise JavaBeans 3.1
JavaMail 1.4

A servlet is a Java programming language


class used to extend the capabilities of
servers that host applications accessed via a
request-response programming model

importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassHelloWorldextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServle
tResponseresponse)throwsServletException,IOException
{
response.setContentType("text/html");
PrintWriterpw=response.getWriter();
pw.println("<html>");
pw.println("<head><title>HelloWorld</title></title>");
pw.println("<body>");
pw.println("<h1>HelloWorld</h1>");
pw.println("</body></html>");
}}

<web-app>

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>

JavaServer Pages (JSP) is a Java technology that


helps Software Developers, serve dynamically
generated pages, based on HTML, XML, or
other document types

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>
<%
out.println("Hello World");
%>
</h1>
</body>
</html>

There are five kinds tags that is available in


JSP
Directive Tag
Declaration Tag
Expression Tag
Scriplet

JSP directives provide directions and instructions


to the container, telling it how to handle certain
aspects of JSP processing.
A JSP directive affects the overall structure of the
servlet class. It usually has the following form:
<%@ directive attribute="value" %>

Types of directive tag:

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.
Following is the syntax of JSP Declarations:

<%! declaration; [ declaration; ]+ ... %>


You can write XML equivalent of the above syntax as follows:

<jsp:declaration> code fragment </jsp:declaration>

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.
Following is the syntax of JSP Expression:
<%=

expression

%>

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.
Following is the syntax of Scriptlet:

<% code fragment %>

You can write XML equivalent of the above syntax as


follows:
<jsp:scriptlet> code fragment </jsp:scriptlet>

JSP Comments:
JSP comment marks text or statements that the
JSP container should ignore. A JSP comment is
useful when you want to hide or "comment out"
part of your JSP page.
Following is the syntax of JSP comments:

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

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT
NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT
NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age
SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

public class UserData { String username;


String email;
int age;
public void setUsername( String value )
{
username = value;
}
public void setEmail( String value )
{
email = value;
}
public void setAge( int value )
{
age = value;
}

public String getUsername() { return username; }


public String getEmail() { return email; }
public int getAge() { return age; } }

<jsp:useBean id="user" class="UserData"


scope="session"/>
<jsp:setProperty name="user" property="*"/>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>

<jsp:useBean id="user" class="UserData"


scope="session"/>
<HTML>
<BODY>
You entered<BR>
Name: <%= user.getUsername() %><BR>
Email: <%= user.getEmail() %><BR>
Age: <%= user.getAge() %><BR>
</BODY>
</HTML>

The Enterprise JavaBeans architecture or EJB for


short is an architecture for the development
and deployment of component-based robust,
highly scalable business applications. These
Applications are scalable, transactional, and
multi-user secure.

Benefits of EJB
EJB simplifies the development of small and
large enterprise applications. The EJB container
provides system-level services to enterprise
beans, the bean developer can just concentrate
on developing logic to solve business
problems.

Session Bean
Entity Bean
Message-Driven Bean

Session is one of the EJBs and it represents a


single client inside the Application Server.
Stateless session is easy to develop and its
efficient. As compare to entity beans session
beans require few server resources.
A session bean is similar to an interactive
session and is not shared; it can have only
one client, in the same way that an interactive
session can have only one user. A session
bean is not persistent and it is destroyed once
the session terminates.

Session Bean Types


Stateless Session Beans

A stateless session bean does not maintain a conversational state


for the client. When a client invokes the method of a stateless bean,
the bean's instance variables may contain a state, but only for
the duration of the invocation.

Stateful Session Beans

The state of an object consists of the values of its instance variables.


In a stateful session bean, the instance variables represent the
state of a unique client-bean session. Because the client interacts
("talks") with its bean, this state is often called the conversational state.

Entity beans are persistence java objects,


whose state can be saved into the database
and later can it can be restored from Data
store. Entity bean represents a row of the
database table.

Message Driven Bean is an enterprise bean that


can be used by JEE applications to process the
messages asynchronously. Message Driven Bean
acts as message consumer and it receives JMS
messages.

Vous aimerez peut-être aussi