java4all@1986 java. Powered by Blogger.

Difference between Servlets and Jsp's?

>> Tuesday, May 31, 2011

Differences Between Servlets and Jsp:
  1.)If we want to design any application with respect to servlets we should require Java Knowledge.  To design any application with respect to Jsp ,it is not required to have Java knowledge with us.

  2.)Servlets are good at to pick up the request from client and process the request,where as jsp's are good at to generate dyanamic response to client machine.

  3.)In case of Servlets there is no clear separation between Presentation layer and business layer,where as in jsp's there is clear separation between  Presentation layer and business layer.

4.)If we modify anything in existed servlet we have to recompile and  reload the application.where as in jsp's we had modify anything in existed jsp's no need to recompile and reload it,Jsp's pages are automatically recompiled and reloaded.

5.)Servlet's are using Servlet container and jsp's are using jsp container.

6.)In Strut's Framework ,servlets act as controller where as jsp's for view part.

Read more...

Aleary we having C.G.I as Severside Component and why we are going for Servlets ?

C.G.I: It is a sever side component,which was designed on the basis of 'C' and 'Scripting Language'.
             Where 'C' is process based technology.If we deploy any C.G.I application on server ,for each request it will create a process.Where process is heavy weight.
If we send more n.o request from  client machine to server machine,more n.o of process are created on server which gives burden to server.In order to overcome the above problem we goes to servlet.

SERVLET: Servlet is a server side component ,and it is dependent on java technology.Where java is thread based technology.If we deploy any SERVLET  application on server ,for each request it will create a Thread.Where Thread is a light weight.If we send more n.o request from  client machine to server machine,more n.o of threads are created on server which doesn't provide burden to server.When compared to C.G.I ,Servlet is Preferable.

Read more...

How to read data into csv file?

For example we are having an excel sheet like below
File Format:
ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued
1,Chai,1,1,10 boxes x 20 bags,18,39,0,10,FALSE
2,Chang,1,1,24 - 12 oz bottles,19,17,40,25,FALSE

To read Data from csv file we will use CsvReader class.

import java.io.FileNotFoundException;
import java.io.IOException;

import com.csvreader.CsvReader;

public class CsvReaderExample {

 public static void main(String[] args) {
  try {
   CsvReader products = new CsvReader("products.csv");
  
   products.readHeaders();//This method is used to headers from csv file

   while (products.readRecord()) //This method is used to read data from records
   {
    String productID = products.get("ProductID");
    String productName = products.get("ProductName");
    String supplierID = products.get("SupplierID");
    String categoryID = products.get("CategoryID");
    String quantityPerUnit = products.get("QuantityPerUnit");
    String unitPrice = products.get("UnitPrice");
    String unitsInStock = products.get("UnitsInStock");
    String unitsOnOrder = products.get("UnitsOnOrder");
    String reorderLevel = products.get("ReorderLevel");
    String discontinued = products.get("Discontinued");
    
    // perform program logic here
    System.out.println(productID + ":" + productName);
   }
 
   products.close();
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

}


This is the simple way to read data from csv file.

Read more...

How to create a directory and a floder inside the directory and how to copy the files from source to target directory?

For example we had a floder D;//drafts where there are csv files. and a floder D://pdffiles it consists of some pdf files.

The first challenge is to create a directory like D://archieve/currentmonthandyear/drafts/csv files.
the second challenge is to create a directory like D://archieve/currentmonthandyear/drafts/pdf files.

For this we need a property files assume me.properties
 property file consists of key value pairs as shown below

savefile=D:/Drafts/
pdffiles=D:/Payslips/
archivefiles=D:/archive/
archivedrafts=/Drafts/
archivepdffiles=/Payslips/
csvfile=D:/Drafts/payslip.csv

We need a class to call kets from property files ,Take user defined classes  and write the following code,
JPropertyReader .java
import java.util.ResourceBundle;
public class JPropertyReader {
 public static String readProperty(String key) {
  String PROPERTY_FILE=MessageConstants.prop_dir;
  ResourceBundle rb = ResourceBundle.getBundle(PROPERTY_FILE);
  String propvalue=rb.getString(key);
  return propvalue;
 }
 public static String  readlog4JProperty(String strKey) 
 { String PROPERTY_NAME="ATR";
  ResourceBundle rb=ResourceBundle.getBundle(PROPERTY_NAME);
  String strPropertyValue=rb.getString(strKey);
  return strPropertyValue;
 }
}



ResourcebBundle .java is an abstarct class and it is available in java.utill.* package

Take a class to create dirctory and store files in it and delete files.

package com.centris.atr.common.util;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;

public class FileOperations {
public void copy_Directory_Files(){
 System.out.println("----u r in copy_directory==========");
 String[] monthName = {"January", "February",
   "March", "April", "May", "June", "July",
   "August", "September", "October", "November",
 "December"};
 Calendar cal = Calendar.getInstance(); 
 String monthSalary = monthName[(cal.get(Calendar.MONTH)-1)]; //current month-1 means pervious month
 int yearSalary = cal.get(Calendar.YEAR);//current year
 String savefile=JPropertyReader.readProperty("savefile"); D://drafts
 File srcDir = new File(savefile);
 File srcDir1 = new File(JPropertyReader.readProperty("pdffiles"));//D://pdffiles
 String archivedraftfiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivedrafts");//here we will create directory like D://archieve/perviousmonth with year/drafts
 String archievepdffiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivepdffiles");  //D://archieve/perviousmonth with year/pdffiles
 File trgDir = new File(archivedraftfiles);
 File trgDir1 = new File(archievepdffiles);
 //boolean success=true;
 try {
   FileUtils.copyDirectory(srcDir, trgDir);//copying files from source to target
   FileUtils.copyDirectory(srcDir1, trgDir1);
  } catch (IOException e) {
  e.printStackTrace();
 }
  
}
public static boolean delete_Files(){
 boolean t=true;
 String savefile=JPropertyReader.readProperty("savefile");
 String pdffiles=JPropertyReader.readProperty("pdffiles");
 File directory = new File(savefile);
 File[] files = directory.listFiles();//deleting the files csvfile from d://drafts/csvfile
 for (File file : files) {
  if (!file.delete())
  {
   t=false;
  }
 }
 File directory1 = new File(pdffiles);
 File[] files1 = directory1.listFiles();
 for (File file : files1) {
  if (!file.delete())
  {
   t=false;
  }
 }
 return t;
}
public static void main(String args[]){
FileOperations objFileOperations=new FileOperations();
  objFileOperations.copy_Directory_Files();
 
}

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP