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...

What is difference between URI,URL,URN?

URI: It is a String specification which can be used to refer an object from server.
URL:It is a String specification ,which can be used to refer an object from server through it's locator.
URN: It is a String specification which can be used to refer an object though it's logical name.

Read more...

Diffrence between Web Server and Application Server?

(1) Webserver serves pages for viewing in web browser, application server provides exposes businness logic for client applications through various protocols

(2) Webserver exclusively handles http requests.application server serves bussiness logic to application programs through any number of protocols.

(3) Webserver delegation model is fairly simple,when the request comes into the webserver,it simply passes the request to the program best able to handle it(Server side program). It may not support transactions and database connection pooling.

(4) Application server is more capable of dynamic behaviour than webserver. We can also configure application server to work as a webserver.Simply applic! ation server is a superset of webserver.

Read more...

By using java technology how many types of applications can we design?

By using java technology we are able to design two types of apllications
1.)Standard-alone application
2.)Enterprise Application

Standard alone Application: These are the application which are designed without using client-server Architecture.
They are of two types
1.)Command User Interface:  These are the application which will use command prompt to provide input and get output.
2.)Graphical User Interface:  These are the applications which will use Graphical components to provide input and to get output.

Enterprise-Applications: These are the applications which  are designed by using client-server Architecture.
They are of two types.
1.)Web Application
2.)Distributed Application
1.)Web Application:These are Application which are designed by using client-server architecture ,and they are designed not to distribute there logical over n.o of machines in the network.
                The purpose of this applications are to  generate dynamic response from server to client machine.  EX:C.G.i,servlets Jsp's ....

Distributed Application: These are applications which could be designed  by distributing there logic over n.o of machines in the network.
    The main aim of this applications is to provide distributed communication between local and remote machine.EX: E.J.B, R.M.I

Read more...

Pagination in Struts using Display Tag?

Struts display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model.
The library provides a significant amount of functionality while still being easy to use.
Displaytag can handle column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style.

In the following example we will see how to dispaly data using display tag and to do pagination and sorting.
We will use Eclipse as an IDE for our example.

Step 1: Create Eclipse dynamic web project and copy JAR files

Start Eclipse and goto File -> New -> Project -> Dynamic Web Project


Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from http://displaytag.sourceforge.net/1.2/download.html.





                 Step 2: Create Action, Form and Bean class

Once the project is created, create 3 java files ForbesData, UserAction and UserForm in package net.viralpatel.struts.displaytag.

 ForbesData is a bean class which consists of setter ,getter methods and properties.
The class code is shown below.



      
package net.viralpatel.struts.displaytag;
 
import java.util.ArrayList;
 
public class ForbesData {
    private int rank;
    private String name;
    private int age;
    private double netWorth;
 
    public ForbesData() {
 //no-argument constructor
    }
 
    public ForbesData(int rank, String name, int age, double netWorth) {
        this.rank = rank;
        this.name = name;
        this.age = age;                //parameterized constructor
        this.netWorth = netWorth;
    }
    public ArrayList loadData() {
        ArrayList userList = new ArrayList();
        userList.add(new ForbesData(1, "William Gates III", 53, 40.0));
        userList.add(new ForbesData(2, "Warren Buffett", 78, 37));
        userList.add(new ForbesData(3, "Carlos Slim Helu & family", 69, 35));
        userList.add(new ForbesData(4, "Lawrence Ellison", 64, 22.5));
        userList.add(new ForbesData(5, "Ingvar Kamprad & family", 83, 22));
        userList.add(new ForbesData(6, "Karl Albrecht", 89, 21.5));
        userList.add(new ForbesData(7, "Mukesh Ambani", 51, 19.5));
        userList.add(new ForbesData(8, "Lakshmi Mittal", 58, 19.3));
        userList.add(new ForbesData(9, "Theo Albrecht", 87, 18.8));
        userList.add(new ForbesData(10, "Amancio Ortega", 73, 18.3));
        userList.add(new ForbesData(11, "Jim Walton", 61, 17.8));
        userList.add(new ForbesData(12, "Alice Walton", 59, 17.6));
        userList.add(new ForbesData(12, "Christy Walton & family", 54, 17.6));
        userList.add(new ForbesData(12, "S Robson Walton", 65, 17.6));
        userList.add(new ForbesData(15, "Bernard Arnault", 60, 16.5));
        userList.add(new ForbesData(16, "Li Ka-shing", 80, 16.2));
        userList.add(new ForbesData(17, "Michael Bloomberg", 67, 16));
        userList.add(new ForbesData(18, "Stefan Persson", 61, 14.5));
        userList.add(new ForbesData(19, "Charles Koch", 73, 14));
        userList.add(new ForbesData(19, "David Koch", 68, 14));
        userList.add(new ForbesData(21, "Liliane Bettencourt", 86, 13.4));
        userList.add(new ForbesData(22, "Prince Alwaleed Bin Talal Alsaud", 54, 13.3));
        return userList;
    }
    public int getRank() {
        return rank;
    }
    public void setRank(int rank) {
        this.rank = rank;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getNetWorth() {
        return netWorth;
    }
    public void setNetWorth(double netWorth) {
        this.netWorth = netWorth;
    }
}
Create a UserForm class which extends ActionForm
Code is shown below
package net.viralpatel.struts.displaytag;
 
import java.util.ArrayList;
 
public class UserForm extends org.apache.struts.action.ActionForm {
 
    private ArrayList forbesList;
 
    public ArrayList getForbesList() {
        return forbesList;
    }
 
    public void setForbesList(ArrayList forbesList) {
        this.forbesList = forbesList;
    }
}

Create an action class name Useraction
package net.viralpatel.struts.displaytag;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
public class UserAction extends Action {
 
    private final static String SUCCESS = "success";
 
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        ForbesData actorData = new ForbesData();
        userForm.setForbesList(actorData.loadData());
        return mapping.findForward(SUCCESS);
    }
 
}
Step 3: Create JSPs, struts-config.xml and web.xml
Create index.jsp and user.jsp in WebContent folder and struts-config.xml and web.xml in WebContent/WEB-INF folder.
Create index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
 
//to farward action to userAction.do
Create user.jsp

<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
Add html tag
   
    

The World's Billionaires 2009 - Forbes List

close html tag

struts-config.xml

    
 
    
 
    
        
    
 
    
        
            
        
        
    
 
    
 

create web.xml

    
        action
        
            org.apache.struts.action.ActionServlet
        
        
            config/WEB-INF/struts-config.xml
        
            debug2
        
            detail2
        2
    
    
        action
        *.do
    
    
        30
    
    
        index.jsp
    


Excute the project,the output looks like below


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