Skip navigation

So you want your page to have an ajax paging for your comments page or tabular data. Ajax paging means that every time you select a page (from page navigator) it will not reload or refresh the whole web page, instead it is only change the comments or tabular data area.

This article is only one from many techniques to accomplish this, we are going to make ajax paging using Java (Hibernate + Spring MVC) and JQuery.

The way for getting data for the selected page is actually simple, for example if selected page is 2 and page size is 10 you can make SQL query like:
MYSQL


select * from app_user limit 10,10;

Notes: In Mysql,¬† zero (0) is first index. The query will get records 11-20 from table “app_user

ORACLE


select * from (SELECT rbt_config.*, ROWNUM r FROM rbt_config) where r between 11 and 20;

Fortunately we are going to use Hibernate and dont have to create specific query for each databases.
Just use code like below and Hibernate will take care how he will make queries for the database we choose.

HIBERNATE

protected List listByPage(Class clazz, int firstResult, int maxResult) {
        String strQry = "from " + clazz.getName() + " c order by c.id desc";

        Query query = this.sessionFactory.getCurrentSession().createQuery(strQry);
        query.setFirstResult(10);
        query.setMaxResults(10);

        return query.list();
    }

After you get the data you simply display it on the page, that’s it. But the tricky part from paging is how do you want the page navigator to look like, there are page navigator with first and last page link/button, there are page navigator with previous and next page link/button, also page navigator with sequences of numbers link to select a page, etc.
It’s up to you how the page navigator will look like, for these example just see it¬†by your selfūüôā .

I don’t put much code here, so just download the code and see it. Here is some of the screenshots:

Home

Comments Page 1

Comments Page 2

Tabular Data 1

Tabular Data 2

There are two examples I provide:  Comments page look a like and tabular data sample.
Every time we select a page from page navigator it will make ajax request to server (using JQuery) and replace/change the comments or tabular data area with new result without reload whole area. Also during make ajax request to server it will display loading ajax image.

Loading Ajax Image

You can download the code from here or here. It’s Java project using Maven, i’m usually open it using IntelliJ IDEA. To set up the data just run “com.ajax.paging.dao.SetUpData” from “src/test” folder,¬†using IntelliJ IDEA¬† I just right click the file –> RUN, It will save arround 20 records¬†to table “COMMENT” and “USER” for testing ajax paging.

Dont forget to edit “src/resources/hibernate.properties” to match your environtment.
In the “src/resources/app.properties” there two important keys:
app.page.size.default –> change the page size
If “app.page.size.default=6” it will display 6 records every page, and so on.

app.page.nav.trail –> change the page navigator trail
If “app.page.nav.trail=2” and selected page is 3 page navigator will look like 1 2 [3] 4 5 (two previous page and two next page) and so on.

Please build, package, and run in your favourite web server / servlet container. Download from here and here.

There’s XML file like below and you want to “extract” data from that file:

<?xml version="1.0"?>
<company name="Great Company, Inc">
	<address>Pearl Plaza Great Kuningan, Jakarta Indonesia</address>
	<employee>
		<firstname>Gardiary</firstname>
		<lastname>Rukhiat</lastname>
		<nickname>gardiary</nickname>
		<salary>3400000</salary>
	</employee>
	<employee>
		<firstname>Zinedine</firstname>
		<lastname>Zidane</lastname>
		<nickname>zidane</nickname>
		<salary>2300000</salary>
	</employee>
	<employee>
		<firstname>Justin</firstname>
		<lastname>Bieber</lastname>
		<nickname>jb</nickname>
		<salary>5000000</salary>
	</employee>
</company>

We are going to parse the XML file using SAX Parser. I’m using Java “org.xml.sax.*” package, so we dont need other library. Using SAX Parser basically we need to make a handler object that extends org.xml.sax.helpers.DefaultHandler class and override few methods. I’m only going to override methods: startElement(), ¬†characters(), endElement().

SAX Parser is reading XML from start to the end of the document, everytime he find an events (e.g tag, text, etc) he will call our overriding methods in this direction: startElement() –> characters() –> endElement(). When the parser found opening tag (“<some_tag>“) it will call startElement(), then it will go to characters() so we can get data/text inside the tag (“<some_tag>Some text“), then if they found closing tag¬†(“<some_tag>Some text</some_tag>“) it will call¬†endElement(). It’s up to you what do you want to do in every events/methods.

In the sample code that you can get from 4shared and ziddu, I provide two type of handler object:  simple one that only output every text according to the methods call and other handler that work like xml-to-object scenario.

Simple DefaultHandler

package com.sample.sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CompanySimpleHandler extends DefaultHandler {
 private final String COMPANY_TAG = "COMPANY";
 private final String ADDRESS_TAG = "ADDRESS";
 private final String EMPLOYEE_TAG = "EMPLOYEE";
 private final String FIRSTNAME_TAG = "FIRSTNAME";
 private final String LASTNAME_TAG = "LASTNAME";
 private final String NICKNAME_TAG = "NICKNAME";
 private final String SALARY_TAG = "SALARY";
 private final String NAME_ATTRIBUTE = "name";

 private boolean bAddress = false;
 private boolean bFirstname = false;
 private boolean bLastname = false;
 private boolean bNickname = false;
 private boolean bSalary = false;

 @Override
 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 System.out.print("[" + qName + "]");

 if(qName.equalsIgnoreCase(COMPANY_TAG)) {
 bCompany = true;
 System.out.println("[name]" + attributes.getValue(NAME_ATTRIBUTE) + "[/name]");
 }

 if(qName.equalsIgnoreCase(ADDRESS_TAG)) {
 bAddress = true;
 }

 if(qName.equalsIgnoreCase(EMPLOYEE_TAG)) {
 bEmployee = true;
 }

 if(qName.equalsIgnoreCase(FIRSTNAME_TAG)) {
 bFirstname = true;
 }

 if(qName.equalsIgnoreCase(LASTNAME_TAG)) {
 bLastname = true;
 }

 if(qName.equalsIgnoreCase(NICKNAME_TAG)) {
 bNickname = true;
 }

 if(qName.equalsIgnoreCase(SALARY_TAG)) {
 bSalary = true;
 }
 }

 @Override
 public void endElement(String uri, String localName, String qName) throws SAXException {
 System.out.println("[/" + qName + "]");

 if(qName.equalsIgnoreCase(ADDRESS_TAG)) {
 bAddress = false;
 }

 if(qName.equalsIgnoreCase(SALARY_TAG)) {
 bSalary = false;
 }

 if(qName.equalsIgnoreCase(NICKNAME_TAG)) {
 bNickname = false;
 }

 if(qName.equalsIgnoreCase(LASTNAME_TAG)) {
 bLastname = false;
 }

 if(qName.equalsIgnoreCase(FIRSTNAME_TAG)) {
 bFirstname = false;
 }

 if(qName.equalsIgnoreCase(EMPLOYEE_TAG)) {
 bEmployee = false;
 }

 if(qName.equalsIgnoreCase(COMPANY_TAG)) {
 bCompany = false;
 }
 }

 @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
 if(bAddress) {
 System.out.print(new String(ch, start, length));
 }

 if(bFirstname) {
 System.out.print(new String(ch, start, length));
 }

 if(bLastname) {
 System.out.print(new String(ch, start, length));
 }

 if(bNickname) {
 System.out.print(new String(ch, start, length));
 }

 if(bSalary) {
 System.out.print(new String(ch, start, length));
 }
 }
}

How to use:

package com.sample.sax;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class CompanySimpleMain {
    public static void main(String args[]) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            CompanySimpleHandler handler = new CompanySimpleHandler();

            saxParser.parse("company.xml", handler);
        } catch (Exception e) {
              e.printStackTrace();
        }
    }
}

Output:

[company][name]Great Company, Inc[/name]
[address]Pearl Plaza Great Kuningan, Jakarta Indonesia[/address]
[employee][firstname]Gardiary[/firstname]
[lastname]Rukhiat[/lastname]
[nickname]gardiary[/nickname]
[salary]3400000[/salary]
[/employee]
[employee][firstname]Zinedine[/firstname]
[lastname]Zidane[/lastname]
[nickname]zidane[/nickname]
[salary]2300000[/salary]
[/employee]
[employee][firstname]Justin[/firstname]
[lastname]Bieber[/lastname]
[nickname]jb[/nickname]
[salary]5000000[/salary]
[/employee]
[/company]

XML-to-Object Scenario
Company class:

package com.sample.sax.model;

import java.util.ArrayList;
import java.util.List;

public class Company {
    private String name;
    private String address;
    private List<Employee> employees = new ArrayList<Employee>();

    // please add setter & getter

    public void addEmployee(Employee employee) {
        this.employees.add(employee);
    }
}

Employee class:

package com.sample.sax.model;

public class Employee {
    private String firstname;
    private String lastname;
    private String nickname;
    private Long salary;

    // please make setter & getter
}

Handler:

package com.sample.sax;

import com.sample.sax.model.Company;
import com.sample.sax.model.Employee;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CompanyHandler extends DefaultHandler {
    private final String COMPANY_TAG = "COMPANY";
    private final String ADDRESS_TAG = "ADDRESS";
    private final String EMPLOYEE_TAG = "EMPLOYEE";
    private final String FIRSTNAME_TAG = "FIRSTNAME";
    private final String LASTNAME_TAG = "LASTNAME";
    private final String NICKNAME_TAG = "NICKNAME";
    private final String SALARY_TAG = "SALARY";
    private final String NAME_ATTRIBUTE = "name";

    private Company company = null;
    private Employee employee = null;

    private boolean bCompany = false;
    private boolean bName = false;
    private boolean bAddress = false;
    private boolean bEmployee = false;
    private boolean bFirstname = false;
    private boolean bLastname = false;
    private boolean bNickname = false;
    private boolean bSalary = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase(COMPANY_TAG)) {
            bCompany = true;
            company = new Company();
            company.setName(attributes.getValue(NAME_ATTRIBUTE));   // if you sure, you can use this one
        }

        if(qName.equalsIgnoreCase(ADDRESS_TAG)) {
            if(!bCompany || company==null) {
                throw new SAXException("Company is null");
            }
            bAddress = true;
        }

        if(qName.equalsIgnoreCase(EMPLOYEE_TAG)) {
            if(!bCompany || company==null) {
                throw new SAXException("Company is null");
            }
            bEmployee = true;
            employee = new Employee();
        }

        if(qName.equalsIgnoreCase(FIRSTNAME_TAG)) {
            if(!bEmployee || employee ==null) {
                throw new SAXException("Staff is null");
            }
            bFirstname = true;
        }

        if(qName.equalsIgnoreCase(LASTNAME_TAG)) {
            if(!bEmployee || employee ==null) {
                throw new SAXException("Staff is null");
            }
            bLastname = true;
        }

        if(qName.equalsIgnoreCase(NICKNAME_TAG)) {
            if(!bEmployee || employee ==null) {
                throw new SAXException("Staff is null");
            }
            bNickname = true;
        }

        if(qName.equalsIgnoreCase(SALARY_TAG)) {
            if(!bEmployee || employee ==null) {
                throw new SAXException("Staff is null");
            }
            bSalary = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equalsIgnoreCase(ADDRESS_TAG)) {
            bAddress = false;
        }

        if(qName.equalsIgnoreCase(SALARY_TAG)) {
            bSalary = false;
        }

        if(qName.equalsIgnoreCase(NICKNAME_TAG)) {
            bNickname = false;
        }

        if(qName.equalsIgnoreCase(LASTNAME_TAG)) {
            bLastname = false;
        }

        if(qName.equalsIgnoreCase(FIRSTNAME_TAG)) {
            bFirstname = false;
        }

        if(qName.equalsIgnoreCase(EMPLOYEE_TAG)) {
            company.addEmployee(employee);
            bEmployee = false;
        }

        if(qName.equalsIgnoreCase(COMPANY_TAG)) {
            bCompany = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String address;
        String firstname;
        String lastname;
        String nickname;
        Long salary;

        if(bAddress) {
            address = new String(ch, start, length);
            company.setAddress(address);
        }

        if(bFirstname) {
            firstname = new String(ch, start, length);
            employee.setFirstname(firstname);
        }

        if(bLastname) {
            lastname = new String(ch, start, length);
            employee.setLastname(lastname);
        }

        if(bNickname) {
            nickname = new String(ch, start, length);
            employee.setNickname(nickname);
        }

        if(bSalary) {
            salary = Long.parseLong(new String(ch, start, length));
            employee.setSalary(salary);
        }
    }

    public Company getCompany() {
        return company;
    }
}

How to use:

package com.sample.sax;

import com.sample.sax.model.Company;
import com.sample.sax.model.Employee;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class CompanyMain {
    public static void main(String args[]) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            CompanyHandler handler = new CompanyHandler();

            saxParser.parse("company.xml", handler);

            Company company = handler.getCompany();
            System.out.println("Company Name : " + company.getName());
            System.out.println("Company Address : " + company.getAddress());
            System.out.println("Employees : ");
            for(int i = 0; i < company.getEmployees().size(); i++) {
                Employee employee = company.getEmployees().get(i);
                System.out.println((i+1) + "\tName : " + employee.getFirstname() + " " + employee.getLastname());
                System.out.println("\tNickname : " + employee.getNickname());
                System.out.println("\tSalary : " + employee.getSalary());
            }
        } catch (Exception e) {
              e.printStackTrace();
        }
    }
}

Output:

Company Name : Great Company, Inc
Company Address : Pearl Plaza Great Kuningan, Jakarta Indonesia
Employees :
1 Name : Gardiary Rukhiat
Nickname : gardiary
Salary : 3400000
2 Name : Zinedine Zidane
Nickname : zidane
Salary : 2300000
3 Name : Justin Bieber
Nickname : jb
Salary : 5000000

Any other implementations can be very different from this sample code. You can get the sampe code from here and here.

This article is not for deploying web application outside “JBOSS_HOME” folder, if usually in JBoss to deploy our web application we put WAR file at “JBOSS_HOME\server\default\deploy” directory, i want to put it on let say “JBOSS_HOME\server\myserver\deploy” directory.

After googling sometimes I found that what I meant is change the JBoss default server configuration. Under “JBOSS_HOME\server” folder we can see folder like “all“, “default“, “minimal“, etc, that is as JBoss said is server configuration file set. With server configuration file set we can have alternate server configurations. You should read JBoss documents about how to configure the server.

To make another server configuration file set, just copy from existing server configuration to another name. Put your web application WAR¬† at “JBOSS_HOME\server\NEW_SERVER\deploy” directory, then run this from console:

Windows
JBOSS_HOME\bin\run.bat -c NEW_SERVER

Linux
JBOSS_HOME\bin\run.sh -c NEW_SERVER

Let say that you have fresh download of Tomcat 6 and want to deploy your web application in it, but you don’t want to access your web application using URL form http://%5BIP_OR_DOMAIN_NAME%5D:%5BPORT%5D/%5BWEB_APPLICATION_NAME%5D (e.g http://localhost:8080/BlaBlaBla) instead you just want URL form http://%5BIP_OR_DOMAIN_NAME%5D:%5BPORT%5D (e.g http://localhost:8080) to access your application. There are few ways to do these that I know and I will it post here.

To deploy web application in Tomcat we can use WAR file or using webapp exploded directory, I will use the last one. You can deploy your webapp in “TOMCAT_HOME/webapps” directory or outside of Tomcat directory, I will use last one also. Now let see how to change Tomcat 6 ROOT context:

Trick 1
Open¬†“TOMCAT_HOME\conf\server.xml” file, look for Host tag, add Context tag to it so it will looks something like these:

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
        ............
        ............
	<Context path="" docBase="D:\Gardiary\temp\my-app" reloadable="true" debug="0" cookies="false"></Context>
</Host>

Note that Context¬†path is set to¬† “” not “/” and docBase is path to your webapp exploded directory. Save it and run your Tomcat, ¬†you should be able to access your application just by using http://localhost:8080.

Trick 2
Remove (or change to other name) “TOMCAT_HOME\webapps\ROOT” folder, rename your webapp to ROOT and then put it in “TOMCAT_HOME\webapps” folder (a.k.a change the content of original ROOT folder with your web application content). Run your Tomcat.

Trick 3
Create file “TOMCAT_HOME\conf\Catalina\localhost\ROOT.xml“, the file looks something like these:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="D:\Gardiary\temp\my-app"
 crossContext="true" reloadable="true">
</Context>

Change docBase to your webapp exploded directory and then run your tomcat.

