“Hello, World” Web Application using Spring MVC in NetBeans IDE 6.7

4

If you are looking to learn web application development with Spring Web MVC framework NetBeans is probably the best IDE to get started with. You don’t have to worry about which framework library files to download, where to copy them etc. and moreover some of the basic configuration is automatically created as part of project template creation wizard. Below is an example “Hello, World” web application created using Spring Web MVC framework in NetBeans IDE.

  1. Start NetBeans IDE and create a new project File -> New Project…
    Select “Java Web” in Categories section and “Web Application” in Projects section and click Next.

    Enter a name for the project in “Project Name:” field(say “HelloWorld”) and click Next.
    Change any of the server settings(like selecting a different web server instead of the default Glassfish) if you want to, otherwise just click Next. Finally, make sure that you check the “Spring Web MVC 2.5″ checkbox and any additional frameworks you plan to use for this web application(like Hibernate) and press Finish.

  2. NetBeans does a nice job of creating a template Spring Web MVC project for you by copying the required spring jar files in web application build directory, generating configuration files with sample content etc. You can see that web.xml has the required configuration lines already. Also have a look at dispatcher-servlet.xml file to see handler mapping bean and view resolver bean already declared. (Netbeans should have it opened automatically; if not, open it from HelloWorld -> Web Pages -> WEB-INF.) Run the application to see what the sample project currently does.

Now let us add our own code to the application.

  1. First step is to create the controller class. Right-click on Source Packages and select New -> Java Class. Enter HelloController in the “Class Name:” field and hello in “Package:” field. Click Finish.

    Enter the following code in the HelloController.java file.
    [java]
    package hello;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;

    public class HelloController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    String message = “Hello, World!”;
    return new ModelAndView(“response”, “msg”, message);
    }
    }
    [/java]

  2. Open dispatcher-servlet.xml and add a mapping from hello.htm URI to HelloController controller class as shown below.
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"></p> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> <prop key="hello.htm">helloController</prop> </props> </property> </bean></p> <p> <bean id="helloController" class="hello.HelloController" />
  3. Time to create the response page. Right-click on jsp directory(Web Pages->WEB-INF->jsp) and create a new JSP file. Since we are returning “response” as the view name from our controller class, enter “response” in the “JSP File Name:” field. Click Finish.
    Now open the response.jsp page we just created and enter the following code in it.
    <br /> <body></p> <h1>Hello World!</h1> <p> Message: ${msg}<br /> </body>
  4. Run the application and change the request uri in the browser address bar to http://localhost:8080/HelloWorld/hello.htm and press Enter. You should see the following response page.

NetBeans IDE saves a bit of time by generating a configured Spring Web MVC project for us to start with. With most other popular IDEs, we need to install the required library files and create the basic configuration files manually. I will describe the procedure using Eclipse IDE in my next post.

Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Ubuntu GNU/Linux

2

Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5 on Ubuntu GNU/Linux

First go to System -> Administration -> Software Sources and make sure that the multiverse and universe Ubuntu repositories are enabled on your system.

Installing Java SE 6, Apache Maven 2 and Apache Tomcat 5.5 software is just one simple command on Ubuntu family of GNU/Linux operating systems:

# sudo aptitude install sun-java6-jdk tomcat5.5 jetty maven2

I recommend setting JAVA_HOME environment variable to point to the JDK installation directory in your profile file(e.g. .bash_profile or /etc/profile):

export JAVA_HOME /usr/lib/jvm/java-6-sun

The above command also installs the jetty web server which is especially good for development purposes.

“Hello, World” Java Web Application using Java SE 6 + Tomcat 5.5 + Maven 2

2

See my previous posts to install Sun Java SE 6, Apache Tomcat 5.5/6, Apache Maven 2 on Windows and Ubuntu GNU/Linux operating systems:

Once all the required software components are installed, simply run the following command in the command prompt/shell to generate a basic Java web application project in the current working directory.

# mvn archetype:create

You can also run ‘mvn archetype:generate’ if you want to generate the project in interactive mode. The command will then prompt you for relevant information when creating the project.

# mvn archetype:generate
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘archetype’.
[INFO] org.apache.maven.plugins: checking for updates from central
[more output]

After all the necessary files are downloaded, you will be shown a list of supported archetypes and will be prompted to select the one that you want to generate.

[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
[more options]
42: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
43: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
44: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44) 15: :

Enter the appropriate number to select the archetype that you want to generate; for example, enter 18 to create a basic Java Web Application project. Next you will be prompted to enter values for groupId(say ‘hello’), artifactId(say ‘HelloWorld’), version(you can accept the default and just press the ENTER key) and package(say ‘war’). You will then be asked for confirmation; type Y and press the ENTER key. For more information on what the above fields mean, read about Maven Co-ordinates.

To learn more about Maven’s archetype plugin, read Maven 2 Archetype plugin usage page.

You can run the basic Java web application project created above in Tomcat web server by running the following command.

# mvn tomcat:run
[lot of output]
[INFO] Starting tomcat server
[INFO] Starting Servlet Engine: Apache Tomcat/5.5.15
[INFO] XML validation disabled
[INFO] Initializing Coyote HTTP/1.1 on http-8080
[INFO] Starting Coyote HTTP/1.1 on http-8080

(If this is the first time you are running the above command, it downloads a lot of necessary files and stores them in the local Maven repository. This is a one-time operation.) You can now access the web application from a location like “http://localhost:8080/HelloWorld/”. ‘HelloWorld’ is the artifactId that we used when creating the sample application. You can read more about running and deploying applications from Maven to Tomcat web server on Maven Tomcat plugin page.

If Jetty web server is installed on your computer, you can also run your web application in Jetty by running the following command:

# mvn jetty:run

How easy it is to switch the container when you are developing Java web applications with the Maven build tool! You can read more about running and deploying applications from Maven to Jetty web server on Maven 2 Jetty Plugin page.

“Hello, World” Web Application in Ruby on Rails using console

4

Installation and setup instructions of Ruby on Rails web framework on different operating systems is covered in the following posts:

  1. Setting Up Rails Development Environment on Windows Vista/XP
  2. Setting Up Rails Development Environment on Ubuntu GNU/Linux
  3. Setting Up Rails Development Environment on Fedora GNU/Linux

If you would rather use an IDE to develop Ruby on Rails applications, Aptana IDE is covered in ““Hello, World” Web Application in Ruby on Rails using Aptana Studio.”

This post describes how to create a basic “Hello, World” web application in Rails using only console tools and a text editor. The instructions work pretty much the same for all operating systems with little to no modifications.

Make sure that you have the latest versions of all components installed:

# ruby -v && gem -v && rails -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]
1.3.1
Rails 2.2.2

(Output text might be slightly different on Windows OS but the version numbers should be the same.)

To create a new rails project, run the following command which generates the required directory structure for a rails application:

# rails hello
# cd hello
# ls
app db lib public README test vendor
config doc log Rakefile script tmp

The purpose of each directory generated is more or less self-explanatory, like the test directory is for storing test files and the log directory contains various log files.

Rails application the scaffold generator way

Scaffolding allows us to generate template code necessary to directly run our application and play with it without having to write a single line of code. We can also open the generated files to take a peek at the generated code. We can then modify this code to our liking or just throw it away and write everything manually once we are satisfied with the prototype. The scaffolding code can be generated, say for a resource called ‘person’, by running the following command:

# ruby script/generate scaffold person name:string password:string email:string age:integer

This command generates(among other files) a Rails migration file in db/xxx_create_people.rb which is responsible to create the database schema for our application; a model in app/models/person.rb; a controller in app/controllers/people_controller.rb and related view files in app/views/people directory. (‘people’ is used wherever plural of ‘person’ is needed.) Have a look at the code generated in the migration file:
[ruby]
create_table :people do |t|
t.string :name
t.string :password
t.string :email
t.integer :age

t.timestamps
end
[/ruby]

Run the following commands to initialize the database:

# rake db:create:all
# rake db:migrate

Now we are ready to run the application and use it. Start the WEBrick web server:

# ruby script/server

Open your favourite web browser and go to http://localhost:3000/persons url. You should see a page similar to the following screenshot(after I added couple of entries using the “New Person” link):

Also make sure you look at the code generated in the controller file and the various view files!
Read the official Getting Started with Rails guide.
Read more Rails articles on this blog.

Setting Up Rails Development Environment on Fedora GNU/Linux

1

