Monday 1 August 2011

Java Technologies


Externalizable
Externalizable is an Interface that covers Serializable Interface. It sends data into Streams in Compressed Format. It has two methods, that is: writeExternal (ObjectOuput out) and readExternal (ObjectInput in).
Difference between Path and Classpath 
Path and Classpath are operating system level environment variables. Path is used define where the system can find the executables (.exe) files and Classpath is used to specify the location .class files.
public interface xyz{
static int i=0;
void ss();

class abc{
abc(){
int j;
System.out.println("In side the interface");
};

public static void main(String abc[]){
System.out.println("Interface is in");
}
}
}
Some alternatives to inheritance
Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often better than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is more difficult to re-use (because it is not a subclass). 



------------------------------------------------------------------------------------------------------------------------------


Difference response.sendRedirect(url) 
The element forwards the request object holding the client request information from one JSP file to another file. sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. 

Difference between custom JSP tags and beans 
Custom JSP tag is a tag you defined. To use custom JSP tags, you need to define three things:
1. The tag handler class that specifies the tag\'s behavior 
2. The tag library descriptor file that maps the XML element names 
3. the JSP file that uses the tag library 
JavaBeans are Java utility classes you defined.
to declare a bean and use
to set value of the bean class and use
to get value of the bean class.
What are comments in JSP?
1: <%-- JSP Comment --%>
2: <% // Java comments %>
3: <% /* Java comments*/ %>



Implicit objects 
Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
* request
* response
* pageContext
* session
* application
* out
* config
* page
* exception 

JSP scripting elements
There are three scripting language elements:
*declarations
*scriptlets
*expressions 

JMS
It provide for two messaging models, 
1:publish-and-subscribe and 
2:point-to-point queuing.

Retrieving Warnings
A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object.

Including static files within a JSP page
Static resources should always be admitted using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.

Scriptlet 
A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can 
1.Declare variables or methods 
2.Write expressions valid in the page scripting language 
3.Use any of the JSP implicit objects 

Difference between forward and sendRedirect
When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. 

life-cycle mehtods in JSP 
The JspPage interface declares only two mehtods - jspInit() and jspDestroy() that must be implemented by all JSP pages regardless of the client-server protocol. However the JSP specification has provided the HttpJspPage interfaec specifically for the JSp pages serving HTTP requests. This interface declares one method _jspService(). The jspInit()- The container calls the jspInit() to initialize the servlet instance.It is called before any other method, and is called only once for a servlet instance. The jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects. The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance.


---------------------------------------------------------------------------------------------------------------------------------



JDBC establish connection

To establish a connection you need to have the appropriate driver connect to the DBMS.
The following line of code we use:
String url = “jdbc:odbc:Fred”;
Connection con = DriverManager.getConnection(url, “Fernanda”, “J8?); 

Difference between local and global transaction
A transaction is atomic unit of Work.Transactions can be divided into two categories.
1.Local Transactions: These transactions are confined to the objects which reside inside one particular JVM.
2.Global Transactions:These transactions may encapsulate objects which are distributed on various JVM's.

Technique to call a Stored Procedure from JDBC 
The first step is to create a CallableStatement object. As with as a Statement an and PreparedStatement objects, this is done with an open Connection object.
E.g.CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();

Difference between Rowset and Resultset
RowSets are a part of JDBC 2.0 API. Essentially a row set is a JavaBean that contains database data. The implementation is store the data offline, or it can simply wrap a connection to make a result set look like a JavaBean. You could even use a row set to access data communicating over HTTP to a servlet, which provides the data for the bean.

Inserting images into a Mysql database    
This code is snip shows the basics:
File file = new File(fPICTURE);
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps =
ConrsIn.prepareStatement("insert into dbPICTURE values (?,?)");
ps.setString(1,file.getName());
ps.setBinaryStream(2,fis,(int)file.length());
ps.close();
fis.close();


Advantage of using a PreparedStatement 
For SQL statements that are executed repeatedly, using a PreparedStatement object would almost always be s faster than using a Statement object. This is because creating a PreparedStatement object by explicitly giving the SQL statement causes the statement to be precompiled within the database immediately. 
Typically, PreparedStatement objects are used for the SQL statements that take parameters. 

JDBC Working
The Java Database Connectivity (JDBC) is used to whenever a Java application should communicate with a relational database for which a JDBC driver exists.
Main JDBC classes:
• DriverManager. Manages a list of database drivers. 
• Driver. The database communications link, handling all communication with the database. 
• Connection. Interface with all the methods for contacting a database
• Statement. Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
• ResultSet. The answer/result from a statement. A ResultSet is a fancy 2D list which encapsulates all outgoing results from a given SQL query. 


--------------------------------------------------------------------------------------------------------------------------





Servlet

Lifecycle methods of Servlet

The interface javax.servlet.Servlet, defines the three life-cycle methods. These are:
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
The container manages the lifecycle of the Servlet. When a new request come to a Servlet, the container performs the following steps.
1. If an instance of the servlet does not exist, the web container
* Loads the servlet class.
* Creates an instance of the servlet class.
* Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
2. The container invokes the service method, passing request and response objects.
3. To remove the servlet, container finalizes the servlet by calling the servlet's destroy method.

ServletContext
ServletContext is an Interface that defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed.