Generating Menu Tree Or Catagory Tree hierarchy

There has always been a basic requirements in website or web applications that the menu bar must be dynamic and all menu items should be appeared under their respected parent. Here, if you are managing the menu items in database, then you have to retrieve all menu items in the way that each menu item must have its proper position in menu tree. here you could use just a loop because you don't know that how many menu times are going to be added in future.

So, Here is the example in which I am using the recursion approach for creating the menu tree dynamically for any level. there is the database table "menu_items" having the columns id, name and parent. Parent column will have the id of the parent menu as foreign key.

You can increase some more columns as per your requirements like label, url and so on. There are some values already there for testing purpose.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CreateMenuTree {
 Connection con = null;

 String menuTreeString="";
 String intendation="\t";
 CreateMenuTree() throws ClassNotFoundException, SQLException {
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
 public void createMenuItem(int parentMenuId) throws SQLException{
  ResultSet resultSet0 = con.createStatement().executeQuery("select count(*) from menu_items where parent="+parentMenuId);
  ResultSet resultSet = con.createStatement().executeQuery("select * from menu_items where parent="+parentMenuId);
   menuTreeString=menuTreeString+"\n<li><a href=\"#\">"+resultSet.getString(2)+"</a>";
   int currentMenuId=resultSet.getInt(1);
 public static void main(String[] args) throws ClassNotFoundException, SQLException {
  CreateMenuTree c=new CreateMenuTree();

The above code is working to generate the menu tree. It will generate HTML structure for the menu tree i.e. ul and li based tree structure.

Getting started with Restful web service in java using jersey

Creating Restful web services in java is not a difficult task. You just use some annotation to expose you methods as web service end points. All annotations those turn your method into web services are in package. To host you web service, you can use tomcat or any web server. One thing you need to do is, configure the jersey ServletContainer servlet in web.xml for the particular url pattern.

See the following example depicting the calculator service.

package pack;



public class CalculatorResource {
    public String getDate() {
        return "Server date:" + new java.util.Date();
    public String add(@PathParam("x")int x,@PathParam("y")int y){
        return "<result>\n" +
                "     <first-value>"+x+"</first-value>\n" +
                "     <second-value>"+y+"</second-value>\n" +
                "     <sum>"+(x+y)+"</sum>\n" +
    public String add2(@PathParam("x")int x,@PathParam("y")int y){
        return "<result>\n" +
                "     <first-value>"+x+"</first-value>\n" +
                "     <second-value>"+y+"</second-value>\n" +
                "     <sum>"+(x+y)+"</sum>\n" +

jersey ServletContainer servlet config in web.xml


Overview of Spring Framework

Spring framework is the platform which supports for developing java applications using its infrastructure (Physical and organizational structures). Spring allows you to develop application with simple plan old java objects (POJO). Spring takes care of managing objects and their lifecycle. This framework provides the stage for your objects to play their role and all dependencies the objects depend on are provided at that time whenever required.

Spring framework integrates your objects to create an application and make them independent from each other. Spring connects all objects being maintained by it by injecting their reference in each other as per requirements.

Spring able to manage various common requirements for your application like database connection, transactions, emailing, orm, aop etc.

For example, if you imagine a calculator application which has the following objects to accomplish its functionality:
  • Keyboards
  • Display
  • Adder
  • Subs tractor
  • Divider
  • Multiplier
  • Calculator
Then spring assembles all objects as follows to make the calculator. All objects are managed by spring and connect to each other to make them working.

Architectural advantages of using frameworks

Indirectly, spring helps you to follow some architecture and design principles of software engineering. Spring does not allow you to bind objects yourself however it provides the structured ways to bind the object to make something understandable. Software design is the important aspects of your project and spring forces you to follow the design patterns and architecture patterns.

Without following the architecture and design pattern, your application may look like a circuit of something which is working fine but you cannot repair it easily. But spring arranges components of your application that looks like the circuit which can be repaired without any hassle.

This is the TV circuit and you can see how difficult to understand of connectivity of the different components(Ref:


 This is also a TV circuit board but having organized way of components (Ref:

Spring MVC - File upload Example

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:mvc=""

    <mvc:annotation-driven />
    <context:component-scan base-package="myproject" />

        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />

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

Controller that handle file uploading
package myproject.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

public class MyController {
    public String home(){
        return "Home";
    @RequestMapping(value="/upload", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file,Model model) throws IOException{
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();//here you can write bytes to any file
            model.addAttribute("ContentType", file.getContentType());
            model.addAttribute("name", file.getName());
            model.addAttribute("OriginalName", file.getOriginalFilename());
            model.addAttribute("size", file.getSize());
            file.transferTo(new File("F:/temp/"+file.getOriginalFilename()));
            return "FileUploaded";
        }else {
            return "Home";

Form from where we upload the file
<title>Spring MVC - File Uploading Example</title>
    <h1>Spring MVC - File Uploading Example</h1>
    <form method="post" action="upload" enctype="multipart/form-data">
        <input type="file" name="file" />
        <input type="submit" />
Result page 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     <%@taglib prefix="c" uri="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<h1>File Uploaded</h1>
<h2>Name: ${name}</h2>
<h2>OriginalName: ${OriginalName}</h2>
<h2>ContentType: ${ContentType}</h2>
<h2>Size: ${size} bytes</h2>
List of Jar files 

Popular Posts