That’s all I know folks!

Access Control List (ACL) is a way to control access for user in an application. This page contains Access Control List sample using Java, this article show an ACL demo application in Java web application, that i hope will make better understanding of what Access Control List (ACL) is. You can find resources that will explain what ACL is, but i think demo application is more effective for understanding about ACL.

Download ACL demo application from ziddu and 4shared, it’s a Java web application, furthermore it’s Songs database application. The application is using following technology/library : JSP, Servlet, EL, Hibernate/JPA. For simplicity, i’m using JSP and servlet for presentation layer and controller, and Hibernate for database layer so i dont have to deal with SQL (i hate it :p ). This application is an IntelliJ IDEA project, you need to re-create project if you are using Eclipse or Netbeans, and add the classes/library accordingly. I use Apache Tomcat 6 for application server.

Note: Because Tomcat is actually a Servlet container rather Application Server (e.g Glassfish, Geronimo, JBoss, etc), basically Tomcat dont have Java EE library, while i’m using Hibernate/JPA annotations (for object-table database mapping) which is using Java EE library, you need to place javaee.jar in “<TOMCAT_HOME>/lib” folder before running tomcat, DO NOT put javaee.jar in application lib directory, it’s against Java Servlet specification

You may want to edit “hibernate.cfg.xml” first to match your environtment. I’m using Mysql database for this demo. Before running the application please run “acl.demo.util.SetUpACLDemoData” class, this class will set up the data on database so the application can run. Now run the application server, open the browser and go to http://localhost:8080/ACLDemo, this will open login page.
Login page

We can’t see other page if we hasn’t login, I create a SecurityFilter class for this, it’s a Servlet Filter that will intercept all request, checked whether there’s user session in session, if it’s not it will send us to login page.

To login, we can use one of the following account:
– admin/admin (Administrator)
– zidane/password (SuperAdministrator)
– kabayan/kabayan (DataEntry)
– tamara/tamara (Administrator)

The one in the parentheses is the type of a user. Type of a user will determine his/her rights of accessing something (e.g resources, page, module, etc), different user type will have different rights for accessing some pages (e.g “DataEntry” user can’t access “Administrator” level page). You can try to login with above different account and see not all the user have the same rights access. We will examine how this is happen, this is what Access Control List is.

Now login as “SuperAdministrator” so we can see all the page/module. As mention earlier this demo is a Songs database application, it’s had modules such as “Song”, “Album”, “Singer”, and “Genre”, which actually is a dummy page. It’s static, I don’t get the data from database and there’s no corresponding table also. Other modules is “ACL”, “Users”, and “UserType”, which corresponding to what we are talking about, the Access Control List (ACL). We retrieve the data from databases, as well we can add or edit, but for simplicity i don’t add delete/remove functionality. Module “ACL”, “Users”, and “UserType” are important component in this version of ACL’s, they collaborate each other determine how a user have the rights to access a modules or to do some action (e.g Insert, Update, Delete).

index.jsp

This is the dummy modules:

This is the corresponding ACL’s modules:
UserType

User

Access Control List

In this demo to manage an ACL we start by creating a user type then assign that user type to some user. Everytime we make new user type, the application also make ACL’s for that user type. Let’s make a “UserType” that i will called it “Editor” and see what happen.

Add UserType

ACL

After we save the “Editor” user type, it will redirect us to ACL page. We can see there’s ACL’s for “Editor” user type created by application for us, what we only need to set is what modules and what action that the “Editor” can access. Let’s say I just want the “Editor” has the rights to update/edit every module except administrator level module, I’ll set the ACL’s for “Editor” like below picture (note: module “ACL” and “User” is administrator level module)

ACL

Then make a User and assign the “Editor” user type to him/her (I do not add functionality to edit user type of a User). My new user will look like this:
Add User

List User

Please logout and login as “Editor” user. We can see that the menu in front page is not as complete as Administrator user, also the user can’t add new or delete record.

Index.jsp Songs Albums Singers Genres

Now let we examine the code. The code structure look like this:
Code structure

Now let’s see why¬† some buttons or links can “missing” or display according to type of¬† user account. In almost every buttons or links that I want to restrict, I put code something like this;

....
<c:if test="<%=aclManager.allowUpdate(session, Permission.ACL)%>">
      <a class="toplink" href="deleteSong.jsp?code=S0001">delete</a>
</c:if>
....
....
<c:if test="<%=aclManager.allowDelete(session, Permission.SONG)%>">
	<input class="kotaktombol" name="save" type="submit" value="Save" />
</c:if>
....

aclManager” is a “acl.demo.manager.AccessControlListManager” class, because I know that there’s “acl.demo.entity.UserSession” object on the Session, i just send “session” object as parameter to method like “allowView”, “allowInsert”, etc, to determine the user type. The “acl.demo.enums.Permission” is to determine the existing modules and what module the page is.
AccessControlListManager

package acl.demo.manager;

import acl.demo.entity.AccessControlList;
import acl.demo.entity.UserType;
import acl.demo.entity.UserSession;
import acl.demo.enums.Permission;
import acl.demo.util.HibernateUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

public class AccessControlListManager extends BaseManager {
    public AccessControlList newAccessControlList() throws Exception {
        return new AccessControlList();
    }

    public AccessControlList get(Long id) throws Exception {
        return (AccessControlList) getEntity(AccessControlList.class, id);
    }

    public AccessControlList save(AccessControlList entity) throws Exception {
        saveEntity(entity);

        return entity;
    }

    public AccessControlList update(AccessControlList entity) throws Exception {
        updateEntity(entity);

        return entity;
    }

