Learning The "J"

Home » JAVA Learning » JAVA EE

Category Archives: JAVA EE

HTTP Basic access authentication

Basic access authentication is a method for a HTTP user agent to provide a user name and password when making a request.

HTTP Basic authentication implementation is one of the easiest ways to secure web pages because it doesn’t require cookies, session handling, or the development of login pages. Rather, HTTP Basic authentication uses static headers which means that no handshakes have to be done in anticipation. Although the scheme is easily implemented, it relies on the assumption that the connection between the client and server computers is secure and can be trusted. Specifically, if SSL/TLS is not used, then the credentials are passed as plaintext and could be intercepted.

Server side Protocol

When the server wants the user agent to authenticate itself towards the server, it can send a request for authentication.
This request should be sent using the HTTP 401 Not Authorized response code containing a WWW-Authenticate HTTP header.

The WWW-Authenticate header for basic authentication (used most often) is constructed as following:

WWW-Authenticate: Basic realm=”insert realm”

Client side Protocol

When the user agent wants to send the server authentication credentials it may use the Authorization header.

The Authorization header is constructed as follows:

Username and password are combined into a string “username:password”
The resulting string literal is then encoded using Base64
The authorization method and a space i.e. “Basic ” is then put before the encoded string.

For example, if the user agent uses ‘Aladdin’ as the username and ‘open sesame’ as the password then the header is formed as follows:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Here is a full working code in context of Restfull Web Services

Understanding MVC

package com.awais.mvc.model;

public class Calculator {
//add numbers. 
    public int add(inta, intb) {
        return a + b;
// multiply numbers
    public int multiply(inta, intb) {
        return a * b;

package com.awais.mvc.controller;

import com.awais.mvc.model.Calculator;

public class CalculatorController {

    private Calculator calculator = new Calculator();
    private intfirstNumber  = 0;
    private intresult  = 0;
    private intsecondNumber  = 0;

    public CalculatorController() {

    public void setCalculator(CalculatoraCalculator) {
        this.calculator = aCalculator;

    public void setFirstNumber(intaFirstNumber) {
        this.firstNumber = aFirstNumber;

    public int getFirstNumber() {
        return firstNumber;

    public int getResult() {
        return result;

    public void setSecondNumber(intaSecondNumber) {
        this.secondNumber = aSecondNumber;

    public intgetSecondNumber() {
        return secondNumber;

    public String add() {
        result = calculator.add(firstNumber, secondNumber);
        return "success";

    public String multiply() {
        result = calculator.multiply(firstNumber, secondNumber);
        return "success";

package com.awais.mvc.view;

import com.awais.mvc.model.CalculatorController

public class MyCalculator{
    public static void main(String ar[]){
        CalculatorController myCal=new CalculatorController();
        if (myCal.add().equalsIgnoreCase("success")){

Parsing JASON Using Google GSON API

package logtest;

import java.util.List;
import com.google.gson.Gson;

public class JasonParsing {

    public static void main(String... args) throws Exception {
        String json =
                + "'title': 'Computing and Information systems',"
                + "'id' : 1,"
                + "'children' : 'true',"
                    + "'groups' : [{"
                       + "'title' : 'Level one CIS',"
                       + "'id' : 2,"
                       + "'children' : 'true',"
                          + "'groups' : [{"
                             + "'title' : 'Intro To Computing and Internet',"
                             + "'id' : 3,"
                             + "'children': 'false',"
                             + "'groups':[]"
                          + "}]"
                    + "}]"
                + "}";

        // Now do the magic.
        Data data = new Gson().fromJson(json, Data.class);

        // Show it.
        System.out.print(data.title + "  ");        
        System.out.print(data.groups.get(0).title + "  ");        
        System.out.print(data.groups.get(0).groups.get(0).title +"  ");        
        System.out.print(data.groups.get(0).groups.get(0).id +"  ");

    class Data {

        private String title;
        private Long id;
        private Boolean children;
        private List<Data> groups;

        public String getTitle() {
            return title;

        public Long getId() {
            return id;

        public Boolean getChildren() {
            return children;

        public List<Data> getGroups() {
            return groups;

        public void setTitle(String title) {
            this.title = title;

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

        public void setChildren(Boolean children) {
            this.children = children;

        public void setGroups(List<Data> groups) {
            this.groups = groups;

        public String toString() {
            return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);