This is an adaptation to Fedora GNU/Linux platform of my previous posts meant for the Windows & Ubuntu platforms: Setting Up Rails Development Environment on Windows Vista/XP and Setting Up Rails Development Environment on Ubuntu GNU/Linux.

I can think of two different ways in which you might want to set up a development environment for Ruby on Rails on a GNU/Linux machine. One is to download everything outside of the package manager of your distribution i.e. build everything from the source. I am going to cover the second, easier way: how to setup Rails on a Fedora machine using its package manager for the most part. I show below how to setup Ruby, RubyGems, Rails, Mongrel, MySQL, and Git as part of the development stack.

  1. Installing Ruby:

    Install the Ruby and related packages as root user:

    # yum install ruby ruby-docs ruby-irb ruby-ri ruby-sqlite3
  2. Installing Rubygems

    Though it is possible to install Rubygems using the Fedora package manager, not only will you get an old version of rubygems if you do so, it also may not be compatible with the latest version of rails. I strongly recommend you download the source code package of the latest version of rubygems and install it using its setup script:

    # cd $HOME
    # wget http://rubyforge.org/frs/download.php/45906/rubygems-1.3.1.zip
    # tar xvzf rubygems-1.3.1.tgz && cd rubygems-1.3.1
    # ruby setup.rb

    Remember to download the latest version of rubygems available(currently 1.3.1). You can remove the downloaded tgz archive and the extracted directory after the installation is finished.

    #cd $HOME
    # rm -r rubygems-1.3.1.tgz rubygems-1.3.1
  3. Installing Rails

    Update all the gems(not really needed) and then install the rails gem.

    # gem update –&ndashsystem
    # gem install rails

    To update rails in future, run ‘gem update rails’ command.

    Check the versions to confirm installation:

    # ruby -v
    ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]
    # gem -v
    1.3.1
    # rails -v
    Rails 2.2.2
  4. The basic Rails development environment is now installed on your system and you can skip the rest of the post if you are happy with WEBrick as the web server, SQLite as the database server and your favourite text editor/IDE that may be already installed on your system. The following instructions cover installing an alternate web server called Mongrel and MySQL database server and its GUI tools.

  5. Installing Mongrel

    Installing the Rails gem also installs the WEBrick web server which is ideally suited for development purposes. Another much recommended web server for Rails development as well as production environment is the Mongrel web server. To install the Mongrel web server, run the following gem command:

    # yum install mongrel

    After installing Mongrel, Rails automatically starts Mongrel instead of WEBrick web server when you run Rails applications in development mode. (Refer to Mongrel documentation to know more about running Rails applications under Mongrel in production mode.)

  6. Installing MySQL

    This step covers the installation of MySQL database server and its GUI tools. Skip this step if you want to use some other database server. Run the following command to install the required mysql packages:

    # yum install mysql-server mysql-administrator mysql-gui-tools

    One important step during the configuration process is to select a root password(you will need to enter it in the database.yml configuration file of your Rails application).

  7. Source Control

    To setup your rails application using git source code management tool, see my other post:
    Setting Up Ruby on Rails Projects with Git and Github

    You can also install cvs or subversion or other version control software using yum package manager.

    # yum install cvs subversion

Now you can create/checkout Rails applications, edit the files using your favourite text editor/IDE and run it under Mongrel. In future, I will try to write about developing Rails applications using integrated development environments like Eclipse and NetBeans.