    public void delete(AccessControlList entity) throws Exception {
        deleteEntity(entity);
    }

    public List listAll() throws Exception {
        return listAll(AccessControlList.class);
    }

    public AccessControlList getByUserTypeAndPermission(UserType userType, Permission permission) throws Exception {
        StringBuffer query = new StringBuffer("from AccessControlList acl ");
        query.append("where acl.userType = :userType and acl.permission = :permission");

        AccessControlList acl = (AccessControlList) HibernateUtil.getSession().createQuery(query.toString())
                .setParameter("userType", userType).setParameter("permission", permission)
                .uniqueResult();

        return acl;
    }

    ..........

    public boolean allowView(HttpSession session, Permission permission) throws Exception {
        return this.getByUserTypeAndPermission(((UserSession) session.getAttribute("userSession")).getUser().getUserType(), permission).isCanView();
    }

    public boolean allowInsert(HttpSession session, Permission permission) throws Exception {
        return this.getByUserTypeAndPermission(((UserSession) session.getAttribute("userSession")).getUser().getUserType(), permission).isCanInsert();
    }

    public boolean allowUpdate(HttpSession session, Permission permission) throws Exception {
        return this.getByUserTypeAndPermission(((UserSession) session.getAttribute("userSession")).getUser().getUserType(), permission).isCanUpdate();
    }

    public boolean allowDelete(HttpSession session, Permission permission) throws Exception {
        return this.getByUserTypeAndPermission(((UserSession) session.getAttribute("userSession")).getUser().getUserType(), permission).isCanDelete();
    }
}

Permission

package acl.demo.enums;

public enum Permission { SONG, SINGER, ALBUM, GENRE, USER, ACL }

UserSession

package acl.demo.entity;

import java.util.Date;

public class UserSession {
    private User user;
    private Date loginTime;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }
}

For object-table mapping I use Hibernate/JPA annotations.
User

package acl.demo.entity;

import javax.persistence.*;

@Entity
@Table(name = "USERS")
public class User {
    private Long id;
    private String username;
    private UserType userType;
    private String password;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "USERNAME")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @ManyToOne(targetEntity = UserType.class)
    @JoinColumn(name = "USER_TYPE_ID")
    public UserType getUserType() {
        return userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    @Basic
    @Column(name = "PASSWORD")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[ID=" + this.getId());
        sb.append(", USERNAME=" + this.getUsername());
        sb.append(", USERTYPE=" + this.getUserType().getType());
        sb.append(", PASSWORD=" + this.getPassword());
        sb.append("]");

        return sb.toString();
    }
}

UserType

package acl.demo.entity;

import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;

@Entity
@Table(name = "USER_TYPE")
public class UserType {
    private Long id;
    private String type;
    private List<User> users = new ArrayList<User>();

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "TYPE")
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @OneToMany(targetEntity = User.class, mappedBy = "userType")
    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[ID=" + this.getId());
        sb.append(", TYPE=" + this.getType());
        sb.append(", USERS=" + this.getUsers());
        sb.append("]");

        return sb.toString();
    }

AccessControlList

package acl.demo.entity;

import acl.demo.enums.Permission;

import javax.persistence.*;

@Entity
@Table(name = "ACCESS_CONTROL_LIST")
public class AccessControlList {
    private Long id;
    private Permission permission;
    private UserType userType;
    private boolean canView;
    private boolean canInsert;
    private boolean canUpdate;
    private boolean canDelete;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Enumerated(value = EnumType.STRING)
    @Column(name = "PERMISSION")
    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }

    @ManyToOne(targetEntity = UserType.class)
    @JoinColumn(name = "USER_TYPE_ID")
    public UserType getUserType() {
        return userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    @Basic
    @Column(name = "CAN_VIEW")
    public boolean isCanView() {
        return canView;
    }

    public void setCanView(boolean canView) {
        this.canView = canView;
    }

    @Basic
    @Column(name = "CAN_INSERT")
    public boolean isCanInsert() {
        return canInsert;
    }

    public void setCanInsert(boolean canInsert) {
        this.canInsert = canInsert;
    }

    @Basic
    @Column(name = "CAN_UPDATE")
    public boolean isCanUpdate() {
        return canUpdate;
    }

    public void setCanUpdate(boolean canUpdate) {
        this.canUpdate = canUpdate;
    }

    @Basic
    @Column(name = "CAN_DELETE")
    public boolean isCanDelete() {
        return canDelete;
    }

    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[ID=" + this.getId());
        sb.append(", PERMISSION=" + this.getPermission());
        sb.append(", USERTYPE=" + this.getUserType().getType());
        sb.append(", CAN_VIEW=" + this.isCanView());
        sb.append(", CAN_INSERT=" + this.isCanInsert());
        sb.append(", CAN_UPDATE=" + this.isCanUpdate());
        sb.append(", CAN_DELETE=" + this.isCanDelete());
        sb.append("]");

        return sb.toString();
    }
}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- DB schema will be updated if needed -->
        <property name="hbm2ddl.auto">update</property>

        <!-- MAPPING -->
        <mapping class="acl.demo.entity.UserType" />
        <mapping class="acl.demo.entity.User" />
        <mapping class="acl.demo.entity.AccessControlList" />
    </session-factory>
</hibernate-configuration>

SecurityFilter looks like this:

package acl.demo.servlet.filter;

