Monday, June 25, 2012

Future Date Validation in JavaScript

Future Date Validation:            

                       var months = {
                                        Jan : 1,
                                        Feb : 2,
                                        Mar : 3,
                                        Apr : 4,
                                        May : 5,
                                        Jun : 6,
                                        Jul : 7,
                                        Aug : 8,
                                        Sep : 9,
                                        Oct : 10,
                                        Nov : 11,
                                        Dec : 12
                                    };
                                    function futureDateValidation(id) {

                                        var dateStr = '27 Jun 2012';

                                        var dateArr = dateStr.split(' ');// not a perfect solution, but meh

                                        var dateObj = new Date();
                                        var day = parseInt(dateArr[0]);
                                        alert(day);

                                        var month = months[dateArr[1]];
                                        alert(month);

                                        var year = parseInt(dateArr[2]);
                                        alert(year);

                                        if (year < 1970)
                                            year += 100;

                                        dateObj.setFullYear(year, month, day);

                                        if (dateObj > new Date()) {
                                            alert('Too late.');
                                        }
                                    }

Tuesday, June 19, 2012

Basic Log4j info

The rootLogger is the one that resides on the top of the logger hierarchy. Here we set its level to DEBUG and added the console appender (CA) to it. The console

appender can have arbitrary name, here its name is CA.

log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


Since the rootLogger level is set to DEBUG all the messages are displayed.

The log4j levels follow the following order.


DEBUG
INFO
WARN
ERROR
FATAL

If you set the rootLogger level to WARN then only the WARN, ERROR and FATAL level log messages will be displayed and the rest will be dropped



How can we write our own arraylist without using collections?

You have to use array of Object to store the element and then provide the functionality of adding,seraching and removing the element.This is the way you can create



your own arrayList without using Collections.



1. Make an array of Object holding some predefined number of objects.

2. Take a double variable indicating loadFactor.

3. if the length of the occupied array elements becomes three fourth(or the value of the loadFactor)

3.1 Create an array of Object with size double of the original array.

3.2 copy content of original array to this new array.

3.3 return the new array and delete old array

Wednesday, June 13, 2012

Database Isolation levels

Isolation levels :


TRANSACTION_SERIALIZABLE: Strongest level of isolation. Places a range lock on the data set, preventing other

users from updating or inserting rows into the data set until the transaction is

complete. Can produce deadlocks.

TRANSACTION_REPEATABLE_READ: Locks are placed on all data that is used in a query, preventing other users from

updating the data, but new phantom records can be inserted into the data set

by another user and are included in later reads in the current transaction.

TRANSACTION_READ_COMMITTED: Can't read uncommitted data by another transaction. Shared locks are held while

the data is being read to avoid dirty reads, but the data can be changed before

the end of the transaction resulting in non-repeatable reads and phantom

records.

TRANSACTION_READ_UNCOMMITTED:

Can read uncommitted data (dirty read) by another transaction, and nonrepeatable

reads and phantom records are possible. Least restrictive of all

isolation levels. No shared locks are issued and no exclusive locks are

honoured.

Saturday, June 2, 2012

Uploadify code to send parameters to server with Uploaded file

JSP code :
























Comment :





Reference :















 
 
Servlet code to get request parameter:
 
 
package com.hp;




import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;



public class UploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;



/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter writer = response.getWriter();

writer.write("call POST with multipart form data");

}



/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*

*/

@SuppressWarnings("unchecked")

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

boolean exit = false;

if (!ServletFileUpload.isMultipartContent(request)) {

throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");

}



ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory());

PrintWriter writer = response.getWriter();

response.setContentType("text/plain");

String comments=request.getParameter("comment");

try {

List items = uploadHandler.parseRequest(request);

for (FileItem item : items) {

if (!item.isFormField()) {

System.out.println("Name: " + item.getName());

System.out.println("Size: " + item.getSize());

System.out.println("Type: " + item.getContentType());

File file = File.createTempFile(item.getName(), "");

item.write(file);

writer.write("{\"name\":\""+ item.getName() + "\",\"type\":\"" + item.getContentType() + "\",\"size\":\"" + item.getSize() + "\"}");

break; // assume we only get one file at a time

}

else{

if(item.getFieldName().equalsIgnoreCase("comment")){

String comment = item.getString();

System.out.println("comment "+comment);

if(comment == null

comment.trim().isEmpty()){

writer.write("{\"error\":\""+"yes"+"\",\"errorType\":\""+"NullComment"+"\"}");

exit = true;

}



}

else if(item.getFieldName().equalsIgnoreCase("reference")){

String ref = item.getString();

System.out.println("reference "+ref);

if(ref == null

ref.trim().isEmpty()){

writer.write("{\"error\":\""+"yes"+"\",\"errorType\":\""+"NullReference"+"\"}");

exit = true;

}

}



if(exit == true){

break;

}



}

}

} catch (FileUploadException e) {

throw new RuntimeException(e);

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

writer.close();

}



}



}