- Java servlets : one of the primary components of the J2EE platform
- The following diagram has basic two-tier model:
- Client tier: Web browser/HTML pages
- Web tier: Web server/servlet container
![]()
The containers are J2EE runtime environments that provide required low-level, platform-specific services to the application components
- When the Web server receives the input, it passes the request to the servlet.
- The servlet then calls a method for action.
- When the servlet receives the results it returns the calculated value to the end user in an HTML page, using the response object passed by the Web server.
- The servlet is bundled with its HTML file into a Web Archive (WAR) file, called as Web modules.
- The WAR module will be added to the J2EE application and bundled into an Enterprise Archive (EAR) file.
- Writing the servlet is a developer function, while creating a J2EE application and adding J2EE components is an application assembly function.
- HTML page that accepts the input.
- Create the Servlet: for processing and dynamic contents.
importDeclarations: J2EE classes/interfaces:
javax.servlet.ServletException: TheHttpServletclasses use this class to indicate a servlet problem.javax.servlet.ServletConfig: This class encapsulates the configuration information about aServlet.javax.servlet.http.HttpServlet: Every HTTP servlet suitable for a Web site should subclass this abstract class.javax.servlet.http.HttpServletRequest: This interface provides the request information for HTTP servlets.javax.servlet.http.HttpServletResponse: This interface provides HTTP-specific functionality in sending a response to a request.java.io.IOException: TheHttpServletclasses use theIOExceptionclass to signal that an input or output exception of some kind has occurred.java.io.PrintWriter: TheServletclasses use this class to write the response data.
initMethod:
- This method is called by the servlet container.
- Input parameter : Servlet configuration information (as
ServletConfig).
doGetMethod:
- takes a
HttpServletRequestandHttpServletResponseobject.
HttpServletRequest:information that the browser sent is captured.HttpServletResponse: used to create an HTML page in response to the browser's request.- The
doGetmethod throws anIOExceptionif there is an input or output problem
destroy Method:
- This method will be called by the servlet container when this servlet is released from service
- Compile the Servlet.
- J2EE Application Server:
- J2EE application server to deploy and run the Application.
Deployment Tool: J2EE applications and their components
- Create J2EE Application
- to assemble J2EE components into a J2EE application Enterprise Archive (EAR) file:
- JAR file and J2EE application, and provides runtime information about the application.
Create the Web Component:
- Web components (Java servlets and associated classes, JavaServer Pages, HTML pages, and other relevant Web resources like images) are bundled into a Web Archive (WAR) file.
- Specify the Root Context:
You have to specify a context root directory where the Deployment tool will put the Web components.
Context Root Directory Structure:
![]()
Verify and Deploy the J2EE Application
- Run the J2EE Application
Multitiered Architecture: 4 Tier:
<jsp:XXX ... />
are converted into method calls to JavaBeans components or
invocations of the Java Servlet API.jspInit
method_jspService
method, passing a request and response object.jspDestroy method.| Variable |
Class |
Description |
|---|---|---|
application |
javax.servlet.ServletContext |
The context for the JSP
page's servlet and any Web components contained in the same
application. See Accessing
the Web Context. |
config |
javax.servlet.ServletConfig |
Initialization information
for the JSP page's servlet. |
exception |
java.lang.Throwable |
Accessible only from an
error page. See Handling
Errors. |
out |
javax.servlet.jsp.JspWriter |
The output stream. |
page |
java.lang.Object |
The instance of the JSP
page's servlet processing the current request. Not typically
used by JSP page authors. |
pageContext |
javax.servlet.jsp.PageContext |
The context for the
JSP page. Provides a single API to manage the various scoped
attributes described in Sharing
Information. This API is used extensively when implementing tag handlers. See Tag Handlers. |
request |
Subtype of javax.servlet.ServletRequest |
The request triggering the
execution of the JSP page. See Getting
Information from Requests. |
response |
Subtype of javax.servlet.ServletResponse |
The response to be returned
to the client. Not typically used by JSP page authors. |
session |
javax.servlet.http.HttpSession |
The session object for the
client. See Accessing
the Web Context. |
The container, shown in the shaded box, is the interface between the session bean and the low-level platform-specific functionality that supports the session bean. The container is created during deployment.
![]()
Application Components
Container-managed persistence is the term used to describe the situation where the container handles data storage and retrieval.
Override the default container-managed persistence and implement bean-managed persistence (BMP).
BMP can be useful if you need to improve performance or map data in multiple beans to one row in a database table.
Bean Lifecycle:
J2EE is a platform for developing distributed enterprise applications using the Java programming language.
J2EE application server, Web server, database, J2EE APIs.
Development and Deployment tools.
| Keyword | Description | Size/Format |
|---|---|---|
| (integers) | ||
byte |
Byte-length integer | 8-bit two's complement |
short |
Short integer | 16-bit two's complement |
int |
Integer | 32-bit two's complement |
long |
Long integer | 64-bit two's complement |
| (real numbers) | ||
float |
Single-precision floating point | 32-bit IEEE 754 |
double |
Double-precision floating point | 64-bit IEEE 754 |
| (other types) | ||
char |
A single character | 16-bit Unicode character |
boolean |
A boolean value (true or false) |
true or false |
Scope of Variable:

| Operator | Use | Description |
|---|---|---|
+ |
op1 + op2 |
Adds op1 and op2 |
- |
op1 - op2 |
Subtracts op2 from op1 |
* |
op1 * op2 |
Multiplies op1 by op2 |
/ |
op1 / op2 |
Divides op1 by op2 |
% |
op1 % op2 |
Computes the remainder of dividing op1 by op2 |
The shortcut increment/decrement operators are summarized in the following table.
| Operator | Use | Description |
|---|---|---|
++ |
op++ |
Increments op by 1; evaluates to the value of op
before it was incremented |
++ |
++op |
Increments op by 1; evaluates to the value of op
after it was incremented |
-- |
op-- |
Decrements op by 1; evaluates to the value of op
before it was decremented |
-- |
--op |
Decrements op by 1; evaluates to the value of op
after it was decremented |
| Operator | Use | Returns true if |
|---|---|---|
> |
op1 > op2 |
op1 is greater than op2 |
>= |
op1 >= op2 |
op1 is greater than or equal to op2 |
< |
op1 < op2 |
op1 is less than op2 |
<= |
op1 <= op2 |
op1 is less than or equal to op2 |
== |
op1 == op2 |
op1 and op2 are equal |
!= |
op1 != op2 |
op1 and op2 are not equal |
The Java programming language supports six conditional operators-five binary and one unary--as shown in the following table.
| Operator | Use | Returns true if |
|---|---|---|
&& |
op1 && op2 |
op1 and op2 are both true,
conditionally evaluates op2 |
|| |
op1 || op2 |
either op1 or op2 is true,
conditionally evaluates op2 |
! |
! op |
op is false |
& |
op1 & op2 |
op1 and op2 are both true,
always evaluates op1 and op2 |
| |
op1 | op2 |
either op1 or op2 is true,
always evaluates op1 and op2 |
^ |
op1 ^ op2 |
if op1 and op2 are different--that is if one or the other of the operands is true but not both |
| Operator | Use | Operation |
|---|---|---|
>> |
op1 >> op2 |
shift bits of op1 right by distance op2 |
<< |
op1 << op2 |
shift bits of op1 left by distance op2 |
>>> |
op1 >>> op2 |
shift bits of op1 right by distance op2
(unsigned) |
The following table shows the four operators the Java programming language provides to perform bitwise functions on their operands:
| Operator | Use | Operation |
|---|---|---|
& |
op1 & op2 |
bitwise and |
| |
op1 | op2 |
bitwise or |
^ |
op1 ^ op2 |
bitwise xor |
~ |
~op2 |
bitwise complement |
Assignment Operators:
The following table lists the shortcut assignment operators and their lengthy equivalents:
| Operator | Use | Equivalent to |
|---|---|---|
+= |
op1 += op2 |
op1 = op1 + op2 |
-= |
op1 -= op2 |
op1 = op1 - op2 |
*= |
op1 *= op2 |
op1 = op1 * op2 |
/= |
op1 /= op2 |
op1 = op1 / op2 |
%= |
op1 %= op2 |
op1 = op1 % op2 |
&= |
op1 &= op2 |
op1 = op1 & op2 |
|= |
op1 |= op2 |
op1 = op1 | op2 |
^= |
op1 ^= op2 |
op1 = op1 ^ op2 |
<<= |
op1 <<= op2 |
op1 = op1 << op2 |
>>= |
op1 >>= op2 |
op1 = op1 >> op2 |
>>>= |
op1 >>>= op2 |
op1 = op1 >>> op2 |
The following table lists the other operators that the Java programming language supports.
Operator Description ?:Shortcut if-elsestatement
[]Used to declare arrays, create arrays, and access array elements .Used to form qualified names (params)Delimits a comma-separated list of parameters (type)Casts (converts) a value to the specified type newCreates a new object or a new array instanceofDetermines whether its first operand is an instance of its second operand
Expressions, Statements, and Blocks:
Each of these expressions performs an operation and returns a value.
| Expression | Action | Value Returned |
|---|---|---|
aChar = 'S' |
Assign the character 'S' to the character variable aChar |
The value of aChar after the assignment
('S') |
"The largest byte value is " +
largestByte |
Concatenate the string "The largest byte
value is " and the value of largestByte
converted to a string |
The resulting string: The largest byte value is
127 |
Character.isUpperCase(aChar) |
Call the method isUpperCase |
The return value of the method: true |
The following table shows the precedence assigned to the operators. The operators in this table are listed in precedence order: the higher in the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with a relatively lower precedence. Operators on the same line have equal precedence.
| postfix operators | [] . (params) expr++ expr-- |
| unary operators | ++expr --expr +expr -expr
~ ! |
| creation or cast | new (type)expr |
| multiplicative | * / % |
| additive | + - |
| shift | << >> >>> |
| relational | < > <= >= instanceof |
| equality | == != |
| bitwise AND | & |
| bitwise exclusive OR | ^ |
| bitwise inclusive OR | | |
| logical AND | && |
| logical OR | || |
| conditional | ? : |
| assignment | = += -= *= /= %= &= ^= |= <<= >>=
>>>= |
while (expression) {
statement
}
do {
statement(s)
} while (expression);
for (initialization; termination; increment) {
statement
}
for ( ; ; ) { // infinite loop
...
}
if (expression) {
statement(s)
}
Statement to conditionally perform statements based on an integer expression
try { statement(s) } catch (exceptiontype name) { statement(s) } finally { statement(s) }
- The try statement identifies a block of statements within which an exception might be thrown.
- The catch statement must be associated with a
trystatement and identifies a block of statements that can handle a particular type of exception. The statements are executed if an exception of a particular type occurs within thetryblock.- The finally statement must be associated with a
trystatement and identifies a block of statements that are executed regardless of whether or not an error occurs within thetryblock.
switch statement,
a for, while, or do-while
loop.for, while
, or do-while loop.return [expr.];
You use return to exit from the current method. The flow of control
returns to the statement that follows the original method call.
Objects and Data Objects:
All the classes in the
java.langpackage are available to your programs automatically.Character data :
- classes in
java.lang:Character,String, andStringBuffer.
- An object of
Charactertype contains a single character value.
- Example: Character a = new Character('a');
- The
Stringclass provides for strings whose value will not change.
- String palindrome = "Dot saw I was Tod";
int len = palindrome.length();- The
StringBufferclass provides for strings that will be modified;
Getting the Length of a String or a String Buffer.
- StringBuffer dest = new StringBuffer(len);
The
toStringMethod and ThereverseItmethod:- Converting Strings to Numbers:
String piStr = "3.14159"; Float pi = Float.valueOf(piStr);Numeric data: number classes:
- The
Numberclass is the super class for all number classes in the Java platform.- Its subclasses include
Float,Integer, and so on.Arrays are implicit extensions of the
ObjectclassThe Life Cycle of an Object:
Once an object has completed the work for which it was created, it is garbage-collected and its resources are recycled for use by other objects.
Creating Objects: you create an object from a class
Example: Point origin_one = new Point(23, 94);
Declaring a Variable to Refer to an Object.
Use the object.
The Garbage Collector:
The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced.
Finalization:
Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's
finalizemethod.Creating Classes:
Write the classes from which objects are created:
The Class Declaration:
public- By default, a class can be used only by other classes in the same package. The public modifier declares that the class can be used by any class regardless of its package.
abstract- Declares that the class cannot be instantiated..
final- Declares that the class cannot be subclassed.
class NameOfClass- The
classkeyword indicates to the compiler that this is a class declaration and that the name of the class isNameOfClass.
extends Super- The
extendsclause identifiesSuperas the superclass of the class, thereby inserting the class within the class hierarchy.- Variables and methods collectively are called members.
Declaring Member Variables
transient- The
transientmarker is not fully specified by The Java Language Specification but is used in object serialization.
volatile- The
volatilekeyword is used to prevent the compiler from performing certain optimizations on a member.Implementing Methods
Method declaration:
returnType methodName() { . . . }
native- If you have a significant library of functions written in another language such as C, you may wish to preserve that investment and use those functions from Java.
synchronized- Concurrently running threads often invoke methods that operate on the same data. These methods may be declared synchronized to ensure that the threads access information in a thread-safe manner.
| Click to go to Home Page |