import acl.demo.entity.UserSession;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SecurityFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession(false);

        System.out.println("[SecurityFilter] ServletPath : " + request.getServletPath());
        if(!request.getServletPath().equalsIgnoreCase("/login") &&
                !request.getServletPath().equalsIgnoreCase("/logout") &&
                !request.getServletPath().equalsIgnoreCase("/loginForm.jsp") &&
                !request.getServletPath().equalsIgnoreCase("/css/format.css")) {
            if(session != null) {
                UserSession userSession = (UserSession) session.getAttribute("userSession");

                if(userSession == null) {
                    System.out.println("[SecurityFilter] userSession : " + userSession);
                    request.setAttribute("message", "Please login first!");
                    request.getRequestDispatcher("/loginForm.jsp").forward(request, response);
                    return;
                }
            } else {
                request.setAttribute("message", "Please login!");
                request.getRequestDispatcher("/loginForm.jsp").forward(request, response);
                return;
            }
        }

        chain.doFilter(req, resp);
    }
}

This is a simple example of an Access Control List (ACL) presented in Java, you can dowload it from here and here

This article will show how to upload and download in web application using Spring-MVC. The application will upload any file format and save it to database, likewise when downloading it will read from database and open save download dialog.

Consider a form like this:

Here is html code:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head><title>Upload and Download files using Spring</title></head>
  <body>
    <table width="80%" border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th width="4%">No</th>
            <th width="30%">Filename</th>
            <th width="30%">Notes</th>
            <th width="16%">Type</th>
            <th width="20%">&nbsp;</th>
        </tr>
        <c:choose>
            <c:when test="${files != null}">
                <c:forEach var="file" items="${files}" varStatus="counter">
                    <tr>
                        <td>${counter.index + 1}</td>
                        <td>${file.filename}</td>
                        <td>${file.notes}</td>
                        <td>${file.type}</td>
                        <td><div align="center"><a href="download.htm?id=${file.id}">Download</a> /
                            <a href="delete.htm?id=${file.id}">Delete</a></div>
                        </td>
                    </tr>
                </c:forEach>
            </c:when>
        </c:choose>
    </table>

    <h2>Add New File</h2>
    <form action="upload.htm" method="post" enctype="multipart/form-data">
        <table width="60%" border="1" cellspacing="0">
            <tr>
                <td width="35%"><strong>File to upload</strong></td>
                <td width="65%"><input type="file" name="file" /></td>
            </tr>
            <tr>
                <td><strong>Notes</strong></td>
                <td><input type="text" name="notes" width="60" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="submit" value="Add"/></td>
            </tr>
        </table>
    </form>
  </body>
</html>

To upload a file from web page we need to set a form with property enctype="multipart/form-data" . This will use by action/controller how to handle the data.

To save a binary file to database we use column with type Blob. I’m using Mysql database, i’m choose column with type longblob. There’s three type of blob in Mysql (other is blob and mediumblob), i’m using longblob so it can store more bigger file. This is create table script:

 

CREATE TABLE `files` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `filename` varchar(100) NOT NULL,
  `notes` varchar(100) default NULL,
  `type` varchar(40) default NULL,
  `file` longblob default NULL,
  PRIMARY KEY  (`id`)
);

 

As we can see column ‘file’ type longblob, we wil use to store the binary of a file. Column ‘filename’ to store original file name, column ‘type’ to save the type of the file, and column ‘notes’ is optional.

And here is the java bean to represent the table:

package com.spring.example.bean;
public class Files {
    private int id;
    private String filename;
    private String notes;
    private String type;
    private byte[] file;

    // please make setter and getter
}

For the blob type column, i use bytes array rather that Java Blob type. It will suit accross different databases blob type.

Service/manager/DAO
For database layer we’re using Spring JDBC, so we create service class that extends¬†JdbcDaoSupport class.

package com.spring.example.web;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.spring.example.service.FilesService;
import com.spring.example.bean.Files;
import java.util.List;

public class FilesService extends JdbcDaoSupport {
    String query = null;

