Sunday, August 31, 2014

Five Common Questions from JavaScript Beginners

What is JavaScript?


JavaScript is an easy-to-use version of programming language (a constructed language that communicates instructions to a computer).  JavaScript is a scripting language (this means it is a programming language that supports scripts, which can interpret and automate the execution of tasks). Developed by Brendan Eich, while working for Netscape Communications Corporation, this program code was designed to appeal to nonprofessional programmers.  JavaScript runs inside an Internet browser. JavaScript is not a stand-alone programming language.  JavaScript is designed to embed information  in a web browser.  JavaScript includes the following features: dynamic typing and has first-class functions.  

Is Java the same as JavaScript?

Though both Java and JavaScript are both programming languages they are also immensely different.  Java is a very complex programming language while JavaScript is an easy-to-use version that is great for those beginners learning about program languages.  JavaScript and Java have the same expression syntax, naming conventions, and basic control-flow constructs.  JavaScript was previously named LiveScript; the name change has caused much confusion between Java and JavaScript.  Java is a programming language that can be used as a ‘stand-alone’ whereas JavaScript must rely on the environment it is operating in.

How Does JavaScript Work?

JavaScript works by placing code within a Web page.  When a browser loads the page, the built in interpreter reads the JavaScript code it finds within the page and runs it.

What is JavaScript Used For?

JavaScript is mostly used to allow ‘client-side’ scripts to interact with the user.  It is also used to control a Web browser, alter content that is displayed in a document, game development, as well as desktop and mobile applications.  Real life applications where this can be used (outside of web pages) include PDF documents, site-specific browsers, and desktop widgets.

What are the Various Data types used in JavaScript?

Number: Numbers can be written with or without decimals and may use scientific (exponential) notation if they are large enough. Does not allow the use of non-numbers such as NaN.
String: Strings are written with quotes.  Either double or single. 
Boolean: Can only have two values: true or false.
Array: Arrays use square brackets ‘[]’ while separating elements with comas.
Object: Objects are written with curly brackets ‘{}’. Comas separate each pair while the colon ‘:’ separates the name from its value.
Null: An empty value.  Variables can be emptied by setting the value to null.
Undefined: The value of a variable with no value.

Author Bio: Right brain, meet left brain. We're the whole brain. Agency Fusion in Salt Lake City, Utah is a web development and design firm with both creative and technological expertise. Learn more about Agency Fusion.

Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAX-WS Jar files for SOAP web services

If you are writing a SOAP based webservice you may need to add below JAR files in your applications classpath

Error: java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Add: jaxws-rt.jar

Error: java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
Add: jaxb-core.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/stream/buffer/XMLStreamBuffer
Add: streambuffer.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
Add: jaxb-impl.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/ws/policy/PolicyException
Add: policy.jar

Error: java.lang.NoClassDefFoundError: org/jvnet/staxex/XMLStreamReaderEx
Add: stax-ex.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/gmbal/ManagedObjectManager
Add: gmbal-api-only.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/external/amx/AMXGlassfish
Add: management-api.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/ha/store/api/BackingStoreException
Add: ha-api.jar

You can download latest version of JAR file (E.g. jaxws-ri-2.2.8.zip ) from https://jax-ws.java.net/2.2.8/. For more information please see the link https://jax-ws.java.net/.

Java - JAXB marshalling and unmarshalling of ArrayList

Marshalling and unmarshalling of ArrayList using JAXB is rather simple however you cannot directly marshall ArrayList. You will need a holder class (wrapper class). It will hold the arraylist that you want to marshall or unmarshall.

Please see the self explanatory java code below -

Output -

--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
    <Value>one</Value>
    <Value>two</Value>
    <Value>three</Value>
</List>

--Unmarshalling--

[one, two, three]

Wednesday, July 23, 2014

Integration of DWR using DWRSpringServlet

DWR is a Easy Ajax for Java. To know more click here.

In this article we will see sample codes for integration of DWR with DWRSpringServlet

Step 1 - Create a web project in eclipse.

Step 2 - Add below Spring libraries

  • Spring Core libraries
  • Spring Web libraries
  • Spring AOP libraries
Step 3 - Add dwr.jar
Download appropriate version of dwr.jar and add it in your application classpath. For example DWR 3 requires Spring version 2.5 or greater.

Step 4 - Create dependent files

web.xml
The DwrSpringServlet can be used if you are not using Spring MVC. This servlet gains access to the Spring context configured in your web.xml. You can create your own context file, in our case it is dwrcontext.xml.


dwrcontext.xml


DWRService.java


index.jsp


Step 5 - Deploy the project in tomcat (or any other web server) and check the output