How to get a timestamp in JavaScript

Below are the ways to get a timestamp in Javascript.

Date.now returns the UTC timestamp in milliseconds.

If you are using IE you can use the below code.

if (!Date.now) {
    Date.now = function now() {
        return new Date().getTime();
    };
}
To convert the timestamp in seconds use below code

Math.floor(Date.now() / 1000)

jQuery Tutorial for Beginners - Hello World Example

Below is a Jquery example of how to print "Hello World" to the Console.

<html>
<head>
<title>jQuery Example in Developersarena</title>
<script type="text/javascript" 
   src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript">
   $(document).ready(function(){
      document.write("Hello, World!");
   });
</script>   
</head>
<body>
<h1>Hello</h1>
</body>
</html>

Top 10 frequently asked JQUERY interview questions

Below are the top 10 Jquery questions that are frequently asked in Interviews

1.  What is JQUERY ?

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.

2 . What is difference between jQuery's ready and holdReady ?
jQuery's ready is an event which gets triggered automatically when DOM is ready while holdReady is a signal/flag to hold this triggering. holdReady was included in 1.6 version and it works only if used before the execution/triggering of ready event. Once ready event is fired, it has nothing to do. It is useful in dynamically loading scripts before the ready starts. It release ready event execution when used with a true parameter

3. How do you update ajax response with id " username"

function updateStatus() {
     $.ajax({
            url: 'updateUsrName.htm',
            success: function(response) {
             // update username
             $('#username').html(response);
         }
     });
}


4. How do you get the text value of a selected option in JQUERY ?

$("#proglang").val();
<select id="proglang">
   <option value="1">Java</option>
   <option value="2">Javascript</option>
   <option value="3">Jquery</option>
   <option value="4">Visual Basic</option>
   <option value="5">Unix</option>
</select>

$("#proglang option:selected").text();

5.Name any four paremeter of Jquery ajax method

data : Specifies data to be sent to the server
url : Specifies the URL to send the request to. Default is the current page
type : Specifies the type of request. (GET or POST)
cache: A Boolean value indicating whether the browser should cache the requested pages. Default is true beforeSend(xhr): A function to run before the request is sent

5.What is $() in jQuery library?

 $() function is used to wrap any object into jQuery object, which then allows you to call various method defined jQuery object. You can even pass a selector string to $() function, and it will return jQuery object containing an array of all matched DOM elements.

6 . Difference between JavaScript window.onload event and jQuery ready function?

Main difference between JavaScript onload event and jQuery ready function is that former not only waits for DOM to be created but also waits until all external resources are fully loaded including heavy images, audios and videos.  If loading images and media content takes lot of time that user can experience significant delay on execution of code defined in window.onload event. On the other hand jQuery ready() function only wait for DOM tree, and does not wait for images or external resource loading, means faster execution. Another advantage of using jQuery $(document).ready() is that you can use it multiple times in your page, and browser will execute them in the order they appear in HTML page, as opposed to onload technique, which can only be used for a single function. Given this benefits, it's always better to use jQuery ready() function than JavaScript window.onload event.

7. How can you call a method inside code-behind using jQuery ?
We can call a method inside code-behind By $.ajax and by declaring method a WebMethod

8.What are the advantages of JQUERY ?

The main advantages to adding jQuery are :
  • browser compatibility - doing something like .attr() is much easier than the native alternatives, and won't break across browsers.
  • simplification of usually complicated operations - if you'd like to see a well written cross browser compatible version of an XHR method, take a look at the source for $.ajax - for this method alone it's almost worth the overhead of jQ.
  • DOM selection - simple things like binding events & selecting DOM elements can be complicated and differ per-browser. Without a lot of knowledge, they can also be easily written poorly and slow down your page.
  • Access to future features - things like .indexOf and .bind are native javascript, but not yet supported by many browsers. However, using the jQuery versions of these methods will allow you to support them cross browser.
9. What is the difference between onload() and document.ready() function used in jQuery

  • We can have more than one document.ready() function in a page where we can have only one body unload function.
  • document.ready() function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.
  • Document.ready() function is called as soon as DOM is loaded.
  • onload() function is called when everything (DOM, images)gets loaded on the page.
10. What is difference between jQuery.get() and jQuery.ajax() method?

ajax() method is more powerful and configurable, allows you to specify how long to wait and how to handle error, get() is a specialization to over ajax just to retrieve some data.

Oracle PL/SQL - INSTRB function

In this tutorial we will explain how to use the Oracle PL/SQL INSTRB  function with syntax and examples

INSTRB Function : 

