Tuesday, November 29, 2011

Difference between CSS Class and ID selector


The first difference is
An ID selector can be called only once in a document.
A class selector can be called multiple times in a document.

The second difference is
ID can be called by Javascript's getElementByID function.

Note1: There is no hard rule on when to use ID and when to use Class. Use class as much as possible for maximum flexibility, with the only exception being when you want to use Javascript's getElementByID function, in which case you need use ID.

Note2: Class and ID names are both case sensitive. For example, .divclass and .DivClass are two different classes.


Monday, November 28, 2011

How to Achieve Double Border HTML Button Using CSS Pseudo-classes


We can Achieve Double Borders HTML Buttons Using CSS Pseudo-classes

That is without using the border-style:double;(As we know these are not easy to control)

Double border means we can have one outer border and one inner border, this can be achieved by using the simple html and applying custom styles.

Here is an example for the same...



<HTML>
<HEAD>
<STYLE>
.spancls
{
border-top: solid 3px #A9B7C6;
border-left: solid 3px #A9B7C6;
border-bottom: solid 3px #657f97;
border-right: solid 3px #657f97;
padding:2px;
background: #EAF5FC;
width:50px;

}
.btncls
{
border:1px solid #EAF5FC;
background:#EAF5FC;
width:50px;
}

.spancls:hover
{
border-top: solid 3px #FFC727;
border-left: solid 3px #FFC727;
border-bottom: solid 3px #D39300;
border-right: solid 3px #D39300;
padding:2px;
background:#FEFAE7;
width:50px;

}
.spancls:active
{
border-top: solid 3px #D39300;
border-left: solid 3px #D39300;
border-bottom: solid 3px #FFC727;
border-right: solid 3px #FFC727;
padding:2px;
background:#FEFAE7;
width:50px;

}

.btncls:hover
{
border:1px solid #FFF1D3 ;
background:#FFF1D3;
width:50px;
}

.btncls:active
{
border:1px solid #FFFFFF ;
background:#FFFFFF;
width:50px;
}
</STYLE>
</HEAD>

<BODY>
<span class="spancls" >
<input type="button" value="hello" class="btncls">
</span>
</BODY>
</HTML>





Here to achieve the double border, we have used a span and added the button inside that span, as we have a 2px padding, it is looking like one more border(internal border) for the button.

And the different states of the button will look like as follows


Plain state of the Button :





Hover(mouse over) state of the Button:





Active(mouse click) state of the Button:



Saturday, November 26, 2011

How To Embed A Streaming Video Files To The Web Page(HTML)


There are couple of ways to embed video file on web page.

1.Use a hyper link (link to video file).
2.Embed the video file within the web page using special HTML code.

Hyperlink

A hyperlink is a direct link to a file (such as webpage, video, or image) using an HTML anchor tag.

In this case, the hyperlink is a link to hosted video file. This will allow the video file to be opened and played with the viewer's default video player. In other words, when the end user clicks the link and if it is linked to a Windows Media File, the Windows Media Player will open the video file and play it.

The HTML code to link to the video is as follows..

<a href="streaming-video-filename">Click here to watch the video</a>


Even though this does provide the end user the ability their preference of players, it provides the least amount of control over how others will view the video and it will not appear to them to be a part of the web page.

Embedded streaming video Files

Embedding a streaming video file is simply putting the file within the web page using HTML code so that it will be displayed within the webpage(not another program).

This will provide the best way to control the way that we want the video clip to be viewed.

The HTML code that we need will depend on the type of video file format that we use.
For example, here is the HTML tags to embed a Windows Media file:


<OBJECT ID="MediaPlayer" WIDTH=320 HEIGHT=331 CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
STANDBY="Loading Windows Media Player components" TYPE="application/x-oleobject">

<PARAM NAME="FileName"
VALUE="streaming-video-filename">

<PARAM name="ShowControls" VALUE="true">

<param name="ShowStatusBar" value="true">

<PARAM name="ShowDisplay" VALUE="true">