    /**
     * find
     */
    public Files find(int id) {
        query = "select * from files where id = ?";

        try {
            Files file = (Files) getJdbcTemplate().queryForObject(query, new Object[] {id},
                new RowMapper() {
                    Files fl;
                    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                        fl = new Files();
                        fl.setId(rs.getInt(1));
                        fl.setFilename(rs.getString(2));
                        fl.setNotes(rs.getString(3));
                        fl.setType(rs.getString(4));
                        fl.setFile(rs.getBytes(5));

                        return fl;
                    }
            });

            return file;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * listAll
     */
    public List<Files> listAll() {
        query = "select id, filename, notes, type from files";

        try{
            List<Files> files = getJdbcTemplate().query(query, new BeanPropertyRowMapper(Files.class));

            return files;
        } catch(Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * save
     */
    public void save(final Files file) {
        query = "insert into files (filename, notes, type, file) values (?, ?, ?, ?)";

        try {
            synchronized(this) {
                getJdbcTemplate().update(new PreparedStatementCreator() {

                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                        PreparedStatement statement = con.prepareStatement(query);
                        statement.setString(1, file.getFilename());
                        statement.setString(2, file.getNotes());
                        statement.setString(3, file.getType());
                        statement.setBytes(4, file.getFile());
                        return statement;
                    }
                });
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * delete
     */
    public void delete(int id) {
        query = "delete from files where id = ?";

        try {
            getJdbcTemplate().update(query, new Object[] {id});
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

File List Form
Here is the controller that will query all the files that we had upload, send it to page so it will display all uploaded files, then we can download or delete it. And we can upload a new file.

package com.spring.example.web;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.spring.example.service.FilesService;
import com.spring.example.bean.Files;
import java.util.List;

public class FilesForm extends AbstractController {
    private FilesService filesService;

    public void setFilesService(FilesService filesService) {
        this.filesService = filesService;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        List<Files> files = this.filesService.listAll();

        return new ModelAndView("files", "files", files);
    }
}

Files Controller
This is a Spring MultiActionController so i dont need separate controller class file to upload, download, and delete a file.

package com.spring.example.web;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.spring.example.bean.Files;
import com.spring.example.service.FilesService;

public class FilesController extends MultiActionController {
    private FilesService filesService;

    public void setFilesService(FilesService filesService) {
        this.filesService = filesService;
    }

    /**
     * upload
     */
    public ModelAndView upload(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");

        Files file = new Files();
        file.setFilename(multipartFile.getOriginalFilename());
        file.setNotes(ServletRequestUtils.getStringParameter(request, "notes"));
        file.setType(multipartFile.getContentType());
        file.setFile(multipartFile.getBytes());

        this.filesService.save(file);

        return new ModelAndView("redirect:files.htm");
    }

    /**
     * download
     */
    public ModelAndView download(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        int id = ServletRequestUtils.getRequiredIntParameter(request, "id");

        Files file = this.filesService.find(id);

        response.setContentType(file.getType());
        response.setContentLength(file.getFile().length);
        response.setHeader("Content-Disposition","attachment; filename=\"" + file.getFilename() +"\"");

        FileCopyUtils.copy(file.getFile(), response.getOutputStream());

        return null;

    }

    /**
     * delete
     */
    public ModelAndView delete(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        int id = ServletRequestUtils.getRequiredIntParameter(request, "id");

        this.filesService.delete(id);

        return new ModelAndView("redirect:files.htm");
    }
}

applicationContext.xml
Here is the applicationContext.xml, please change proper value to match your environtment:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- datasource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test">
        </property>
    </bean>

    <!-- template -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- services -->
    <bean id="filesService" class="com.spring.example.service.FilesService">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

dispatcher-servlet.xml
The dispatcher-servlet.xml contains list of mapping url address to controller class, i use SimpleUrlHandlerMapping class for url handler mapping. And there’s two view resolver, the basic InternalResourceViewResolver and CommonsMultipartResolver to handle file upload, i set the ‘maxUploadSize’ property to 5Mb.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- mapping -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="files.htm">filesForm</prop>
                <prop key="upload.htm">filesController</prop>
                <prop key="download.htm">filesController</prop>
                <prop key="delete.htm">filesController</prop>
            </props>
        </property>
    </bean>

    <!-- The view resolver -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5242880" />
    </bean>

    <!-- controller -->
    <bean id="filesForm" class="com.spring.example.web.FilesForm">
        <property name="filesService" ref="filesService"/>
    </bean>

    <bean id="filesController" class="com.spring.example.web.FilesController">
        <property name="filesService" ref="filesService"/>
        <property name="methodNameResolver">
            <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
                <property name="mappings">
                    <props>
                        <prop key="/upload.htm">upload</prop>
                        <prop key="/download.htm">download</prop>
                        <prop key="/delete.htm">delete</prop>
                    </props>
                </property>
            </bean>
        </property>
    </bean>
</beans>

Common Errors/Exception when upload a file (in Mysql)
Commons error during file upload process is how big size of file can be transferred to a server or save to a databases.
If you experienced similiar error like this, when using mysql database:

 

Packet for query is too large (1217245 > 1047552). You can change this value
on the server by setting the max_allowed_packet' variable.; nested exception is
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1217245 > 1047552).
You can change this value on the server by setting the max_allowed_packet' variable.

 

or

 

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too
large (1217245 > 1047552). You can change this value on the server by setting
the max_allowed_packet' variable.

 

In Unix/Linux system open file “/etc/my.cnf“, look for the max_allowed_packet string and set the max value you want to.

Here, here and here is the code.

There’s many way to make USB modem Vodafone Huawei E220 work on openSUSE 11, this modem work in Ubuntu 9.04 also, so i think this will work on different (latest) version of openSUSE or Ubuntu. USB modem Huawei E220 is run out of the box in OpenSUSE 11 and Ubuntu 9, it’s completelly detected.

To make it run we can use one of this program:

  1. NetworkManager
  2. Wvdial
  3. UMTSmon
  4. Vodafone Mobile Connect Card Driver for Linux
  5. KInternet
  6. smpppd
  7. kppp
  8. HSOconnect

I’m going to show how to configure Vodafone Huawei E220 in NetworkManager, wvdial, UMTSmon, and Vodafone Mobile Connect.

Issues in Point-to-Point Protocol (PPP) on Linux
Before we got into that, there’s a problem when using ppp on Linux. Sometimes after we make a successfull connection using one of the program, it set the wrong DNS on /etc/resolv.conf usually to 10.11.12.13 and 10.11.12.14, so we still not connect to the internet. This is not always happen, some other time it set correct DNS. But in my case it’s almost always set bogus DNS, so usually after the connection is establish I open /etc/resolv.conf with text editor (Vi, Kwrite, Gedit, etc) then set the correct DNS.

Where do i get the correct DNS ? well i just run this modem in Windows, see the DNS and then get back to Linux to set the correct DNS. Off course if you only use one GSM card, you only need to do this once. To make thing simple on changing the DNS, I make another file (eg, “/etc/resolv-true.conf“) with the correct DNS, after the connection is establish in the console I type “CP /etc/resolv-true.conf /etc/resolv.conf“, it will replace /etc/resolv.conf that had wrong DNS with my file. I almost always to do this every time¬†ūüė¶ .

I don’t know what issues this is, in openSuse 11 it’s always set wrong DNS, but it’s not always in Ubuntu 9. Maybe it’s modem or ppp version issues. However i’m trying Vodafone K3520 / Huawei K3520 in openSuse 11 and Ubuntu 9, it always get the correct DNS, really weird.

Ref:
https://bugzilla.redhat.com/show_bug.cgi?id=467004
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=445711
http://umtsmon.sourceforge.net/docs/OLS.umts.paper.pdf

Issues using Firefox
If we make a connection besides using NetworkManager,¬† it’s likely that you wont be able to use Mozilla Firefox, we can’t browse anything. The reason is I got it from here. it said¬†“even if a ¬†connection is made, software like Gaim and Firefox will refuse to connect because according to them there is no connection. Apparently they use libnm to ask NetworkManager if the system is on-line or not”.

But we still can use Opera or Konqueror, even this is annoying since Firefox is one of the best browser.

Updated 11-01-2010:
Ok, what we need to is just untick Work Offline menu (File –> Work Offline). Apparently firefox just make the browser to work offline if NetworkManager is not in useūüôā . This is firefox bug by the way.

Ref:
https://bugzilla.mozilla.org/show_bug.cgi?id=424626

Detecting Device
After we plug the modem to PC/Laptop use, we can use the command like “dmesg | grep modem” or “dmesg | grep HUAWEI” to see if our modem is detected.

if you get output something like above picture, then you got your modem detected.

NetworkManager
The simplest and reliable way to make ppp connection using Huawei E220 modem is NetworkManager (KNetworkManager in KDE). Here is the step:

1. Klik on KNetworkManager Icon then New connection … –> ttyUSB0

2. Insert Username, Password, Number, and APN from your operator/provider. In my case I only insert those field. Click Next.

3. Just click Next

4. Set Baud rate to 9600. Click Next.

5. Just click Next

6. Set the Connection Name, you can use any name. I’m just using my operator name. You can click “Connect & Save” to start connecting or just click “Save” to save the configuration.

7. To connect, click on KNetworkManager Icon then the name of the connection. After succesfull connection, you’ll see the KNetworkManager icon is changed.

WvDial
WvDial is a Point-to-Point Protocol dialer, it’s console based. Wvdial is used as a connection by Vodafone Mobile Connect, smpppd, KInternet. WvDial save and loads its configuration from /etc/wvdial.conf. To get a best practice using wvdial we can start by delete default /etc/wvdial.conf (e.g “rm /etc/wvdial.conf”¬† in openSuse as root, or “sudo rm /etc/wvdial.conf” in Ubuntu).

Open a console and now let’s see how wvdial detect our modem. To configure wvdial we need to do it as root. Here is the step:

1. Type “wvdialconf /etc/wvdial.conf“, then ENTER. The output is like ¬†picture below.

Here is the content of generated /etc/wvdial.conf:
[Dialer Defaults]
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = Analog Modem
; Phone =
ISDN = 0
; Username =
Init1 = ATZ
; Password =
Modem = /dev/ttyUSB0
Baud = 9600

2. Here is my /etc/wvdial.conf:
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1, "IP", "AXIS"
Modem = /dev/ttyUSB0
Phone = *99#
Idle Seconds = 300
Modem Type = Analog Modem
Stupid Mode = 1
Compuserve = 0
Baud = 9600
Auto DNS = 1
Dial Command = ATDT
Ask Password = 0
ISDN = 0
Password = 123456
Username = axis

Just like using NetworkManager we need to provide Username, Password, Phone Number, and APN. APN is set in Init3 parameter using AT+CGDCONT command, replace it with APN from your provider only if your connection need APN, otherwise we can remove Init3 string. Note on modem, change it if your device mapping is not set to /dev/ttyUSB0.

3. To establish a connection type¬†“wvdial” in the console, the output will likely like this:

Output above is on Ubuntu, it resolve correct DNS, so you can begin browsing. Output below is when it set wrong DNS on openSuse, need to edit /etc/resolv.conf manually.

note: For the screenshoot i’m using Ubuntu 9.04, still in the same machine. ¬†Somehow my openSuse 11 always set the wrong DNS after wvdial command. Maybe you wont find this problem. So just remove the sudo command in openSuse.

UMTSmon
Get umtsmon from here, here, or openSuse one click install file. To get umtsmon detect our huawei e220 modem, we need huawei_umtsmodem package, get it from here or this one click install file. Install both files and it should detect our modem.

1. Open umtsmon, it will analyze the modem, detect card and signal strength.

2. Click menu Connection –> Manage Profiles …, it should be empty on Manage Profiles box in the first run. Press Add profile… button.

3. Type Profile Name, then press Create button.

4. Set the APN, username, and password. Press Save button.

5. New profile is now in the list. Close the Manage Profile box.

6. Click menu Connection –> Connect or press Connect button. Set the profile we want to activate, then press OK button.

7. Umtsmon is setting up internet connection.

8. Successfull connection will look something like this.

Ref:
http://en.opensuse.org/Umtsmon
http://en.opensuse.org/Huawei_UMTS_USB_Stick

Vodafone Mobile Connect Card Driver for Linux
Get vodafone mobile connect card driver for linux and it’s dependencies from here.

1. Open¬†vodafone mobile connect card driver for linux, then create new profile using menu Tools –> Profiles –> New Profile

2. Set Profile Name, username, password, and APN.

Check Use static DNS checkbox, if you want to set DNS manually. Then press OK button.

3. Click the Connect button to establish a connection.

4. Successfull connection shown like picture below.

Ref:
http://www.howtoforge.com/vodafone_mobile_connect_card_driver_linux
http://www.vodafonebetavine.net/bvcms/documents/vmc/installation-en.pdf

New connection …