Syntax : INSTRB ('input_string',  'search_string',  start_position,  occurrence);

  • In Oracle PL/SQL INSTRB function returns the position of the "search_string" in the given input_string using bytes instead of characters.
  • input_string  and search string can be of data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB
  • return type is of data type NUMBER.
  • The first position in the input_string is treated as 1.
  • start_position is the position in the input_string from where the search will start.
  • If start_position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.
  • occurrence must be a positive integer.
  • If search_string is not found in input_string, then the INSTRB function will return 0.
  • start_position and occurrence are optional and the default value is 1.
EXAMPLE 1 :

SELECT INSTRB('DEVELOPERS ARENA','E',3, 2)
  "INSTRING_POSITION" FROM DUAL;
 
  INSTRING_POSITION
-------------------
                  8
EXAMPLE 2 :

SELECT INSTRB('DEVELOPERS ARENA','E',-2, 3)
  "INSTRING_POSITION" FROM DUAL;
 
  INSTRING_POSITION
-------------------
                  4

Oracle PL/SQL - SIGN function

In this tutorial we will explain how to use the Oracle PL/SQL SIGN function with syntax and examples

SIGN Function : 

Syntax : SIGN(input_number) ;

  • In Oracle PL/SQL , SIGN is a inbuilt function which is used to determine the sign of a give number.
  • Returns -1 if input_number is < 0
  • Returns 1 if input_number is > 0
  • Returns 0 if input_number is = 0
  • Return type is number and is either -1 ,1 or 0.


EXAMPLE :1 (When Input number is < 0 )

SELECT SIGN(-100) FROM DUAL;

SIGN
--------------------
-1
EXAMPLE :2 ( When Input number is > 0)

SELECT SIGN(143.12) FROM DUAL;

SIGN
--------------------
1
EXAMPLE :3 (When Input number is = 0 )

SELECT SIGN(0) FROM DUAL;

SIGN
--------------------
0

Oracle PL/SQL - CURRENT_DATE function

In this tutorial we will explain how to use the Oracle PL/SQL CURRENT_DATE function with syntax and examples

CURRENT_DATE Function : 

Syntax : CURRENT_DATE ;

  • CURRENT_DATE returns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE.
  • Return type is of data type DATE.
  • There are no input parameters to this function.
  • The output date can be altered by changing the timezone.

In the below Example 1 the CURRENT_DATE returns the date as per the timezone of UTC-5 and in NLS_DATE_FORMAT of DD-MON-YYYY HH24:MI:SS

In Example 2 , the session is altered to a timezone of UTC-7.

EXAMPLE :1 

ALTER SESSION SET TIME_ZONE = '-5:0';

SELECT CURRENT_DATE FROM DUAL;

CURRENT_DATE
--------------------
31-MAR-2015 18:14:08
EXAMPLE :2

ALTER SESSION SET TIME_ZONE = '-7:0';

SELECT CURRENT_DATE FROM DUAL;

CURRENT_DATE
--------------------
31-MAR-2015 16:14:08

JSP and Servlet Interaction in Java with an example

Below example shows how to print the data that is retrieved from a Form using a JSP and then passing it to a Servlet for printing it on the GUI.
What components are needed for this ?
  • JSP Page with a form input
  • A Controller 
  • Web Configuration file (web.xml)
JSP Page :

Below is the code for the JSP Page with a Form input. The Form has the below input fields.
  • First name
  • Last name
  • City
The JSP Page code looks like below.

Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
 <form action="/Index" method="post">
        <input type="text" name="fname"/><br>        
        <input type="text" name="lname"/>
        <input type="text" name="city"/>                                                                                                                                                                         
        <input type="submit" value="submit">            
    </form>

</body>
</html>

Once we have the JSP with the Form data, we need to create the Controller. Below is the code for the controller.


ControllerServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ControlServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String city = request.getParameter("city");
        System.out.println("FirstName :"+ fname);
        System.out.println("Lastname :"+ lname);
        System.out.println("City :"+ city);
    }

}
Once we have created the Controller, we need to map this in the web.xml file. Below is the code for the web.xml file for the servlet mapping.

web.xml
  <servlet>
    <servlet-name>controllerServlet</servlet-name>
    <servlet-class>com.developersareana.ControllerServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>controllerServlet</servlet-name>
    <url-pattern>/Index</url-pattern>
  </servlet-mapping> 
Hope this example helps you understand how you can pass the data from the JSP to a Servlet.

Unable to load JNI shared library Error in spring tool suite Installation

Friends, many of you would have faced this issue when opening the spring IDE. Below is the error message.




Error : Failed to load JNI shared library C:\Program Files(x86)\Java\jre1.8.0_40\bin\client\jvm.dll

How to resolve this issue ? :

First thing to do in this situation is to check the versions of OS, JDK & STS.

  • 32-bit OS - 32-bit JDK - 32-bit STS (32-bit only)
  • 64-bit OS - 32-bit JDK - 32-bit STS
  • 64-bit OS - 64-bit JDK - 64bit STS (64-bit only)

Second thing to do is to check the make sure that the JRE/JDK path is pointed correctly in the config uration file. It should look like below.

-vm
C:\<path to 64 bit program files>\Java\<jdk version>\bin\javaw.exe
After making sure the above two checks the error will be resolved.

How to Pass data from a JSP page to a Servlet using Plain Java J2EE

Below example explains how to use the JSP to get the Form data input and pass it to the servlet to store/update the database.


Step 1:  Create a class that extends the HttpServlet() as shown below.
public class FormServlet extends HttpServlet {}
Step 2 : 

Map this servlet in the web.xml as shown below
<servlet>
    <servlet-name>formServlet</servlet-name>
    <servlet-class>com.developersarena.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>formServlet</servlet-name>
    <url-pattern>/form</url-pattern>
</servlet-mapping>
Step 3 :  In the Form action POST method , point the action to the Form as shown below.
<form action="form" method="post">
Step 4 :
Then create a doPost() method in your servlet which takes the input as request parameters. The input field name is the request parameter name.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String fname = request.getParameter("firstname");
    String lname = request.getParameter("lastname");
  }
Step 5 : 

Pass these details to the persistence API's as show below..
User user = new User(fname, lname);
userDAO.save(user);

Oracle PL/SQL - INSTR function

In this tutorial we will explain how to use the Oracle PL/SQL INSTR function with syntax and examples

INSTR Function : 

Syntax : INSTR ('input_string',  'search_string',  start_position,  occurrence);

  • In Oracle PL/SQL INSTR function returns the position of the "search_string" in the given input_string.
  • input_string  and search string can be of data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB
  • return type is of data type NUMBER.
  • The first position in the input_string is treated as 1.
  • start_position is the position in the input_string from where the search will start.
  • If start_position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.
  • occurrence must be a positive integer.
  • If search_string is not found in input_string, then the INSTR function will return 0.
  • start_position and occurrence are optional and the default value is 1.

EXAMPLE 1 :

SELECT INSTR('DEVELOPERS ARENA','E',3, 2)
  "INSTRING_POSITION" FROM DUAL;
 
  INSTRING_POSITION
-------------------
                  8
EXAMPLE 2 :

SELECT INSTR('DEVELOPERS ARENA','E',-2, 3)
  "INSTRING_POSITION" FROM DUAL;
 
  INSTRING_POSITION
-------------------
                  4
EXAMPLE 3 :

SELECT INSTR('DEVELOPERS ARENA','E')
  "INSTRING_POSITION" FROM DUAL;
 
  INSTRING_POSITION
-------------------
                  2

Note: In the above example 3,since start_position and occurrence are omitted, they are defaulted to 1. Hence the search will happen from the first position and the will return the first occurrence of the letter "E".

Oracle PL/SQL - ABS function

In this tutorial we will explain how to use the Oracle PL/SQL ABS function with syntax and examples

ABS Function : 

Syntax : ABS ('input_number');

  • In Oracle PL/SQL ABS function returns the absolute value of the input number that is passed to the function.
  • input_number is of numeric datatype.
  • return type is same as the input datatype.

EXAMPLE 1 :

SELECT ABS(-143) "Absolute_value" FROM DUAL;

  Absolute_value
-----------------
             143
EXAMPLE 2 :

SELECT ABS(143.14) "Absolute_value" FROM DUAL;

  Absolute_value
-----------------
           143.14

Oracle PL/SQL - MONTHS_BETWEEN function

MONTHS_BETWEEN Function : 

Syntax : MONTHS_BETWEEN ('Date1','Date2');

  • In Oracle PL/SQL MONTHS_BETWEEN function returns the number of months between two dates.
  • Input argument to the MONTHS_BETWEEN function should be of data type 'date'.
  • Return Type of MONTHS_BETWEEN is a number.
  • If Date 1 is greater than Date 2 then the return number is positive
  • If Date 1 is less than Date 2 then the return number is negative
  • If Date 1 and Date 2  have the same day component or are the last day of the month, the return number is a whole number,Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components Date1 and Date2.

EXAMPLE 1 : ( Date 1 is greater than Date 2)

SELECT MONTHS_BETWEEN (TO_DATE('02-02-2015','MM-DD-YYYY'),
       TO_DATE('01-01-2015','MM-DD-YYYY') ) "Months"
    FROM DUAL;

    Months
----------
1.03225806
EXAMPLE 2 : ( Date 1 is less than Date 2)

SELECT MONTHS_BETWEEN (SYSDATE,SYSDATE+1) "Months"
    FROM DUAL;

Months
----------
-.03225806
EXAMPLE 3 : ( Date 1 and Date 2 has same day)

SELECT MONTHS_BETWEEN (TO_DATE ('2015-10-25', 'YYYY-MM-DD'), TO_DATE ('2015-03-25', 'YYYY-MM-DD')) "Months"
       FROM DUAL;

Months
----------
7