<PARAM name="autostart" VALUE="false">

<EMBED TYPE="application/x-mplayer2" SRC="streaming-video-filename" NAME="MediaPlayer"
WIDTH=320 HEIGHT=331 ShowControls="1" ShowStatusBar="1" ShowDisplay="1" autostart="0"> </EMBED>

</OBJECT>



The <object> and the <embed> are used to reach the greatest browser compatibility.

The <object> tag is used to include objects such as images, audio, videos, Java applets, ActiveX, PDF, and Flash.

The object element was intended to replace the img and applet elements. However, because of bugs and a lack of browser support this has not happened.

The object support in browsers depend on the object type. Unfortunately, the major browsers use different codes to load the same object type.

Luckily, the object element provides a solution. If the object element is not displayed, the code between the <object> and </object> tags will be executed. This way we can have several nested object elements (one for each browser).

The <object> has the support for Internet Explorer.

The "CLASSID" attribute is used to set the class ID value as set in the Windows Registry(for windows media player).

The "STANDBY" attribute is used to set the text to display while the object is loading.

The <embed> tag is for Mozilla Firefox and other compatible browsers.

The both tags are pretty much a duplicate of each other. So, remember, for each tag, we'll have to set the file name and any other parameters.


Friday, November 25, 2011

Simple example for css class and id selector


While defining the css block, we can use either the class name or id as the selector.

example:

selector {
property1:value1;
property2:value2;
}

Using the class name as the selector:

.[Class Name] {
property1:value1;
}

That is..

.nav {
color:#0220FF;
}

can be used in html as follows

<div class="nav">using a class selector.</div>

We can specify different instances of a class selector, by using the following syntax:

[tag type].[class] {
property:value;
}

example:

div.clr {
color:#02400FF;
}

span.clr {
color:#1F0E00;
}

can be used in html as follows

<div class="clr">div one</div>
<span class="clr">span one..</span>

We can use multiple selectors for an html component


.fntsize{
font-size:22px;
}

.fntcolor{
color:#FF4710;
}
can be used as follows..
<p class="fntsize fntcolor">multiple classes.</p>

Using ID as selector :

#[ID Name] {
property:value;
}

#test {
color:#FF00FF;
}

<p id="test">using ID as selector.</p>


Thursday, November 24, 2011

Linking (including) an external CSS (style sheet) to a HTML page


An external style sheet can be linked to the page with a <link> tag.

Example link tag which includes an external style sheet is as follows..


<link rel="stylesheet" type="text/css" href="xyz.css" />


The advantage of doing this is that the same style sheet can be used in every page on your site. And the entire site's styling can be managed by changing just this one file.

When the CSS is defined inside the html page, the css classes can be declared with in the <style> tags, but we do not put <style> tags inside an external style sheet.


What is CSS? Where is CSS Used? and an example HTML that uses CSS


CSS is nothing but Cascading Style Sheets, these are used to separate the content from design of the HTML page, so that in future if we want to change the design and layout of the HTML page, without disturbing the content, it will become any easy job. That is we can change the layout / design by simply modifying the CSS.

The CSS can be declared with in the html file or in a separate CSS file.

The structure of the simple CSS is as follows,


.leftcol
{
float: left;
width: 33%;
background:#908800;
}
.middlecol
{
float: left;
width: 34%;
background:#effefe;
}
.rightcol
{
float:left;
width: 33%;
background:#dddd11;
}


In the above example the "leftcol","middlecol","rightcol" are the css class names, which defines its own styling, these classes need to be applied on the HTML elements to see these styling effects.

simple html that uses the above declared css class


<html>

<head>

<style type=​"text/​css">
.leftcol
{
float: left;
width: 33%;
background:#908800;
}
.middlecol
{
float: left;
width: 34%;
background:#effefe;
}
.rightcol
{
float:left;
width: 33%;
background:#dddd11;
}
</style>

</head>

<body>

<div class="leftcol">this is a left div ...</div>
<div class="middlecol">this is a middle div ...</div>
<div class="rightcol">this is a right div ...</div>

</body>

</html>


What is the difference between Dojo and jQuery


jQuery is a JavaScript Library that simplifies HTML document traversing(DOM manipulation), event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

Dojo is a more comprehensive toolkit that offers the above features, but also includes many other pieces that facilitate building rich web applications


Tuesday, November 22, 2011

What is HTTP multipart request?


A HTTP multipart request is a type of HTTP request that a HTTP client construct to send file and data to the server.


Sunday, November 20, 2011

Synchronizing a HashMap


How to make hashmap operations synchronized?

For JDK 5 or after, we can use ConcurrentHashMap . This is an efficient implementation of a concurrent hash map using different locks for different portions of the HashMap . This reduces the contention of the lock and hence increase throughput tremendously as compared to a synchronized Map.

For JDK versions before JDK5 there is a back port of the same, if we do not want to use that, then we can use Collections.synchronizedMap() method to create a synchronized map. The new map will be backed with the original map and all the access will be synchronized on a single monitor. Also, we have to do external synchronization while iterating over the map entries.


Simple SAX parser example (count number of elements in an xml file)


Example Xml File :



<?xml version = "1.0" ?>

<Employees>



<Employee>

<Emp_Id> 1 </Emp_Id>

<Emp_Name> Bob </Emp_Name>

<Emp_E-mail> bob@hotmail.com </Emp_E-mail>

</Employee>



<Employee>

<Emp_Id> 2 </Emp_Id>

<Emp_Name> Nick </Emp_Name>

<Emp_E-mail> nick@yahoo.com </Emp_E-mail>

</Employee>



<Employee>

<Emp_Id> 3 </Emp_Id>

<Emp_Name> John </Emp_Name>

<Emp_E-mail> john@gmail.com </Emp_E-mail>

</Employee>



</Employees>

Java code to parse the above xml file and provides the elements count

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class ElementCount{
int startTag = 0;
public static String ele;
public static void main(String args[])throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name:");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if (file.exists()){
System.out.print("Enter tag name for which the count should be calculated:");
ele = bf.readLine();
ElementCount tagCount = new ElementCount(xmlFile);
}
else{
System.out.println("File not found!");
}
}
public ElementCount(String str){
try{
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
DefaultHandler dHandler = new DefaultHandler(){
public void startElement(String uri, String name, String element,
Attributes atri)throws SAXException{
if (element.equals(ele)){
startTag++;
}
}
public void endDocument(){
System.out.println("Total elements: " + startTag);
}
};
parser.parse(str,dHandler);
}
catch (Exception e){
e.printStackTrace();
}
}
}


What is a constructor and difference between this and super


A constructor is a special type of function that gets called every time you initialize a class, constructor has the same name as class name. Constructor has no return type not even void. We can pass the parameters to the constructor.

-this() is used to invoke a constructor of the same class.

-super() is used to invoke a super class constructor.

Constructor is called immediately after the object is created before the new operator completes.

For example, say you have this class called Person:
class Person
{
public Person ()
{
Console.WriteLine("We're making a new person");
}
}

Now you can create a new object of type Person, like this:
Person Tim= new person();

The program will write the line "We're making a new person" to the screen.

  • Constructor can use the access modifiers public, protected, private or have no access modifier
  • Constructor can not use the modifiers abstract, static, final, native, synchronized or strictfp
  • Constructor can be overloaded, we cannot override.
  • You cannot use this() and Super() in the same constructor.
  • You can not have both super() and this() invocation in the same constructor.
  • Either super() or this() should be the first statement in the constructor.


What is the difference between SAX and DOM parsers?


DOM Parser:

  1. A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.
  2. A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.
  3. A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.

SAX Parser:

  1. A SAX (Simple API for XML) parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events, and tells the client what it reads as it reads through the input document.
  2. A SAX parser serves the client application always only with pieces of the document at any given time.
  3. A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What’s more, it runs faster and is easier to learn than DOM parser because its API is really simple. But from the functionality point of view, it provides a fewer functions, which means that the users themselves have to take care of more, such as creating their own data structures.


