# FruitTest.java A class that connects to either an Oracle or a Sybase database and prints
out the values of predetermined columns in the "fruits" table.
package cwp;
import java.sql.*;
/** A JDBC example that connects to either an Oracle or
* a Sybase database and prints out the values of
* predetermined columns in the "fruits" table.
*
*/
public class FruitTest {
/** Reads the hostname, database name, username, password,
* and vendor identifier from the command line. It
* uses the vendor identifier to determine which
* driver to load and how to format the URL. The
* driver, URL, username, host, and password are then
* passed to the showFruitTable method.
*/
public static void main(String[] args) {
if (args.length < 5) {
printUsage();
return;
}
String vendorName = args[4];
int vendor = DriverUtilities.getVendor(vendorName);
if (vendor == DriverUtilities.UNKNOWN) {
printUsage();
return;
}
String driver = DriverUtilities.getDriver(vendor);
String host = args[0];
String dbName = args[1];
String url = DriverUtilities.makeURL(host, dbName, vendor);
String username = args[2];
String password = args[3];
showFruitTable(driver, url, username, password);
}
/** Get the table and print all the values. */
public static void showFruitTable(String driver,
String url,
String username,
String password) {
try {
// Load database driver if not already loaded
Class.forName(driver);
// Establish network connection to database
Connection connection =
DriverManager.getConnection(url, username, password);
// Look up info about the database as a whole.
DatabaseMetaData dbMetaData = connection.getMetaData();
String productName =
dbMetaData.getDatabaseProductName();
System.out.println("Database: " + productName);
String productVersion =
dbMetaData.getDatabaseProductVersion();
System.out.println("Version: " + productVersion + "\n");
System.out.println("Comparing Apples and Oranges\n" +
"============================");
Statement statement = connection.createStatement();
String query = "SELECT * FROM fruits";
// Send query to database and store results
ResultSet resultSet = statement.executeQuery(query);
// Look up information about a particular table
ResultSetMetaData resultsMetaData =
resultSet.getMetaData();
int columnCount = resultsMetaData.getColumnCount();
// Column index starts at 1 (ala SQL) not 0 (ala Java).
for(int i=1; i
Aug 02
FruitTest.java: A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the “fruits” table.
Aug 01
নোড.জেএস । Node.js – আরইএসটি সম্পন্ন এপিআই
নোড . জে এস । Node.js – আরইএসটি সম্পন্ন এপিআই
রিদওয়ান বিন শামীম
আরইএসটি আর্কিটেকচার কীঃ আরইএসটির মানে হল, রিপ্রেজেন্টেশনাল স্টেট ট্রান্সফার। এটি ওয়েব স্ট্যান্ডার্ডভিত্তিক আর্কিটেকচার এবং এইচটিটিপি প্রটোকল ব্যবহার করে। Roy Fielding এটিকে ২০০০ সালে প্রবর্তন করেন।
আরইএসটি সার্ভার রিসোর্সে এক্সেস নিশ্চিত করে, ক্লায়েন্ট এইচটিটিপি প্রটোকল ব্যবহার করে এর পরিবর্তন ও পরিবর্ধন করে থাকেন। আরইএসটি টেক্সট, জেএসওএন ও এক্সএমএলভিত্তিক রিপ্রেজেন্টেশন ব্যবহার করে রিসোর্স রিপ্রেজেন্ট করতে। এদের মধ্যে জেএসওএন সবচেয়ে জনপ্রিয়।
আরইএসটি ভিত্তিক আর্কিটেকচারে চারটি এইচটিটিপি মেথড ব্যবহার করা হয়,
• গেট
• পুট
• ডিলিট
• পোস্ট
আরইএসটি সম্পন্ন ওয়েব সার্ভারঃ এপ্লিকেশন ও সিস্টেমের প্রটোকল ও স্ট্যান্ডার্ডের সমন্বয়ে ওয়েব সার্ভার গঠিত হয়।
লাইব্রেরীর জন্য আরইএসটি তৈরি করাঃ জেএসওএন ডাটাবেসে users.json নামক ফাইলের ক্ষেত্রে
{
“user1” : {
“name” : “mahesh”,
“password” : “password1”,
“profession” : “teacher”,
“id”: 1
},
“user2” : {
“name” : “suresh”,
“password” : “password2”,
“profession” : “librarian”,
“id”: 2
},
“user3” : {
“name” : “ramesh”,
“password” : “password3”,
“profession” : “clerk”,
“id”: 3
}
}
এসবের ভিত্তিতে যে আরইএসটি এপিআই পাই তাদের তালিকা,
S. N. URI HTTP Method POST body Result
1 listUsers GET empty সব ইউজারের তালিকা দেখায়
2 addUser POST JSON String নতুন ইউজারে বিবরণ সংযুক্তি
3 deleteUser DELETE JSON String বিদ্যমান ইউজার ডিলিট করা
4 :id GET empty ইউজারের বিবরণ প্রদর্শন
লিস্ট ইউজারঃ আমাদের প্রথম আরইএসটি এপিআই listUsers এপিআই server.js ফাইলে নিচের কোড ব্যবহার করে পাই,
var express = require(‘express’);
var app = express();
var fs = require(“fs”);
app.get(‘/listUsers’, function (req, res) {
fs.readFile( __dirname + “/” + “users.json”, ‘utf8’, function (err, data) {
console.log( data );
res.end( data );
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Example app listening at http://%s:%s”, host, port)
})
লোকাল মেশিনে নির্দিষ্ট http://127.0.0.1:8081/listUsers এপিআইতে ব্যবহার করে আমরা পাই,
{
“user1” : {
“name” : “mahesh”,
“password” : “password1”,
“profession” : “teacher”,
“id”: 1
},
“user2” : {
“name” : “suresh”,
“password” : “password2”,
“profession” : “librarian”,
“id”: 2
},
“user3” : {
“name” : “ramesh”,
“password” : “password3”,
“profession” : “clerk”,
“id”: 3
}
}
ইউজার সংযুক্ত করাঃ নিচের এপিআই দ্বারা নতুন ইউজার সংযুক্ত করা হয়,
user = {
“user4” : {
“name” : “mohit”,
“password” : “password4”,
“profession” : “teacher”,
“id”: 4
}
}
addUser এপিআই দ্বারা ডাটাবেসে নতুন ইউজার সংযুক্ত করা হয়,
var express = require(‘express’);
var app = express();
var fs = require(“fs”);
var user = {
“user4” : {
“name” : “mohit”,
“password” : “password4”,
“profession” : “teacher”,
“id”: 4
}
}
app.get(‘/addUser’, function (req, res) {
// First read existing users.
fs.readFile( __dirname + “/” + “users.json”, ‘utf8’, function (err, data) {
data = JSON.parse( data );
data[“user4”] = user[“user4”];
console.log( data );
res.end( JSON.stringify(data));
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Example app listening at http://%s:%s”, host, port)
})
লোকাল মেশিনে এপিআইতে http://127.0.0.1:8081/addUsers ব্যবহার করে যে ফলাফল আমরা পাব তা হল,
{ user1:
{ name: ‘mahesh’,
password: ‘password1’,
profession: ‘teacher’,
id: 1 },
user2:
{ name: ‘suresh’,
password: ‘password2’,
profession: ‘librarian’,
id: 2 },
user3:
{ name: ‘ramesh’,
password: ‘password3’,
profession: ‘clerk’,
id: 3 },
user4:
{ name: ‘mohit’,
password: ‘password4’,
profession: ‘teacher’,
id: 4 }
}
শো ডিটেইলঃএর জন্য যে এপিআই ব্যবহার করতে হবে তা হল,
var express = require(‘express’);
var app = express();
var fs = require(“fs”);
app.get(‘/:id’, function (req, res) {
// First read existing users.
fs.readFile( __dirname + “/” + “users.json”, ‘utf8’, function (err, data) {
data = JSON.parse( data );
var user = users[“user” + req.params.id]
console.log( user );
res.end( JSON.stringify(user));
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Example app listening at http://%s:%s”, host, port)
})
উপরের সার্ভিসকে http://127.0.0.1:8081/2 ব্যবহার করে লোকাল মেশিনে কল দেয়াতে হয়।
{
“name”:”suresh”,
“password”:”password2″,
“profession”:”librarian”,
“id”:2
}
ইউজার ডিলিট করার জন্যঃ এক্ষেত্রে যে এপিআই ব্যবহৃত হয় তা হল,
var express = require(‘express’);
var app = express();
var fs = require(“fs”);
var id = 2;
app.get(‘/deleteUser’, function (req, res) {
// First read existing users.
fs.readFile( __dirname + “/” + “users.json”, ‘utf8’, function (err, data) {
data = JSON.parse( data );
delete data[“user” + 2];
console.log( data );
res.end( JSON.stringify(data));
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Example app listening at http://%s:%s”, host, port)
})
উপরের সার্ভিসকে http://127.0.0.1:8081/deleteUser ব্যবহার করে লোকাল মেশিনে কল দিলে যে ফলাফল আসবে,
{ user1:
{ name: ‘mahesh’,
password: ‘password1’,
profession: ‘teacher’,
id: 1 },
user3:
{ name: ‘ramesh’,
password: ‘password3’,
profession: ‘clerk’,
id: 3 }
}
তথ্যসূত্রঃ http://www.tutorialspoint.com/nodejs/nodejs_restful_api.htm
Aug 01
ContactSection.jsp A snippet of a JSP page. It defines a field (accessCount), so pages that include it and want to directly utilize that field must use the include directive, not jsp:include.
ContactSection.jsp হচ্ছে JSP পৃষ্ঠার একটি অংশ।এটি একটি ফিল্ডকে সঙ্গায়িত করে (accessCount), সুতরাং যে পৃষ্ঠায় এটি অন্তর্ভুক্ত থাকে এবং সরাসরি উক্ত ফিল্ড ব্যবহার করাতে চায় তাকে আবশ্যই include directive ব্যবহার করতে হবে, jsp:include নয়।.
<%@ page import="java.util.Date" %>
<%-- The following become fields in each servlet that
results from a JSP page that includes this file. --%>
<%!
private int accessCount = 0;
private Date accessDate = new Date();
private String accessHost = "<I>No previous access</I>";
%>
<P>
<HR>
This page © 2000
<A HREF="http//www.my-company.com/">my-company.com</A>.
This page has been accessed <%= ++accessCount %>
times since server reboot. It was last accessed from
<%= accessHost %> at <%= accessDate %>.
<% accessHost = request.getRemoteHost(); %>
<% accessDate = new Date(); %>
Jul 31
নোড . জে এস । Node.js – ওয়েব মডিউল (Web Module)
নড জেএসঃ ওয়েব মডিউল
রিদওয়ান বিন শামীম
ওয়েব সার্ভার কীঃ ওয়েব সার্ভার হল সফটওয়ার এপ্লিকেশন যা এইচটিটিপি ক্লায়েন্টের পাঠানো এইচটিটিপি রিকোয়েস্ট নিয়ন্ত্রণ করে। ওয়েব সার্ভার ইমেজ, স্টাইল শিট ও স্ক্রিপ্টসহ এইচটিএমএল ডকুমেন্ট আদানপ্রদান করে।
বেশিরভাগ ওয়েব সার্ভার স্ক্রিপ্টিং ল্যাঙ্গুয়েজ সমৃদ্ধ সার্ভার সাইড স্ক্রিপ্ট সমর্থন করে যা ডাটাবেস থেকে তথ্য সংগ্রহ, জটিল লজিক সম্পাদন ও পরিশেষে এইচটিটিপি ক্লায়েন্টকে ওয়েব সার্ভারের মাধ্যমে তার ফলাফল প্রেরণ করে থাকে।
এপাচি হল বহুল ব্যবহৃত একটি ওয়েব সার্ভার যা কিনা একটি ওপেন সোর্স প্রকল্প।
ওয়েব এপ্লিকেশন স্থাপত্যঃ এই প্রক্রিয়া চারটি স্তরে বিভক্ত,
• ক্লায়েন্টঃ সার্ভারে এইচটিটিপি রিকোয়েস্ট পাঠাতে পারে এমন ওয়েব ব্রাউজার, মোবাইল ব্রাউজার ও এপ্লিকেশন এই স্তরের আলোচ্য বিষয়।
• সার্ভারঃ এইচটিটিপি রিকোয়েস্ট ইন্টারপ্রিট ও সাড়াদান করার ক্ষেত্রে সার্ভার এই ধাপ সম্পন্ন করে।
• বিজনেসঃ এই লেয়ারে থাকে এপ্লিকেশন সার্ভার যা বিভিন্ন প্রক্রিয়া সম্পন্ন করার জন্য ওয়েব সার্ভারের দ্বারা সম্পাদিত হয়ে আছে। ডাটাবেস বা এক্সটারনাল প্রোগ্রামের মাধ্যমে ডাটা লেয়ারের সাথে সমন্বয় সাধন এই ধাপের কাজ।
• ডাটাঃ ডাটাবেস বা তথ্যের অন্য কোনও উৎস এই ধাপের অন্তর্গত বিষয়।
নড ব্যবহার করে ওয়েব তৈরি করাঃ Node.js http মডিউল তৈরি করে যা সার্ভারের এইচটিটিপি ক্লায়েন্ট বা সার্ভার তৈরিতে ব্যবহৃত হয়। 8081 port মেনে চলে এমন সার্ভারের কাঠামো তৈরি করতে server.js নামের ফাইল তৈরি করতে হবে,
File: server.js
var http = require(‘http’);
var fs = require(‘fs’);
var url = require(‘url’);
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console.log(“Request for ” + pathname + ” received.”);
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {‘Content-Type’: ‘text/html’});
}else{
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {‘Content-Type’: ‘text/html’});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// Console will print the message
console.log(‘Server running at http://127.0.0.1:8081/’);
এখন একই ডিরেক্টরিতে index.htm নামের এইচটিএমএল ফাইল তৈরি করে সংরক্ষণ করি,
File: index.htm
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>
এখন server.js রান করিয়ে ফলাফল দেখি,
$ node server.js
আউটপুট ভেরিফাই করে পাই,
Server running at http://127.0.0.1:8081/
নড জেএস সার্ভারের জন্য রিকোয়েস্ট তৈরি করাঃ এজন্য যেকোনো ব্রাউজারে http://127.0.0.1:8081/index.htm খুলে ফলাফল দেখতে পারি। সার্ভার এন্ডে আউটপুট ভেরিফাই করলে,
Server running at http://127.0.0.1:8081/
Request for /index.htm received.
নড ব্যবহার করে ওয়েব ক্লায়েন্ট তৈরি করাঃ http মডিউল তৈরি করে ওয়েব ক্লায়েন্ট তৈরি করা যায়। নিচের উদাহরণটি দেখি,
client.js নামের ফাইল তৈরি করতে হবে,
File: client.js
var http = require(‘http’);
// Options to be used by request
var options = {
host: ‘localhost’,
port: ‘8081’,
path: ‘/index.htm’
};
// Callback function is used to deal with response
var callback = function(response){
// Continuously update stream with data
var body = ”;
response.on(‘data’, function(data) {
body += data;
});
response.on(‘end’, function() {
// Data received completely.
console.log(body);
});
}
// Make a request to the server
var req = http.request(options, callback);
req.end();
এখন, server.js ছাড়া অন্য কম্যান্ড টার্মিনাল ব্যবহার করে client.js রান করালে ফলাফল,
$ node client.js
আউটপুট ভেরিফাই করে আমরা পেতে পারি,
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>
সার্ভার এন্ডে আউটপুট ভেরিফাই করে পাই,
Server running at http://127.0.0.1:8081/
Request for /index.htm received.
তথ্যসূত্রঃ http://www.tutorialspoint.com/nodejs/nodejs_web_module.htm
Jul 31
J2SE : LinkedList and Iterators in Java
/*
* LinkedList.java
*
* Created on January 10, 2008, 8:51 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package linkedlist;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;
/**
*
* @author Sayed
*/
public class LinkedListTest {
/** Creates a new instance of LinkedList */
public LinkedListTest() {
}
/**
*Example operations using linked lists
*/
public void linkedListOperation(){
final int MAX = 10;
int counter = 0;
//create two linked lists
List listA = new LinkedList();
List listB = new LinkedList();
//store data in the linked list A
for (int i = 0; i < MAX; i++) {
System.out.println(" - Storing Integer(" + i + ")");
listA.add(new Integer(i));
}
//print data from the linked list using iterator
Iterator it = listA.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//print data from the linked list using listIterator.
counter = 0;
ListIterator liIt = listA.listIterator();
while (liIt.hasNext()) {
System.out.println("Element [" + counter + "] = " + liIt.next());
System.out.println(" - hasPrevious = " + liIt.hasPrevious());
System.out.println(" - hasNext = " + liIt.hasNext());
System.out.println(" - previousIndex = " + liIt.previousIndex());
System.out.println(" - nextIndex = " + liIt.nextIndex());
System.out.println();
counter++;
}
//retrieve data from the linked list using index
for (int j=0; j < listA.size(); j++) {
System.out.println("[" + j + "] - " + listA.get(j));
}
//find the location of an element
int locationIndex = listA.indexOf("5");
System.out.println("Index location of the String \"5\" is: " + locationIndex);
//find the first and the last location of an element
System.out.println("First occurance search for String \"5\". Index = " +
listA.indexOf("5"));
System.out.println("Last Index search for String \"5\". Index = " +
listA.lastIndexOf("5"));
//create a sublist from the list
List listSub = listA.subList(10, listA.size());
System.out.println("New Sub-List from index 10 to " + listA.size() + ": " +
listSub);
//sort the sub-list
System.out.println("Original List : " + listSub);
Collections.sort(listSub);
System.out.println("New Sorted List : " + listSub);
System.out.println();
//reverse the new sub-list
System.out.println("Original List : " + listSub);
Collections.reverse(listSub);
System.out.println("New Reversed List : " + listSub);
System.out.println();
//check to see if the lists are empty
System.out.println("Is List A empty? " + listA.isEmpty());
System.out.println("Is List B empty? " + listB.isEmpty());
System.out.println("Is Sub-List empty? " + listSub.isEmpty());
//compare two lists
System.out.println("A=B? " + listA.equals(listB));
System.out.println();
//Shuffle the elements around in some Random order for List A
Collections.shuffle(listA, new Random());
//convert a list into an array
Object[] objArray = listA.toArray();
for (int j=0; j < objArray.length; j++) {
System.out.println("Array Element [" + j + "] = " + objArray[j]);
}
//clear listA
System.out.println("List A (before) : " + listA);
System.out.println();
listA.clear();
System.out.println("List A (after) : " + listA);
System.out.println();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LinkedListTest listExample = new LinkedListTest();
listExample.linkedListOperation();
}
}
Jul 30
Use sorting criterion in sort function
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
void printCollection (const list<int>& l)
{
PRINT_ELEMENTS(l);
}
bool lessForCollection (const list<int>& l1, const list<int>& l2)
{
return lexicographical_compare
(l1.begin(), l1.end(), // first range
l2.begin(), l2.end()); // second range
}
int main()
{
list<int> c1, c2, c3, c4;
// fill all collections with the same starting values
INSERT_ELEMENTS(c1,1,5);
c4 = c3 = c2 = c1;
// and now some differences
c1.push_back(7);
c3.push_back(2);
c3.push_back(0);
c4.push_back(2);
// create collection of collections
vector<list<int> > cc;
cc.push_back(c1);
cc.push_back(c2);
cc.push_back(c3);
cc.push_back(c4);
cc.push_back(c3);
cc.push_back(c1);
cc.push_back(c4);
cc.push_back(c2);
// print all collections
for_each (cc.begin(), cc.end(),
printCollection);
cout << endl;
// sort collection lexicographically
sort (cc.begin(), cc.end(), // range
lessForCollection); // sorting criterion
// print all collections again
for_each (cc.begin(), cc.end(),
printCollection);
}
/*
1 2 3 4 5 7
1 2 3 4 5
1 2 3 4 5 2 0
1 2 3 4 5 2
1 2 3 4 5 2 0
1 2 3 4 5 7
1 2 3 4 5 2
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 2
1 2 3 4 5 2
1 2 3 4 5 2 0
1 2 3 4 5 2 0
1 2 3 4 5 7
1 2 3 4 5 7
*/
Jul 30
C++ code : Read file content
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("test", ios::in | ios::binary);
if(!in) {
cout << "Cannot open input file.\n";
return 1;
}
double num;
char str[80];
in.read((char *) &num, sizeof(double));
in.read(str, 14);
str[14] = '\0'; // null terminate str
cout << num << ' ' << str;
in.close();
return 0;
}
Jul 29
Sort objects stored in deque
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <string>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;
/* class Person
*/
class Person {
private:
string fn; // first name
string ln; // last name
public:
Person() {
}
Person(const string& f, const string& n)
: fn(f), ln(n) {
}
string firstname() const;
string lastname() const;
// ...
};
inline string Person::firstname() const {
return fn;
}
inline string Person::lastname() const {
return ln;
}
ostream& operator<< (ostream& s, const Person& p)
{
s << "[" << p.firstname() << " " << p.lastname() << "]";
return s;
}
/* binary function predicate:
* - returns whether a person is less than another person
*/
bool personSortCriterion (const Person& p1, const Person& p2)
{
/* a person is less than another person
* - if the last name is less
* - if the last name is equal and the first name is less
*/
return p1.lastname()<p2.lastname() ||
(p1.lastname()==p2.lastname() &&
p1.firstname()<p2.firstname());
}
int main()
{
// create some persons
Person p1("nicolai","josuttis");
Person p2("ulli","josuttis");
Person p3("anica","josuttis");
Person p4("lucas","josuttis");
Person p5("lucas","otto");
Person p6("lucas","arm");
Person p7("anica","holle");
// insert person into collection coll
deque<Person> coll;
coll.push_back(p1);
coll.push_back(p2);
coll.push_back(p3);
coll.push_back(p4);
coll.push_back(p5);
coll.push_back(p6);
coll.push_back(p7);
// print elements
cout << "deque before sort():" << endl;
deque<Person>::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << *pos << endl;
}
// sort elements
sort(coll.begin(),coll.end(), // range
personSortCriterion); // sort criterion
// print elements
cout << "deque after sort():" << endl;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << *pos << endl;
}
}
/*
deque before sort():
[nicolai josuttis]
[ulli josuttis]
[anica josuttis]
[lucas josuttis]
[lucas otto]
[lucas arm]
[anica holle]
deque after sort():
[lucas arm]
[anica holle]
[anica josuttis]
[lucas josuttis]
[nicolai josuttis]
[ulli josuttis]
[lucas otto]
*/
Jul 29
Expressions.jsp Page that demonstrates JSP expressions. Uses the JSP-Styles style sheet.
Expressions.jsp Page that demonstrates JSP expressions. Uses the JSP-Styles style sheet.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
Example of JSP Expressions.
Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
<META NAME="keywords"
CONTENT="JSP,expressions,JavaServer,Pages,servlets">
<META NAME="description"
CONTENT="A quick example of JSP expressions.">
<LINK REL=STYLESHEET
HREF="JSP-Styles.css"
TYPE="text/css">
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
<LI>The <CODE>testParam</CODE> form parameter:
<%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>
