Fork me on GitHub

4/22/2013

First week java web development - WD-041913

Last week, I start my journey on back-end web development. Below are some references from internet for getting some basic understanding on java web development.

Resources


Introduction into Java Web development

This tutorial introduces some basic concept about web development and java part: JSP and servlet.

REST with Java (JAX-RS) using Jersey - Tutorial

This helps me to build a simple JAX-RS web app and deploy it to Tomcat servlet container.

MySQL and Java JDBC - Tutorial & Connect To MySQL With JDBC Driver

These help me understand how to use JDBC to access MySQL database.

Definition


What is Java Servlet

A servlet is a Java programming language class used to extend the capabilities of a server. Technically speaking, a "servlet" is a Java class in Java EE that conforms to the Java Servlet API.

Servlets are most often used to

  • Process or store data that was submitted from an HTML form
  • Provide dynamic content such as the results of a database query
  • Manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer

Problems


Below are some solution for the problem I met

INSTALLING Oracle Java JDK 7 On Ubuntu 12.04 Step By Step

How do I import the javax.servlet API in my Eclipse project?

Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.

Apache2, MySQL, php, phpmyadmin installation on ubuntu12.04

注意,要把 phpadmin conf 加到 apache 裡面

Ref: 1, 2

sudo vim /etc/apache2/apache2.conf  
Include /etc/phpmyadmin/apache.conf //Add the phpmyadmin config to the file    
sudo service apache2 restart //then restart apache

Java Naming and Directory Interface

-- EOF --

4/03/2013

[Bash] Annoying directory green highlight in terminal

Recently, I found some directory become green highlighted in the background when I issue ls command as below picture.

After google that, it is due to the write permission of the others (as the red mark in the picture). There are 2 ways to fix this annoying coloring.

  1. Change the bash color setting: refer to the Reference 2
  2. Use the chmod to recover the directory permission to default 755, or use below command to recursively set all directory to default 755

    $ find . -type d -print0 | xargs -0 chmod 755 
    

Ref

  1. Gnome-terminal syntax highlighting - green highlight?
  2. NTFS directory coloring in terminal
  3. Wikipedia: File system permissions
  4. Wikipedia: Sticky_bit
  5. How to chmod 755 all directories but no file (recursively)

--EOF--

3/31/2013

[Algorithm] Money Change Problem (dynamic programming)

Question

Given a list of N coins, their values being in an array A[], return the minimum number of coins required to sum to S (you can use as many coins you want). If it's not possible to sum to S, return -1

Below are the solutions by "greedy" and "dynamic programming"
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

// greedy
int minCoins_greedy(vector<int> a, int sum) {

    sort(a.begin(), a.end(), greater<int>()); // sorted in descending order
    a.erase(unique(a.begin(), a.end()), a.end()); // remove duplicates

    int ret;
    for (int i = 0; i < a.size(); i  ) {
        int remains = sum - a[i];
        //cout << sum << " - " << a[i] << " = " << remains << endl;
        if (remains > 0) {
            ret = minCoins_greedy(a, remains);
            //cout << "ret: " << ret << endl;
            if (ret > 0) {
                cout << a[i] << "   ";
                return   ret;
            }
        } else if (remains == 0) {
            cout << a[i] << "   ";
            return 1;
        }
    }
    return -1;
}

// dynamic programming
#define MIN(a, b) ((a) == -1) ? (b) : min((a),(b));
int minCoins(vector<int> S, int sum) {

    int setSize = S.size();
    int count[setSize 1][sum 1];

    // For sum = 0, do not need any coins
    for (int i = 0; i < setSize 1; i  )
        count[i][0] = 0;

    // For empty set, it is impossible to sum to >0
    // use -1 represents fail
    for (int j = 1; j < sum 1; j  )
        count[0][j] = -1;

    /*
     * cost func:
     * c(n, m) = min( c(n-1, m) , c(n, m-M[n])   1 )
     *                ^^^^^^^^^   ^^^^^^^^^^^^^^^^
     *                don't take     take M[n]
     */
    for (int i = 1; i < setSize 1; i  ) {
        for (int j = 1; j < sum 1; j  ) {
            if ((j - S[i-1]) >= 0) {
                count[i][j] = MIN(count[i-1][j], count[i][j-S[i-1]]   1);
            }
            else
                count[i][j] = count[i-1][j];
        }
    }

    // uncomment this code to print table
    /*
    for (int i = 0; i <= setSize; i  )
    {
        for (int j = 0; j <= sum; j  )
            printf ("%4d", count[i][j]);
        printf("\n");
    }
    */

    return count[setSize][sum];
}


int main() {

    // Input to a sorted vector
    int size, sum;
    cout << "input 'size' and 'sum'" << endl;
    cout << "ex: 4 63" << endl;
    cin >> size >> sum;

    cout << "input coins" << endl;
    cout << "ex: 1 10 30 40" << endl;

    int* arr;
    arr = new int[size];
    for (int i = 0; i < size; i  ) {
        int c;
        cin >> c;
        arr[i] = c;
    }
    vector<int> vecArr(arr, arr size);

    // Output
    int out = minCoins(vecArr, sum);
    //int out = minCoins_greedy(vecArr, sum);

    cout << endl;
    if (out > 0)
        cout << "Minimum " << out << " coins needed" << endl;
    else
        cout << "Impossible" << endl;

    delete[] arr;

    return 0;
}

Ref

  1. Wikipedia: Dynamic programming
  2. Lecture 12: More about debugging, knapsack problem, introduction to dynamic programming
  3. The 0/1 Knapsack Problem - Dynamic Programming Method
  4. Dynamic Programming | Set 25 (Subset Sum Problem)
  5. Money Changing Problem 之一

3/10/2013

[C++] Reference to Pointer

Recently, I met the usage of reference of the pointer as the function parameter. I don't know what the purpose of it, and google the answer for it.

The 1st ref. well explains the difference between "pass by pointer" and "pass by reference of the pointer". The key is the default parameter passing of C/C++ is "pass by value", evne the pointer.

The 2nd ref. shows the situation which the reference of pointer come to solve elegantly

Ref

  1. C++: Reference to Pointer

  2. why pointer to pointer is needed to allocate memory in function

11/23/2012

[Android] Pack 3rd-party shared library .jar/.so into apk by Android.mk

When building android application package by Eclipse, the shared library placed in libs/armeabi* will be packed into apk automatically.
If the building process is from android source tree, the Android.mk needs to be written. So, how to pack 3rd-party shared library .jar/.so into apk by Android.mk?
For .jar
# the name is just a name, it will map to real .jar later
LOCAL_STATIC_JAVA_LIBRARIES := refJar1 \
                               refJar2

# here maps to actual .jar location
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := refJar1 :libs/oooo.jar\
                                        refJar2 :libs/xxxx.jar
For .so
# Put .so to out/target/product/***/obj/lib
$(shell cp $(wildcard $(LOCAL_PATH)/libs/armeabi/*.so) $(TARGET_OUT_INTERMEDIATE_LIBRARIES)) 

LOCAL_JNI_SHARED_LIBRARIES := libs/libxxxx

References

  1. 在apk裡打包進.so文件的方法: http://blog.csdn.net/androidboy365/article/details/6772890
  2. Android build system分析: http://blog.csdn.net/ccskyer/article/details/6122963
  3. android 內置app編譯方法及Android.mk中的系統變量說明: http://bbs.ancode.org/forum.php?mod=viewthread&tid=86
  4. Android NDK開發指南---Android.mk文件: http://hualang.iteye.com/blog/1140414
  5. Android Application, Android Libraries and Jar Libraries: http://devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/