Engineering lessons is a blog for engineer departments like programing ,communication,electronic,civil,mechanics
Monday, October 31, 2011
scjp question and answer by photos
Thursday, October 27, 2011
Creational Patterns
All of the creational patterns deal with the best way to create
instances of objects. This is important because your program should not
depend on how objects are created and arranged. In Java, of course, the
simplest way to create an instance of an object is by using the new operator.
Fred = new Fred(); //instance of Fred class
However, this really amounts to hard coding, depending on how you
create the object within your program. In many cases, the exact nature of the
object that is created could vary with the needs of the program and
abstracting the creation process into a special "creator" class can make your
program more flexible and general.
The Factory Method provides a simple decision making class that
returns one of several possible subclasses of an abstract base class depending
on the data that are provided.
The Abstract Factory Method provides an interface to create and
return one of several families of related objects.
The Builder Pattern separates the construction of a complex object
from its representation, so that several different representations can be created
depending on the needs of the program.
The Prototype Pattern starts with an initialized and instantiated
class and copies or clones it to make new instances rather than creating new
instances.
The Singleton Pattern is a class of which there can be no more than
one instance. It provides a single global point of access to that instance.
instances of objects. This is important because your program should not
depend on how objects are created and arranged. In Java, of course, the
simplest way to create an instance of an object is by using the new operator.
Fred = new Fred(); //instance of Fred class
However, this really amounts to hard coding, depending on how you
create the object within your program. In many cases, the exact nature of the
object that is created could vary with the needs of the program and
abstracting the creation process into a special "creator" class can make your
program more flexible and general.
The Factory Method provides a simple decision making class that
returns one of several possible subclasses of an abstract base class depending
on the data that are provided.
The Abstract Factory Method provides an interface to create and
return one of several families of related objects.
The Builder Pattern separates the construction of a complex object
from its representation, so that several different representations can be created
depending on the needs of the program.
The Prototype Pattern starts with an initialized and instantiated
class and copies or clones it to make new instances rather than creating new
instances.
The Singleton Pattern is a class of which there can be no more than
one instance. It provides a single global point of access to that instance.
--
Posted By م /عوض خليل سيد to Engineering lessons at 10/27/2011 05:23:00 PM
--
Eng: Awad Khalil Sayed
Team leader
Mobile:+20104940612
Design Patterns definition
We all talk about the way we do things in our everyday work,
hobbies and home life and recognize repeating patterns all the time.
We see the same thing in programming, when we tell a colleague
how we accomplished a tricky bit of programming so he doesn't have to
recreate it from scratch. We simply recognize effective ways for objects to
communicate while maintaining their own separate existences.
Some useful definitions of design patterns have emerged as the
literature in his field has expanded:
implementation." (Coplien & Schmidt, 1995).
But while it is helpful to draw analogies to architecture, cabinet
making and logic, design patterns are not just about the design of objects, but
about the communication between objects. In fact, we sometimes think of
them as communication patterns. It is the design of simple, but elegant,
methods of communication that makes many design patterns so important.
Design patterns can exist at many levels from very low level specific
solutions to broadly generalized system issues. There are now in fact
hundreds of patterns in the literature. They have been discussed in articles
and at conferences of all levels of granularity. Some are examples which have
wide applicability and a few (Kurata, 1998) solve but a single problem.
It has become apparent that you don't just write a design pattern off
the top of your head. In fact, most such patterns are discovered rather than
written. The process of looking for these patterns is called "pattern mining,"
and is worthy of a book of its own.
The 23 design patterns selected for inclusion in the original Design
Patterns book were ones which had several known applications and which
were on a middle level of generality, where they could easily cross
application areas and encompass several objects.
The authors divided these patterns into three types creational,
structural and behavioral.
flexibility in deciding which objects need to be created for a given case.
We'll be looking at Java versions of these patterns in the chapters that
follow.
hobbies and home life and recognize repeating patterns all the time.
- Sticky buns are like dinner rolls, but I add brown sugar and nut filling to
- Her front garden is like mine, but, in mine I use astilbe.
- This end table is constructed like that one, but in this one, the doors
We see the same thing in programming, when we tell a colleague
how we accomplished a tricky bit of programming so he doesn't have to
recreate it from scratch. We simply recognize effective ways for objects to
communicate while maintaining their own separate existences.
Some useful definitions of design patterns have emerged as the
literature in his field has expanded:
- Design patterns are recurring solutions to design problems you see over
- Design patterns constitute a set of rules describing how to accomplish
- Design patterns focus more on reuse of recurring architectural design
implementation." (Coplien & Schmidt, 1995).
- A pattern addresses a recurring design problem that arises in specific
- Patterns identify and specify abstractions that are above the level of
But while it is helpful to draw analogies to architecture, cabinet
making and logic, design patterns are not just about the design of objects, but
about the communication between objects. In fact, we sometimes think of
them as communication patterns. It is the design of simple, but elegant,
methods of communication that makes many design patterns so important.
Design patterns can exist at many levels from very low level specific
solutions to broadly generalized system issues. There are now in fact
hundreds of patterns in the literature. They have been discussed in articles
and at conferences of all levels of granularity. Some are examples which have
wide applicability and a few (Kurata, 1998) solve but a single problem.
It has become apparent that you don't just write a design pattern off
the top of your head. In fact, most such patterns are discovered rather than
written. The process of looking for these patterns is called "pattern mining,"
and is worthy of a book of its own.
The 23 design patterns selected for inclusion in the original Design
Patterns book were ones which had several known applications and which
were on a middle level of generality, where they could easily cross
application areas and encompass several objects.
The authors divided these patterns into three types creational,
structural and behavioral.
- Creational patterns are ones that create objects for you, rather than
flexibility in deciding which objects need to be created for a given case.
- Structural patterns help you compose groups of objects into larger
- Behavioral patterns help you define the communication between objects
We'll be looking at Java versions of these patterns in the chapters that
follow.
--
Posted By م /عوض خليل سيد to Engineering lessons at 10/27/2011 05:20:00 PM
--
Eng: Awad Khalil Sayed
Team leader
Mobile:+20104940612
What’s a Mapplet?
Mapplets are small web applications that run within Google Maps. They are a
type of Google Gadget—the framework that allows developers to create small
web applications that can run within iGoogle, Google Maps, Orkut, or any web
page outside Google. Mapplets use the basic concepts and APIs of Google
Gadgets, though they are specifically used within the Google Maps realm.
In its basic form, a Mapplet is an XML file that contains the HTML and
JavaScript that make up your application. Mapplets have two components:
?? A web application that is displayed on the Google Maps site at
http://maps.google.com. This application is typically displayed in the lowerleft
corner of the page on http://maps.google.com.
?? JavaScript that lets you control the map on http://maps.google.com, retrieve
external data, or even store and retrieve user preferences.
After you create your Mapplet, it's up to you to host the Mapplet XML file
containing your HTML and JavaScript. When a user installs your Mapplet,
Google will grab the Mapplet XML file from your server and display it within an
<iframe> element on http://maps.google.com. (An <iframe> is an HTML element
that is used to embed HTML from another web site in a web page.) In this case,
Google uses an <iframe> element to embed your Mapplet in
http://maps.google.com.
Google will cache your Mapplet source and serve it from its own servers at
http://gmodules.com. This is done for a few reasons:
?? To restrict your Mapplet's JavaScript from doing anything harmful such as
accessing a user's cookies on http://maps.google.com
?? To reduce the load on your site from the potential high number of users you'll
have
You'll learn more about how to create Mapplets in this chapter, but before you
do, let's take a look at a live Mapplet. Open your browser to
http://maps.google.com/. Click the My Maps tab, and you will see a list of
Featured Content along with any maps you have created. Select the one about gas
prices, and you'll see a Mapplet in action. Figure 1-1 shows the Gas Prices from
GasBuddy.com Mapplet. This Mapplet consists of the application, at the bottom
of the left column, and all the map interaction. If the Gas Prices Mapplet is
available in your list of featured Mapplets, give it try—enter your ZIP/postal
code in the search box, and see what the gas prices are in your area.
type of Google Gadget—the framework that allows developers to create small
web applications that can run within iGoogle, Google Maps, Orkut, or any web
page outside Google. Mapplets use the basic concepts and APIs of Google
Gadgets, though they are specifically used within the Google Maps realm.
In its basic form, a Mapplet is an XML file that contains the HTML and
JavaScript that make up your application. Mapplets have two components:
?? A web application that is displayed on the Google Maps site at
http://maps.google.com. This application is typically displayed in the lowerleft
corner of the page on http://maps.google.com.
?? JavaScript that lets you control the map on http://maps.google.com, retrieve
external data, or even store and retrieve user preferences.
After you create your Mapplet, it's up to you to host the Mapplet XML file
containing your HTML and JavaScript. When a user installs your Mapplet,
Google will grab the Mapplet XML file from your server and display it within an
<iframe> element on http://maps.google.com. (An <iframe> is an HTML element
that is used to embed HTML from another web site in a web page.) In this case,
Google uses an <iframe> element to embed your Mapplet in
http://maps.google.com.
Google will cache your Mapplet source and serve it from its own servers at
http://gmodules.com. This is done for a few reasons:
?? To restrict your Mapplet's JavaScript from doing anything harmful such as
accessing a user's cookies on http://maps.google.com
?? To reduce the load on your site from the potential high number of users you'll
have
You'll learn more about how to create Mapplets in this chapter, but before you
do, let's take a look at a live Mapplet. Open your browser to
http://maps.google.com/. Click the My Maps tab, and you will see a list of
Featured Content along with any maps you have created. Select the one about gas
prices, and you'll see a Mapplet in action. Figure 1-1 shows the Gas Prices from
GasBuddy.com Mapplet. This Mapplet consists of the application, at the bottom
of the left column, and all the map interaction. If the Gas Prices Mapplet is
available in your list of featured Mapplets, give it try—enter your ZIP/postal
code in the search box, and see what the gas prices are in your area.
Monday, October 24, 2011
What Is a JSP Page?
A JSP page is a text document that contains two types of text: static data, which can be expressed
in any text-based format (such as HTML, SVG,WML, and XML), and JSP elements, which
construct dynamic content.
The recommended file extension for the source file of a JSP page is .jsp. The page can be
composed of a top file that includes other files that contain either a complete JSP page or a
fragment of a JSP page. The recommended extension for the source file of a fragment of a JSP
page is .jspf.
The JSP elements in a JSP page can be expressed in two syntaxes, standard and XML, though
any given file can use only one syntax. A JSP page in XML syntax is an XML document and can
be manipulated by tools and APIs for XML documents. This chapter and Chapters Chapter 7,
“JavaServer Pages Standard Tag Library,” through Chapter 9, “Scripting in JSP Pages,”
document only the standard syntax. The XML syntax is covered in Chapter 6, “JavaServer Pages
Documents.”
in any text-based format (such as HTML, SVG,WML, and XML), and JSP elements, which
construct dynamic content.
The recommended file extension for the source file of a JSP page is .jsp. The page can be
composed of a top file that includes other files that contain either a complete JSP page or a
fragment of a JSP page. The recommended extension for the source file of a fragment of a JSP
page is .jspf.
The JSP elements in a JSP page can be expressed in two syntaxes, standard and XML, though
any given file can use only one syntax. A JSP page in XML syntax is an XML document and can
be manipulated by tools and APIs for XML documents. This chapter and Chapters Chapter 7,
“JavaServer Pages Standard Tag Library,” through Chapter 9, “Scripting in JSP Pages,”
document only the standard syntax. The XML syntax is covered in Chapter 6, “JavaServer Pages
Documents.”
The source code for this example is in the tut-install/javaeetutorial5/examples/web/date/
directory. The JSP page, index.jsp, appears below; it is a typical mixture of static HTML
markup and JSP elements. If you have developed web pages, you are probably familiar with the
HTML document structure statements (<head>, <body>, and so on) and the HTML statements
that create a form (<form>) and a menu (<select>).
The lines in bold in the example code contain the following types of JSP constructs:
■ A page directive (<%@page ... %>) sets the content type returned by the page.
■ Tag library directives (<%@taglib ... %>) import custom tag libraries.
■ jsp:useBean is a standard element that creates an object containing a collection of locales
and initializes an identifier that points to that object.
■ JSP expression language expressions (${ }) retrieve the value of object properties. The
values are used to set custom tag attribute values and create dynamic content.
■ Custom tags (see Chapter 8, “Custom Tags in JSP Pages”) set a variable (c:set), iterate over
a collection of locale names (c:forEach), and conditionally insert HTML text into the
response (c:if, c:choose, c:when, c:otherwise).
■ jsp:setProperty is another standard element that sets the value of an object property.
■ A function (f:equals) tests the equality of an attribute and the current item of a collection.
(A built-in == operator is usually used to test equality.)
Here is the JSP page:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/cor"
prefix="c" %>
<%@ taglib uri="/functions" prefix="f" %>
<html>
<head><title>Localized Dates</title></head>
<body bgcolor="white">
<jsp:useBean id="locales" scope="application"
class="mypkg.MyLocales"/>
<form name="localeForm" action="index.jsp" method="post">
<c:set var="selectedLocaleString" value="${param.locale}" />
<c:set var="selectedFlag"
value="${!empty selectedLocaleString}" />
<b>Locale:</b>
<select name=locale>
<c:forEach var="localeString" items="${locales.localeNames}" >
<c:choose>
<c:when test="${selectedFlag}">
<c:choose>
<c:when
test="${f:equals(selectedLocaleString, localeString)}" >
<option selected>${localeString}</option>
</c:when>
<c:otherwise>
<option>${localeString}</option>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<option>${localeString}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
<input type="submit" name="Submit" value="Get Date">
</form>
<c:if test="${selectedFlag}" >
<jsp:setProperty name="locales"
property="selectedLocaleString"
value="${selectedLocaleString}" />
<jsp:useBean id="date" class="mypkg.MyDate"/>
<jsp:setProperty name="date" property="locale"
value="${locales.selectedLocale}"/>
<b>Date: </b>${date.date}</c:if>
</body>
</html>
directory. The JSP page, index.jsp, appears below; it is a typical mixture of static HTML
markup and JSP elements. If you have developed web pages, you are probably familiar with the
HTML document structure statements (<head>, <body>, and so on) and the HTML statements
that create a form (<form>) and a menu (<select>).
The lines in bold in the example code contain the following types of JSP constructs:
■ A page directive (<%@page ... %>) sets the content type returned by the page.
■ Tag library directives (<%@taglib ... %>) import custom tag libraries.
■ jsp:useBean is a standard element that creates an object containing a collection of locales
and initializes an identifier that points to that object.
■ JSP expression language expressions (${ }) retrieve the value of object properties. The
values are used to set custom tag attribute values and create dynamic content.
■ Custom tags (see Chapter 8, “Custom Tags in JSP Pages”) set a variable (c:set), iterate over
a collection of locale names (c:forEach), and conditionally insert HTML text into the
response (c:if, c:choose, c:when, c:otherwise).
■ jsp:setProperty is another standard element that sets the value of an object property.
■ A function (f:equals) tests the equality of an attribute and the current item of a collection.
(A built-in == operator is usually used to test equality.)
Here is the JSP page:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/cor"
prefix="c" %>
<%@ taglib uri="/functions" prefix="f" %>
<html>
<head><title>Localized Dates</title></head>
<body bgcolor="white">
<jsp:useBean id="locales" scope="application"
class="mypkg.MyLocales"/>
<form name="localeForm" action="index.jsp" method="post">
<c:set var="selectedLocaleString" value="${param.locale}" />
<c:set var="selectedFlag"
value="${!empty selectedLocaleString}" />
<b>Locale:</b>
<select name=locale>
<c:forEach var="localeString" items="${locales.localeNames}" >
<c:choose>
<c:when test="${selectedFlag}">
<c:choose>
<c:when
test="${f:equals(selectedLocaleString, localeString)}" >
<option selected>${localeString}</option>
</c:when>
<c:otherwise>
<option>${localeString}</option>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<option>${localeString}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
<input type="submit" name="Submit" value="Get Date">
</form>
<c:if test="${selectedFlag}" >
<jsp:setProperty name="locales"
property="selectedLocaleString"
value="${selectedLocaleString}" />
<jsp:useBean id="date" class="mypkg.MyDate"/>
<jsp:setProperty name="date" property="locale"
value="${locales.selectedLocale}"/>
<b>Date: </b>${date.date}</c:if>
</body>
</html>
To deploy the date application withNetBeans IDE, follow these steps:
1. Start the Application Server.
2. InNetBeans IDE, select File→Open Project.
3. In the Open Project dialog, navigate to:
tut-install/javaeetutorial5/examples/web/
4. Select the date folder.
5. Select the Open asMain Project check box.
6. Click Open Project.
7. In the Projects tab, right-click the date project, and select Undeploy andDeploy.
To deploy the date application with the Ant tool, follow these steps:
1. In a terminal window, go to tut-install/javaeetutorial5/examples/web/date/.
2. Type ant. This command will spawn any necessary compilations, copy files to the
tut-install/javaeetutorial5/examples/web/date/build/ directory, and create aWAR
file.
3. Start the Application Server.
4. Type ant deploy.
To run the example, do the following:
1. Set the character encoding in your browser to UTF-8.
2. Open your browser to http://localhost:8080/date.
3. You will see a combo box whose entries are locales. Select a locale and click GetDate. You
will see the date expressed in a manner appropriate for that locale.
Some of the characters might not display properly if you don’t have the appropriate language
files installed on your machine. Consult the user guide or online help for your operating system
to determine how you can install these language files.
1. Start the Application Server.
2. InNetBeans IDE, select File→Open Project.
3. In the Open Project dialog, navigate to:
tut-install/javaeetutorial5/examples/web/
4. Select the date folder.
5. Select the Open asMain Project check box.
6. Click Open Project.
7. In the Projects tab, right-click the date project, and select Undeploy andDeploy.
To deploy the date application with the Ant tool, follow these steps:
1. In a terminal window, go to tut-install/javaeetutorial5/examples/web/date/.
2. Type ant. This command will spawn any necessary compilations, copy files to the
tut-install/javaeetutorial5/examples/web/date/build/ directory, and create aWAR
file.
3. Start the Application Server.
4. Type ant deploy.
To run the example, do the following:
1. Set the character encoding in your browser to UTF-8.
2. Open your browser to http://localhost:8080/date.
3. You will see a combo box whose entries are locales. Select a locale and click GetDate. You
will see the date expressed in a manner appropriate for that locale.
Some of the characters might not display properly if you don’t have the appropriate language
files installed on your machine. Consult the user guide or online help for your operating system
to determine how you can install these language files.
What Is Ajax?
Adding Reporting Capabilities to Java Applications
We
can easily add reporting capabilities to Java applications by taking advantage
of the classes included in the JasperReports class library. JasperReports can
be easily embedded into both client and server-side Java applications, simply
by adding the required libraries to our CLASSPATH and calling the appropriate
methods in the JasperReports API.
Setting Up Our
Environment
The
JasperReports project folder we have, contains the following files and
directories:
To get up and running quickly, the files to be added
to the CLASSPATH are jasperreports-2.0.3.jar which can be found under the dist
subdirectory, and all the JAR files under the lib subdirectory.
Jasper Reports overview
what is
JasperReports ?
JasperReports
is one of the most popular (if not the most popular) Java reporting tool
available.
It is an open-source Java class library designed to aid
developers with the task of adding reporting capabilities to Java applications.
Features of
JasperReports :
·
JasperReports is capable of generating
reports including textual data, images, charts, and graphs.
·
It has flexible report layout.
JasperReports allows us to
separate data into optional report sections, like:
The report title, which will appear
once at the top of the report.
A page header, which will appear at
the top of every page.
A detail section, which typically
contains the primary report data.
A page footer, which will appear at
the bottom of every page.
A summary section, which will appear
at the end of the report.
- It has the ability to group report data.
This grouping allows us to display the
data in a logical, easy-to-follow way.
·
It is capable of presenting data
textually or graphically (statically or dynamically via report expressions).
·
It allows developers to supply data in
multiple ways ( parameters and/or datasources).
Note: report
parameters can be instances of any java class.
·
It can accept data from multiple
datasources (relational db, XML files, java data objects, any class
implementing the java.util.Map interface, or any class implementing the javax.swing.TableModel
interface).
·
It can generate watermarks.
·
It can generate subreports (reports
within master report).
·
It is capable of exporting reports to a
variety of formats (pdf, xls, rtf, html, xml, csv, and plain text).
Class Library
Dependencies:
JasperReports
leverages other open-source Java libraries to implement some of their
functionality. Some of the libraries JasperReports builds on include:
iText, JFreeChart,
Jakarta POI, JAXP, and Jakarta
Commons.
Typical Workflow:
The flow
chart overleaf illustrates the typical workflow while creating reports with
JasperReports:
1.
Create JRXML report template file
XML
report templates can be hand-coded or generated by a graphical report designer
like iReport which is the official JasperReports visual design
tool.
JasperReports
XML templates are commonly referred to as JRXML files and has the extension of
a .jrxml.
Here
is what a typical JRXML file looks like.(empty report template )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report
Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="simple_template">
<title>
<band height="50">
</band></title>
<pageHeader>
<band height="50">
</band>
</pageHeader>
<columnHeader>
<band height="30">
</band>
</columnHeader>
<detail>
<band height="100">
</band>
</detail>
<columnFooter>
<band height="30">
</band>
</columnFooter>
<pageFooter>
<band height="50">
</band>
</pageFooter>
<lastPageFooter>
<band height="50">
</band>
</lastPageFooter>
<summary>
<band height="50">
</band>
</summary>
</jasperReport>
The JRXML file here
mostly illustrates the main elements of a JRXML file. All elements in the file
are optional except for the root <jasperReport> element.
Each main element of
the JRXML file contains a <band> element as its only child element. Bands
contain the data that is displayed in the report.
Bands can contain
child elements that are used to position, format, and display the actual report
data, both textual and graphical.
2.
Compile JRXML into Jasper template
JRXML files are
compiled into a JasperReports native binary template, either programmatically
by calling the appropriate methods on the JasperReports class library
(compileReportToFile()), or by using a custom ANT task
The resulting
compiled template is commonly known as the Jasper file, and is typically saved
to disk with a .jasper extension.
3.
Fill the Jasper template to generate the report
The Jasper file is
then used to generate the final report, by providing it with its required data.
This process is known as filling the report.
A JRXML file has to
be compiled only once. The generated Jasper file can be filled as many times as
necessary to create and display reports.
Filled reports can be
saved to disk in a JasperReports native format. Reports saved in this format
are known as JasperPrint files. JasperPrint file names have a .jrprint extension.
JasperPrint files can only be viewed with a JasperReports-specific viewer.
JasperPrint files can be exported to other formats so that they can be opened
with commonly available tools like PDF viewers and word processors.
Subscribe to:
Posts (Atom)