AJP Notes - aarya classes

advertisement
Q. Explain the following methods of ServletRequest interface.
1. getContentType()
Returns the type of the request. A null value is returned if the type cannot be determined.
Syntax:
String getContentType( )
2. getParametersNames()
Returns an enumeration of the parameter names for this request.
Syntax:
Enumeration getParameterNames( )
3. getReader()
Returns a buffered reader that can be used to read text from the request. An
llegalStateException is thrown if getInputStream( ) has already been invoked for this request.
Syntax:
BufferedReader getReader( ) throws IOException
4. getParameter()
Returns the value of the parameter named pname.
Syntax:
String getParameter(String pname)
5. getProtocol()
Returns a description of the protocol.
Syntax:
String getProtocol( )
Q. What is the use of GenericServlet? What is a difference between GenericServlet and
HttpServlet?
Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web,
extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be
directly extended by a servlet, although it's more common to extend a protocolspecific subclass such as HttpServlet.
GenericServlet makes writing servlets easier. It provides simple versions of
the lifecycle methods init and destroy and of the methods in
the ServletConfig interface. GenericServlet also implements the log method, declared in
the ServletContext interface.
To write a generic servlet, you need only override the abstract service method.
Differences :
1. The main difference is that, HttpServlet is a protocol
dependent whereas GenericServlet is protocol independent. So GenericServlet can handle all
types of protocols, but HttpServlet handle only HTTP specific protocols.
2. GenericServlet belongs to javax.servlet package. HttpServlet belongs
to javax.servlet.http package.
3. GenericServlet is an abstract class which extends Object and implements Servlet,
ServletConfig and java.io.Serializable interfaces. HttpServlet is an abstract class which
extends GenericServlet and implements java.io.Serializable interface.
4. GenericServlet is an abstract class that defines a generic, protocol-independent Servlet. Any
protocol dependent Servlet has to extend this class in order to provide a specific implementation
and override service() method. One such an example class is HTTPServlet class, which
extends GenericServlet class.
5. GenericServlet does not
contain doGet() and doPost() methods. HttpServlet extends GenericServlet and adds support
for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(),
doTrace() methods
Q. What is a Swing? Write use of JApplet class.
Swing:
Swing is a set of classes that provides more powerful and flexible components than are possible
with the AWT.
In addition to the familiar components, such as buttons, check boxes, and labels, Swing supplies
several exciting additions, including tabbed panes, scroll panes, trees, and tables. Even familiar
components such as buttons have more capabilities in Swing. For example, a button may have
both an image and a text string associated with it. Also, the image can be changed as the state of
the button changes.
Unlike AWT components, Swing components are not implemented by platform-specific code.
Instead, they are written entirely in Java and, therefore, are platform-independent.
JAppet:
Fundamental to Swing is the JApplet class, which extends Applet. Applets that use Swing must
be subclasses of JApplet. JApplet is rich with functionality that is not found in Applet. For
example, JApplet supports various “panes,” such as the content pane, the glass pane, and the
root pane.
When adding a component to an instance of JApplet, do not invoke the add( ) method of the
applet. Instead, call add( ) for the content pane of the JApplet object.
Container getContentPane( )
The add( ) method of Container can be used to add a component to a content pane.
void add(comp)
comp is the component to be added to the content pane.
Q. Write the sequential steps for establishing a connection between client socket and server
socket using TCP protocol.
Sockets provide the communication mechanism between two computers using TCP. A client
program creates a socket on its end of the communication and attempts to connect that socket to
a server.
When the connection is made, the server creates a socket object on its end of the communication.
The client and server can now communicate by writing to and reading from the socket.
The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a
mechanism for the server program to listen for clients and establish connections with them.
The following steps occur when establishing a TCP connection between two computers using
sockets:

The server instantiates a ServerSocket object, denoting which port number communication is to
occur on.

The server invokes the accept() method of the ServerSocket class. This method waits until a
client connects to the server on the given port.

After the server is waiting, a client instantiates a Socket object, specifying the server name and
port number to connect to.

The constructor of the Socket class attempts to connect the client to the specified server and port
number. If communication is established, the client now has a Socket object capable of
communicating with the server.

On the server side, the accept() method returns a reference to a new socket on the server that is
connected to the client's socket.
After the connections are established, communication can occur using I/O streams. Each socket
has both an OutputStream and an InputStream. The client's OutputStream is connected to the
server's InputStream, and the client's InputStream is connected to the server's OutputStream.
TCP is a twoway communication protocol, so data can be sent across both streams at the same
time. There are following usefull classes providing complete set of methods to implement
sockets.
Q. What is a frame? Give example of frame.
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of
Window and has a title bar, menu bar, borders, and resizing corners. If you create a
Frame object from within an applet, it will contain a warning message, such as “Java Applet
Window,” to the user that an applet window has been created. This message warns users that the
window they see was started by an applet and not by software running on their computer.
When a Frame window is created by a program rather than an applet, a normal window is
created.
Frame’s constructors:
Frame( )
Frame(String title)
The first form creates a standard window that does not contain a title. The second form
creates a window with the title specified by title.
Example:
Refer Notebook
Q. Explain the following terms.
Component
At the top of the AWT hierarchy is the Component class. Component is an abstract class that
encapsulates all of the attributes of a visual component. All user interface elements that are
displayed on the screen and that interact with the user are subclasses of Component. It defines
over a hundred public methods that are responsible for managing events, such as mouse and
keyboard input, positioning and sizing the window, and repainting. A Component object is
responsible for remembering the current foreground and background colors and the currently
selected text font.
Container
The Container class is a subclass of Component. It has additional methods that allow other
Component objects to be nested within it. Other Container objects can be stored inside of a
Container (since they are themselves instances of Component). This makes for a multileveled
containment system. A container is responsible for laying out (that is, positioning) any
components that it contains. It does this through the use of various layout managers
Panel
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it simply
implements Container. A Panel may be thought of as a recursively nestable, concrete screen
component. Panel is the superclass for Applet. When screen output is directed to an applet, it is
drawn on the surface of a Panel object. In essence, a Panel is a window that does not contain a
title bar, menu bar, or border. When you run an applet using an applet viewer, the applet viewer
provides the title and border. Other components can be added to a Panel object by its add( )
method (inherited from Container). Once these components have been added, you can position
and resize them manually using the setLocation( ), setSize( ), or setBounds( ) methods defined
by Component.
Window
The Window class creates a top-level window. A top-level window is not contained within any
other object; it sits directly on the desktop. Generally, you won’t create Window objects directly.
Instead, you will use a subclass of Window called Frame
Frame
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window
and has a title bar, menu bar, borders, and resizing corners. If you create a Frame object from
within an applet, it will contain a warning message, such as “Java Applet Window,” to the user
that an applet window has been created. This message warns users that the window they see was
started by an applet and not by software running on their computer. When a Frame window is
created by a program rather than an applet, a normal window is created.
Download