Last. Chapter
Last. Chapter
Contents
• Introduction
• Types Of Packages
– System Packages or Java API
• Java System Packages & Their Classes
– User Defined Packages.
• Creating User Defined Packages
• Steps For Creating Package
• Accessing a Package
o Advantages Of Packages
Introduction
In java, programmers can create several classes &
Interface. After creating these classes and interface,
it is better if they are divided into some groups
depending on their relationship. Thus, the classes
and interface which handle similar or same task are
put into the same directory or folder, which is also
known as package.
We must first declare the name of the package using the package
keyword followed by the package name. This must be the first statement
in a Java source file. Then define a classes as normally as define a class.
Example :
package myPackage;
public class class1
{
- - - - - - - - - - - - -
// Body of class1
}
In the above example, myPackage is the name of the package. The class
class1 is now considered as a part of this package.
This listing would be saved as a file called class1.java & located in a directory
named mypackage. When the source file is compiled, java will create a .class
file & store it in the same directory.
The .class files must be located in a directory that has the same name as the
package & this directory should be a subdirectory of the directory where
classes that will import the package are located.
Steps For Creating Package :
To create a user defined package the following steps
should be involved :-
1: Declare the package at the beginning of a file using
the syntax :-
package packageName;
2: Define the class that is to be put in the package &
declare it public.
3: Create a subdirectory under the directory where the
main source files are stored.
4: Store the listing as the classname.java file in the
subdirectory created.
5: Compile the file. This create .class file in the
subdirectory.
Java also supports the concept of package
hierarchy. This is done by specifying multiple names
in a package statement, seprated by dots (.).
Ex :- package firstPackage.secondPackage;
This approach allows us to group related classes
into a package and their group related package into
a larger package. Store this package in a
subdirectory named firstpackage/secondPackage.
A java package file can have more than one class
definition. In such cases, only one of the classes
may be declared public & that class name with .java
extension is the source file name. When a source
file with more than one class definition is compiled,
java creates independent .class files for those
classes.
Accessing a Package
Java package can be accessed either using a fully qualified class name
or using a shortcut approach through the import statement.
Syntax :
import package1[.package2][.package3].classname;
Here, package1 is the name of the top level package, package2 is the
name of the package that is inside the package & so on. We can have
any number of packages in a package hierarchy. Finally the explicit
classname is specified. The import statement must end with a
semicolon (;). The import startment should appear before any class
definitions in a source file. Multiple import statements are allowed.
Ex :
import firstpackage.secondPackage.Myclass;
or
import firstpackage.*;
Advantages Of Packages
There are several advantages of package some of them are as
follow :-
1: Packages are useful to arrange related classes and interface
into a group.This makes all the classes & interface
performing the same task to put together in the same
package.
2: Packages hide the classes & interfaces in a seprate
subdirectory, so that accidental deletion of classes &
interfaces will not take place.
3: The classes & interfaces of a packages are isolated form the
classes & interfaces of another packages. This means that
we can use same names for classes of two different classes.
4: A group of packages is called a library. The classes &
interface of a package are like books in a library & can be
reused several times. This reusability nature of packages
makes programming easy.
Web Application Deployment
Help from Java
• Session tracking is provided by Java servlet
API.
– HttpSession object
• Returned by getSession method of request.
– Can store info in session object as named
attributes.
• setAttribute, getAttribute
– Other methods to configure session timeout, get
information about session, etc.
– Uses cookies.
Java Servlet API
• Documentation on Java Servlet API
• A good book.
– Java Servlet Programming by Jason Hunter,
published by O’Reilly
• Unfortunately, pretty expensive.
Web Application Deployment
• Typically a web application is comprised of:
– HTML pages
– Data
– Images
– Servlets
– Source code
– Libraries
– Other resources
• A servlet is a component of a web application.
Separating server from web app.
• Person running the web server and person
writing a web application running on that
web server may not be the same.
– Example: a web hosting company runs a web
server which “hosts” the sites of several other
companies for a fee.
– What could be problematic about this?
• Need to isolate customers.
• Web app developer shouldn’t need to know anything
about web server’s installation.
Standardizing deployment
• Server products (like Tomcat) have support
for setting up resources and isolating one
web application from another.
• Old days: different servers each did things
their own way.
• Now: there is a standard (Servlet 2.4) that
defines exactly how a web application is
organized and how a server maps requests
to it.
– Note: our version of Tomcat implements 2.3
Mapping to a web app.
• The server is responsible for mapping
URL’s that start with a specific prefix to the
location of a web application.
– This is the “root” of the web application.
• In Tomcat, this is done with a configuration
file.
• We have defined a single web application
that maps to each of your class directories.
Web app structure
• /WEB-INF/classes
– Class files for servlets
– Directory structure needs to reflect package
names.
• /WEB-INF/lib
– Libraries (jar) files that may be needed by
servlets
Common code
• Many web applications may need access to
common libraries and classes.
– Servlet API
– XML processing libraries
– Email processing libraries
• These are put in the server’s root
– /common/classes
– /common/lib
COMP 118 Setup
• Server: wwwj.cs.unc.edu:8080
• To compile correctly, be sure to:
setenv JAVA_HOME /usr/java1.2
setenv CLASSPATH /opt/jakarta-4.1.18/common/lib/servlet.jar:.
web.xml
• This file contains important configuration
information needed to make servlets work.
• For a servlet to function properly, you need
to add a “servlet definition” and a “servlet
mapping” for each servlet.
– /servlet/class_name used to be a shortcut
• Supposed to work, but doesn’t seem to in the new
version.
• The order of things in web.xml is important.
– We will put in a template which you can
modify.
Defining Servlets
<servlet>
<servlet-name>Name</servlet-name>
<servlet-class>servlet.class</servlet-class>
</servlet>
Servlet Mapping
• This determines what URL’s get mapped to
the servlet.
<servlet-mapping>
<servlet-name>Name</servlet-name>
<url-pattern>/path</url-pattern>
</servlet-mapping>
Revisiting previous examples.
Administrivia
• Examples from class will be put up on the
web site.
• First programming assignment will be put
up on the web site by the weekend.
• Make a home page (index.html) and put it at
the top of your class directory.
– Put a picture and your name on it.
• Write and deploy a “hello world” servlet to
test your environment.
Initialization Parameters
• Can use web.xml to provide initialization
parameters to a web application.
– Within servlet definition:
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
• Example reading book info from file.
Debugging
• Our server is set up to log error messages to
a file in WEB-INF/logs
Multithread Issues
• There is one instance of each servlet deployed
which handles every request to that servlet.
• But, each request is handled in its own thread.
– When will this cause problems?
• Problematic if the servlet updates non-local variables
(i.e., persistent state).
– What is the solution?
• Use Java’s synchronization mechanisms
• Example using forms to add new books.
Redirection and Errors
• Servlet can redirect request
– sendRedirect()
• Servlet can generate an error page.
– sendError()
– Status codes in documentation for
HttpServletResponse
Generating Your Own Error Pages
• Server puts together error page to report
errors to users.
• You can configure your web application in
order to generate your own error pages.
<error-page>
<error-code> 400 </error-code>
<location> /400.html </location>
</error-page>