What are the differences between finalize(), final and finally


  • finalize() – method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.
  • finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. It allows the programmer to avoid having clean up code accidentally bypassed by a return, continue, or break. Keeping the clean up code in a finally block is always a good practice, even when no exceptions are anticipated.
  • final – This is useful in several ways...
When it is used with variable declaration, it becomes constant
When it is used with method, the method cant be overridden by the child class
When it is used with the class, the class cant have child classes.


What are the differences between HashMap and Hashtable?


Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.


Wednesday, November 16, 2011

What is Cross-site Scripting (XSS)


Cross-Site Scripting attacks are a type of injection problem, in which malicious scripts are injected into the trusted web sites. Cross-site scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user in the output it generates without validating or encoding it.

An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site. These scripts can even rewrite the content of the HTML page.


Tuesday, November 15, 2011

Java Faq


differentiate between Java Bean and Enterprise Java Bean?

Java Bean as is a plain java class with member variables and getter setter methods. Java Beans are defined under JavaBeans specification as Java-Based software component model. which lets in the features like introspection, customization, events, properties and persistence.

What is binding (JSF) ?

Wiring UI components to back-end data sources such as backing bean properties.

What is J2EE?

J2EE is a Java 2 Enterprise Edition. J2EE is an environment for developing and deploying enterprise applications. J2EE specification is defined by Sun Microsystems Inc. The J2EE platform is lies of a set of services, application programming interfaces (APIs), and protocols, which provides the functionality necessary for developing multi-tiered, web-based applications. The J2EE platform is one of the richest platform for the development and deployment of enterprise applications.

What do you understand by a J2EE module?

A J2EE module is a software building block that consists of one or more than J2EE components of the same container type along with one deployment descriptor of that type. J2EE specification specifies four characters of modules: a) Web module b) EJB module c) resource adapter d) application client and Modules can also be assembled into J2EE applications.

What is J2EE component?

J2EE component is a collected functional software unit supported by a container and configurable at deployment time. The J2EE specification shows the following J2EE component part: Application clients and applets that run on the client machine. Java servlet and JavaServer Pages (JSP) technology are Web components that run on the server machine. Enterprise JavaBeans (EJB) (enterprise beans) are business components that run on the server.

What is bean-managed transaction ?

Transaction whose boundaries are defined by an enterprise bean.

What is binding (XML) ?

Generating the code needed to process a well-defined portion of XML data.

What is build file ?

The XML file that contains one or more asant targets.

What is business logic ?

The code that implements the functionality of an application.

What is business method ?

A method of an enterprise bean that implements the business logic or rules of an application.

What is callback methods ?

Component methods called by the container to notify the component of important events in its life cycle.

What is caller principal ?

It is principal that identifies the invoker of the enterprise bean method.

What is caller ?

Same as caller principal.

What are the components of web module?

There are the following modules: a) Java classes b) JSP files c) gif and html files and d) web component deployment descriptors

Differentiate .ear, .jar and .war files?

These files are created for different uses .These files are only zipped file using java jar tool. Here is the description of these files: .ear files: The .ear file comprises the EJB modules of the application. .jar files: are with the .jar extension. The .jar files comprises the libraries, resources and accessories files like property files. .war files: are with the .war extension. The war file comprises the web application that can be deployed on the any servlet/jsp container. The .war file contains jsp, html, JavaScript and other files for necessary for the development of web applications.

Differentiate between Session Bean and Entity Bean?

Session Bean: Session is one of the EJB and it shows a single client inside the Application Server. Stateless session bean is easy to develop and its efficient. As compare to entity beans session beans needs few server resources. Entity Bean: An entity bean represents persistent global data from the database. Entity beans data are stored into database.

Why J2EE is suitable for the development of distributed multi-tiered enterprise applications?