buy genuine Zovirax online purchase prednisone prescription online prednisone no prior script prednisone no rx needed order prednisone usa cod buy prednisone without rx from us pharmacy buy prednisone online without rx purchase prednisone without prescription order prednisone saturday delivery buy prednisone amex online without prescription ordering prednisone over the counter buy prednisone amex online without rx ordering Paxil over the counter order Paxil without rx needed order Paxil pharmacy Paxil overnight delivery fed ex ordering Paxil without a script order Paxil order amex order Paxil amex online without prescription buy Paxil pay pal without prescription order rx free Paxil cheapest Paxil available online purchase Paxil without purchase Paxil without prescription how to order Paxil online without a prescription purchase Paxil cod next day delivery buy discount Paxil online buy Paxil no visa online without rx prednisone cheap overnight fedex Zovirax canadian pharmacy Zovirax cheap overnight fedex purchase Strattera usa cod where to buy accutane buy frontpage software cheap mortgage application software buy autocad software best price for final draft 7 software iphone software download plagiarism software to buy for parents Crestor side effects buy masterwriter software discount bmi sesac microsoft software downloads wm5 software download treo software download cheap discount software xxasdf discount trellian software best antivirus software download cheap microsoft word software buy software for mac software discounts for educators microsoft office buy windows xp oem software buy flash cs3 software photoshop cs 5 full fashion conference italy buy Orlistat with amex ordering Lasix over the counter purchase online prescription Lasix without windows 7 product key purchase online cheapest xenical available online prescription xenical online online Maxalt achat Maxalt Zithromax uk sales where to buy Valtrex by cod where to purchase generic valtrex online without a prescription quality generic valtrex cheap valtrex usa (no prescriptions needed for Buspar|buy Buspar with no prescription|online pharmacies Buspar|Buspar cheap|buy Buspar without rx|purchase rx Buspar without|Buspar purchase online|purchase Buspar online without rx|purchase Buspar free consultation|buy Buspar Online|buy Buspar american express|buy Buspar Online|buy cheap Buspar with dr. prescription|Buspar side effects|fedex Buspar without priscription|overnight Buspar without a rx|order cheap overnight Buspar|Buspar toronto|uk order Buspar|Buspar no doctors prescription|Buspar mexico|Buspar order|no prescription Buspar with fedex|order generic Buspar|buy Buspar without rx from us pharmacy|prezzo Buspar|Buspar 10mg|Buspar from canada|purchasing Buspar without a script|buy Buspar australia|purchase Buspar visa without prescription|online purchase Buspar|buy Buspar no perscription cod|buy Buspar drugs|buy Buspar with visa|buy Buspar without rx needed|buy Buspar without prescription|buy Buspar no prescription low cost|purchase purchase Buspar no prescription cheap buy cheap Nolvadex cod buy Nolvadex infertility in internet visa at Wisconsin Ontario omicrosoft office 2003 megaupload online purchase Lasix Buy genuine accutane online buy Cytotec online no rx Cytotec online buy nolvadex amex online where to buy synthroid in germany where to buy synthroid pills cheap discount Nolvadex overnight Accutane 20mg mexico order Lasix usa Lasix overnight delivery fed ex buy zithromax overnight purchase online prescription zithromax without buy Nolvadex cheap cheapest Nolvadex available online buy Nolvadex without rx from us pharmacy buy Nolvadex without a prescription overnight delivery buy 20mg Nolvadex free shipping buy Nolvadex no prescription order zithromax overnight delivery buy zithromax 100 mg visa Valtrex without doctor prescription buying zithromax over the counter order zithromax with no prescription order zithromax 250mg mastercard order zithromax 250 mg visa order zithromax 500mg amex buy mail order Orlistat buy Valtrex online now Orlistat tablets purchase cheap Maxalt onlinebuy generic Maxalt pills free fedex delivery Buspar buy cheap Finpecia under without rx purchase online finpecia without rx finpecia fedex no prescription buy line finpecia generic Crestor uk Buspar u.p.s shipping cod order cheap overnight Buspar free fedex delivery prednisone prednisone no script fedex buy valtrex cheap without prescription purchase Zithromax without purchase Zithromax no scams Arimidex delivered overnight order cheap overnight Arimidex want to buy Flomax in malaysia buy cheap Flomax without prescription purchase Cytotec over the counter fedex buy cheap Cytotec online free consult buy Cytotec online overnight adobe financial results microsoft office professional 2007 upgrade download microsoft office professional 2007 cheap Buspar by money order how to buy Valtrex without a prescription cheap valtrex uk purchase Proscar usa cod adobe lm service adobe after effects simple creativity latest adobe flash download buy Crestor amex measurement software Photoshop For Sale buy discount Tamsulosin line Autocad 2010 Review download adobe premiere complete removal of office 2003 adobe indesign cs2 photoshop lightroom 3 cheap where can i buy herbal Crestor where to buy Crestor buy Crestor online cod Buy Fincar online 5 mg mastercard cheapest price for microsoft office vio software discount coupon purchase Orlistat amex online without prescription buy maxalt online without a prescription adobe photoshop 6 cd key Cheap Software Buy Downloads For Photoshop flash catalyst sql ms office 2010 enterprise windows 7 upgrade oem Windows Server 2003 Standard Edition Downloads For Windows 7 Ultimate Buy cheap Valtrex without a perscription application free download office microcoft2007 buy Crestor drugs Valtrex no prescription purchase Valtrex amex online without prescription Xenical Orlistat Flomax buy on line boilsoft viseo create new instrument logic studio 9 cheap purchase finpecia buy Strattera no prescription low cost Flomax best buy buy cheap oem software formica countertop cyberlink power dvd 9 download buy Prednisone no prescriptions cheap indesign cs android acdsee buy Flomax australia generic Valtrex online lightroom license .txt download de driver para corel windvd buy microsoft office online cheap generic Valtrex online buy Flomax online no prescription buy pharmacy Flomax waterview ifinance price price of valtrex buy valtrex doctor prescription buy generic Crestor order generic Tamsulosin imtoo dvd ripper serial look like buy Buspar without a rx cheap digital video want to buy Buspar in malaysia buy Crestor now buy Valtrex without a rx purchase rx Valacyclovir without Bupropion buy Bupropion buy no perscription Amitriptyline buy discount Zithromax cheapest Zithromax available online how to order Buspar online without a rx purchase prednisone cod next day delivery where can i purchase prednisone online prednisone online cash on delivery microsoft office 2010 3 user license cheap mac desktops no prescription Orlistat cod delivery Work Order Tracking System Acrobat 6.0 Professional Oem Version Windows 7 buy Valtrex where order Orlistat no rx cheap proffes Microsoft Windows Xp Purchase buy Valtrex legally buy cheap Proscar without prescription Windows Xp Server buy cheap online pharmacy Buspar buy Prednisone online now generic Zithromax usa accutane 40 mg delivered overnight buy accutane 40 mg with no prescription media 8 oline cheap cs software Discount Microsoft WindowsLightroom 2 Windows 7Ms Office StandardPhotoshop Cs5 UpgradeComputer Monitors For SaleWindows Xp InstallSuite Microsoft OfficeAutocad Version 2007Adobe Acrobat 9.0 Standard DownloadIe8 Download For Windows 7Adobe Paint ShopMicrosoft Service Pack 2Free Download Adobe AcrobatStudent And Teacher EditionManage ImageAdobe Acrobat 7 Pro DownloadVista Home Premium To Windows 7 UltimateWindows 7 Home Premium Upgrade OemAdobe Creative Suite 5 Master Collection Student And Teacher EditionBuy Adobe Photoshop Lightroom 3Ms Office 2010 Home And StudentCompare Photo SoftwareMicrosoft Office Word Viewer 2010Windows 7 Upgrade Student Discount ProfessionalWindows 7 Updates DownloadCreative Suite WebAdobe Reader VistaMicrosoft Windows 7 Home Premium Upgrade 64 BitPhotoshop 2Ie8 Download For Windows 7Photoshop 2009Suite Microsoft OfficeMicrosoft Office 2007 VersionUpgrade Windows Vista To 7Autocad Lt 2010Autocad 2010 Best PriceDownload Acrobat Reader 8 buy Flomax online us pharmacy cheapest Buspar available online where can i purchase Buspar without a prescription sony oem software Valtrex from india Maxalt pharmacy Prednisone prescription order buy Crestor overnight Buspar shipped by cash on delivery purchase Proscar online with overnight delivery prezzo Valtrex buy Valtrex shipped cod Proscar no prescription overnight Proscar overnight no consult where to purchase cheap Cytotec no rx Cytotec to buy canadian prescriptions prednisone where to buy Prednisone online Prednisone buy Prednisone Proscar online no rx overnight where can i buy Valtrex online without a prescription buy valtrex legally order Valtrex without a rx overnight shipping c.o.d prednisone purchase Prednisone money purchase is it legal to buy Cytotec online in australia where to buy generic Crestor online without a rx buy Cytotec cheapest Cytotec without rx medications buy Zithromax on line buy rx Maxalt without Buy Finpecia 1 mg online microsoft onenote and educator discount window mirror film charset windows 1252 reinstal windows finpecia overdose excel new window windows for loop microsoft office 2008 for mac home student cheap collection software virtual dj software web design software mac cheap or discount photoshop software load windows xp buy Orlistat legally Rosuvastatin generic order buspar pharmacy dreamweaver cs4 windows mobile opera adobe photoshop price epson creativity suite
Go to Top