Posts

Tomcat Introduction and Purpose of Different Folders

Image
If you are planning to develop web application in Java. The Java EE technology used to develop web applications is Servlets. Servlets are java classes that process the web requests and deliver response to browser. Putting these classes directly in just JVM is not possible, nor it would start processing web requests. We need some server to receive the HTTP requests (HTTP is the protocol the browser and server talks in), convert it into an object oriented representation (from stream of bytes written on server socket into objects understandable by our java classes i.e. Servlet) and pass the request to our servlets. So that we don't have to deal with lower level details of network programming like programming HTTP protocol to receive the requests and sending response to user browser. But we could focus on building business logic of our projects. Tomcat Tomcat is a server that receive web requests over the network, converts them into refined object oriented representation and dispatch t...

How Do Packages Work In Java

Packages are used to store related classes in one folder for better code maintenance and reusability. Here are some details that you need to understand to create and use packages in Java: Why we need packages What is package hierarchy How to place class in a package and compile it How to use classes placed in other packages How to compile to place .class files separate from source files What is NoClassDefFoundError How to use classpath to link classes placed at other locations and libraries Lets see each section one by one: Why we need packages When we start learning Java, we usually place different classses in same folder but real Java applications has tens or even thousands of classes, as the project size increase, so the number of classes. Most software consists of multiple modules and each module may consists of multiple classes. If we place all classes in one folder, it become difficult to locate a particular piece of code or a class that provide a specific fun...

Encapsulation in Java

Consider a case that can help you better understand the concept of encapsulation. Assume there is a class Student with 'id' and 'name' attributes. We want to enforce, the student id must be greater than 0. Assume another class StudentTest that creates a Student object, initilize its attributes and print the initilalized values. See the code sample here: public class Student { int id; String name; } public class StudentTest { public static void main(String[] args) { Student student = new Student(); // object instantiation // Initialize instance attributes student.id = -100; student.name = "Alice23"; // Lets just print the instance attribute values System.out.println("ID : " + student.id); System.out.println("Name : " + student.name); } } You see, we set the id to -100 and name to Alice23. Both attributes values are incorrect. But if we compile the code and run...

Access Modifiers in Java

When we define attributes or methods in a class, we can access those attributes and methods by creating that class object. If the method is static, it can be accessed using class name. For example if there is a class named Student, we can create its object and access its methods, lets see the code sample. public class Student { int id; String name; void registerCourse(String courseCode){ System.out.print("registerCourse called with code: " + courseCode); } } public class StudentTest { public static void main(String[] args) { Student student = new Student(); student.id = 1; student.name = "Alice"; System.out.println("ID : " + student.id); System.out.println("Name : " + student.name); student.registerCourse("CSC245"); } } In StudentTest class, we have created the Student object and accessed id and name attribute using the object name. We named the ob...

How to Redirect in Spring MVC

We often need to redirect our request to another controller. Its very easy to do in Spring MVC. In below code sample, I have created a controller method i.e. list which is mapper to context/users/ URL. It just displays the list of users. There is another method i.e. save, it process the POST request and used to save data to database. I have not actually saved the data into DB because that is not our object in this post. We want to see, when save controller is called, and we have done with saving data into DB, how we can redirect our request to /users/ controller from /save/ controller method. Its very simple, below I share the code sample I have created to demonstrate the Spring MVC redirect: package com.springpoc.controllers; import com.springpoc.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.spring...

How to Install and Make First Laravel App on Windows

Image
I use Wamp on my Windows box for developing PHP based apps, I recently installed Laravel on my Windows box to test how to framework works and performs common tasks related to web development. I would keep posting good stuff I learned about Laravel in easy to follow steps for new developers of Laravel like me. The Steps I followed are following: Laravel used Composer for dependency management, so first I installed Composer for Windows. Like to download is: https://getcomposer.org/Composer-Setup.exe During the Composer installation, it would ask for the location of PHP.exe, so make sure you have installed PHP on your system before you install Composer. It would download a few things from internet, so wait with patience. It uses SSL, so make sure SSL module is enabled in your php.ini file, just remove the ";" before the like: "extension=php_openssl.dll". The php.ini file is located at your php's installation home folder. The installation would also set the composer...

SQL Query To Append Data

If there exist some record already, and you need to append a string with an existing value of a field. You can use CONCAT function for it. Assume we have a table named students which has following record before we execute the query: mysql> select * from students; +----+----------------------+------------------+------------+ | id | name | email | phone | +----+----------------------+------------------+------------+ | 1 | Alice | alice@gmail.com | 5487845784 | | 2 | Bob Chris | bob@gmail.com | 879874578 | | 3 | Oracle | oracle@gmail.com | 548798453 | +----+----------------------+------------------+------------+ To concat a string with name Alice, record id 1, you can execute following query: update students set name = CONCAT(name, " Lewis") where id=1 After the query, the record would look like this: mysql> select * from students; +----+-------------+------------------+------------+ | id | na...