J2EE application program allows the developers to design and implement the business logic into components according to business requirement. J2EE architecture allows the development of multi-tired applications and the developed applications can be installed on different machines depending on the tier in the multi-tiered J2EE environment . The J2EE application parts are: a) Web-tier components. b) Client-tier components . c) Business-tier components . d) Enterprise information system (EIS)-tier software.

what is a container?

containers are the interface between a component and the low-level platform specific functionality that supports the component.
What services provided by a container?

The services provided by container are as follows: a) Transaction management for the bean b) Instance pooling for the bean c) Persistence d) Remote access e) Lifecycle management f) Database-connection pooling g) Security

What are types of J2EE clients?

a) Java-Web Start b) clients Applets c) Web applications d) Wireless clients

What is Deployment Descriptor?

A deployment descriptor is simply an XML(Extensible Markup Language) file with the extension of .xml.. Application servers reads the deployment descriptor to deploy the components contained in the deployment unit. As in: ejb-jar.xml file is used to describe the setting of the EJB.

What is JTA and JTS?

JTA stands for Java Transaction API and JTS stands for Java Transaction Service. JTA provides a standard interface which allows the developers to demarcate transactions . JTA is a high level transaction interface used by the application code to control the transaction.

What is JAXP?

The Java API for XML Processing (JAXP) enables applications to parse and transform XML documents independent of a particular XML processing implementation.

What is J2EE Connector architecture?

J2EE Connector Architecture (JCA) is a Java-based technology solution for connecting application servers and enterprise information systems (EIS) as part of enterprise application integration (EAI) solutions.

Differentiate JTS and JTA?

JTS shows the implementation of a Java transaction manager. JTS specifies the implementation of a Transaction Manager which supports the Java Transaction API (JTA) 1.0. The JTA shows an architecture for building transactional application servers and defines a set of interfaces for various components of this architecture. The components are: the application, resource managers, and the application server.

Can Entity Beans have no create() methods?

Entity Beans have no create() method, when entity bean is not used to store the data in the database. In this case entity bean is used to retrieve the data from database.

What are the call back methods in Session bean?

Callback methods are called by the container to notify the important events to the beans in its life cycle. The callback methods example are ejbCreate(), ejbPassivate(), and ejbActivate().

What is bean managed transaction?

In the EJB transactions can be maintained by the container or developer can write own code to maintain the transaction. If a developer doesn�t want a Container to manage transactions, developer can write own code to maintain the database transaction.

What are transaction isolation levels in EJB?

There are four levels : * Serializable * Committed Read * Repeatable Read * Uncommitted Read

What is "application client" ?

Application clients have access to some J2EE platform APIs.

What is "application client container" ?

A container that supports application client components.

What is "application client module" ?

A software unit that have one or more classes and an application client deployment descriptor.

What is "application component provider" ?

A vendor that provides the Java classes that implement components' methods, JSP page definitions, and any required deployment descriptors.

What are "application configuration resource file" ?

An XML file used to configure resources for a Java Server Faces application.

What is "archiving" ?

Process of saving the state of an object and restoring it.

What is "asant" ?

A Java-based build tool that can be extended using Java classes. The configuration files are XML-based, calling out a target tree where various tasks get executed.

What is "attribute"?

A qualifier on an XML tag that provides additional information to the client .

What is authentication ?

Process that verifies the identity of a user, device, or other entity in a computer system.

What is authorization ?

The process by which access to a method or resource is determined.

What is authorization constraint ?

An rule that determines who is permitted to access a Web resource collection.

What is B2B ?

B2B stands for Business-to-business process.

What is backing bean ?

The backing bean defines properties for the components on the page and methods that perform processing for the component.

What is basic authentication ?

An authentication mechanism in which a Web server authenticates an entity via a user name and password obtained using the Web application's built-in authentication mechanism.

What is bean-managed persistence ?

The mechanism whereby data transfer between an entity bean's variables and a resource manager is managed by the entity bean.