Friday, June 10, 2011

Demo Login example in Spring 3

Sample of Login application in Spring 3 with Annotation, Spring Annotation Example
Spring 3 support very good approach of annotation, you don't need to define each and every controller in xml. We can directly define name of the controller in the controller. Annotation has been used to define url in the controller.
Here is the sample of  login application in spring 3.
Application Name: Spring 3
For login Sample, we required following files:
index.jsp
Welcome to Main Page
<a href="LoginForm.bp">Login</a> 

Spring3-servlet.xml

<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="com.bp.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>


LoginForm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

Login Form

User Name:
Password:

LoginController.java
package com.bp.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bp.controller.domain.User;
 
@Controller
@RequestMapping("LoginForm.bp")//this mean all  url must be */User...
public class HelloWordController {
 
 
  @RequestMapping(method = RequestMethod.GET)
     public String showForm(Map model) 
  {
             User loginForm = new User();
             loginForm.setUsername("Biren");             
             model.put("loginForm", loginForm);
             return "LoginForm";
     }
  
  @RequestMapping(method = RequestMethod.POST)
     public String processForm(User loginForm, BindingResult result,Map model) 
  {
             String userName = "a";
             String password = "a";
             if (result.hasErrors()) {
                     return "LoginForm";
             }
             System.out.println(loginForm.getUsername());
           //  loginForm = (User) model.get("loginForm");
             System.out.println(loginForm.getUsername());
             if (!loginForm.getUsername().equals(userName)
                             || !loginForm.getPassword().equals(password)) {
              
             
              model.put("loginForm", loginForm);
                     return "LoginForm";
             }
             model.put("loginForm", loginForm);
             return "LoginSuccess";
     }
 
}

User.java
package com.bp.controller.domain;


public class User {

 String username;
 String password;
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 
}

Thursday, June 9, 2011

How to solve Address or Port already in use error in tomcat java


Tomcat startup failed: Address already in use: JVM_Bind
java.net.BindException: Address already in use:8080


If you are getting this exception & Tomcat server in not running, it mean tomcat server is already running in your computer using port 8080. You can check it from command prompt by typing netstat -an. To solve this problem, shut down your tomcat server 2-3 times an wait for few min (1-2 minutes). Then again start you tomcat server.

Wednesday, June 8, 2011

How to convert exponential value to normal decimal form in jsp/java

Problem to display double/large value in jsp/java? How to convert exponential value to normal decimal form in jsp/java?
Many of the time, when we try to display large decimal value in jsp, it display in the value in exponential form instead of normal.

Example: double value 11588200.0, but jsp display 1.15882E7
To display this value in normal decimal format, we can have small parser method separately or just format  as below
               DecimalFormat fmt = new DecimalFormat(value);
       String finalValue = fmt.format(d);


Now finalValue variable will have  11588200.00 value

Saturday, June 4, 2011

How to solve java.lang.ClassCastException?

java.lang.ClassCastException at...
You might get full exception stack trace of ClassCasteException. Before solving this problem we must know following information.

What is java.lang.ClassCastException ? Why do I get java.lang.ClassCastException?
This exception indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);

Another example is, suppose you have set ArrayList into the request Object in Action, and you are retrieving Map object as below:
List a = new ArrayList();a.add("Biren");
request.setAttribute("data",a); //in servlet/action you set object of List
Map data = (Map)request.getAttribute("data"); // in jsp, you tried to store List object into Map object.
 or, here in jsp, you are trying to get date which is list type  and assign to map object.

So, to solve this problem, you must go through the code and find what kind of object you are setting, and store the object into same type.
If you are using Java 5, you can define type of object while creating object.
example:
List info = new ArrayList();
If you try to store int value into info, it will give you error at compile time,



Example:
In Servlet/Action
  
List a = new ArrayList();a.add("Biren");
request.setAttribute("data",a); //in servlet/action you set object of List

Class Cast Exception in jsp/other class:
Map data = (Map)request.getAttribute("data");

Correct approach:
 List a =(List)request.getAttribute("data");






Exception in thread main java.lang.OutOfMemoryError: Java heap space in tomcat

How to solve java.lang.out of memory error? Why do I get  java.lang.out of memory error? "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"
While we develop web application in java and deploy into Apache Tomcat server, if we are playing with huge data then we might be getting java.lang.OutOfMemoryError most of time.
This is error, and can't be caught at development time, so at runtime we will get this error at any time when we try to create lots of variable more than available.
People might be thinking that their machine have memory of 2GB, then why are they getting memory error? You might have small confusion, if you have 2GB it does not mean you server is using whole 2GB ram for your application. By default Apache Tomcat use just 64K from whole memory of the system.

Then how to fix out of lang memory problem? How to increase heap size memory for tomcat server?
Here is the solution for java.lang.OutOfMemoryError Java heap space error:

1: Increase memory for apache tomcat server from the system:
Example to increase memory for tomcat server:
Go to tomcat main folder\bin, open catilina.bat file, add following line to set minimum 128 M to maximum 256 M
set CATALINA_OPTS=-Xms128M -Xmx256M -Djava.awt.headless=true

2: Refractor code and use StringBuilder instead of String.
Basically, writing lots of code in same method is not good practice. Declaring lots of String variables and appending text to same String variable use much more memory.
As we know when we append any text to same String variable, it actually create new variable in memory, you can look into the String documentation. So if you use StringBuilder, it won't create new variable when you append text to same variable, it append text to same variable in the memory instead.





Friday, June 3, 2011

Content Management System, CMS List in java

Now a days, Java Content Management System makes life easier for developer who want to develop software with dynamic content. You can find list of JAVA CMS List.
List of  CMS in Java:
InfoGlue
This is another scabable, robust and advanced content management System
or portal platform.
OpenCms Project
This  is a high level (professional) Open Source Website Content
Management System based on XML and Java technology.
Enonic
This is flexible prttal framework that provides a powerful solution to
efficiently produce, maintain and publish content for Intranet ,
Internet and Extranet.
Ivata op 
This is a free Groupware/Exchange/Intranet system that have web front
end
Xitex WebContent M1
Enterprise content management solution Xitex WebContent M1,
which allows easy website management by any one who are non technical.
CuppaWeb CMS
This content management system is based in j2ee, java, WebStart and XML
technology.
Simplicis Marketing Dashboard
Asbru
Dot CMS
Alfresco
Magnolia
Hippo CMS
VosaoCMS
Yanel
OpenWGA
jAPS
Liferay
DSpace
Fedora
LogicalDOC
Nuxeo EP

Thursday, June 2, 2011

List of IDE for Java Development

Here are the list of useful IDE to develop java based project. These IDE are not serially, but all have some special advantage.

List of Java IDE, Java/J2EE IDE Lists, List of IDE for Java Development:

1. Eclipse

Eclipse very good and open source IDE for Java project development. It is used a lot commercially and personally. It was made in Java so it's cross-platform.
Lots of support for additional plug-ins  are available to extend
your developing needs.

2. IntelliJ Idea

This is an intelligent Java IDE intensely focused on developer productivity
which provides a robust combination of enhanced development tools.

3. Netbeans

Netbeans is another very good IDE. Netbeans has a built-in GUI Builder for those you like that R.A.D. It was made in Java so it's cross-platform like Eclipse.
This is best for Desktop based project that allows drag and drop option
to create form, textbox etc

4. JCreator

This is very good and very easy to use. This IDE was made in C++ unlike the ones above, which were all made in Java.
and it runs only in Windows platform.

5. DrJava

This IDE is a lightweight development environment for writing Java
project. DR. Java  is designed primarily for students, providing an
intuitive interface and the ability to interactively evaluate Java code.

6. BlueJ

BlueJ IDE developed towards first time Java developers.This IDE teaches
us lot of programming concepts in Java and  it also has a good UML tool.

7. Borland JBuilder

Borland JBuilder is a great commercial IDE for Java project. Some developers believe it's worth it
to pay its price. JBuuildet has a built-in Java GUI Builder too

Which programming language is best to develop software between java and .net?

What programming should I select for developing any software? Which programming language is best to develop software between java and .net? How do you choose the right programming language for your project?
There are lot of programming language that are used to create software from desktop application to web based. Java programming language and .Net are most popular programming language that is used world wide. Php is another popular one.  But lots of developer might want to know the difference between Java and .Net programming language. Basically both have language have some strong and weak points. Most of the programmer might want to know which language is to be used while developing any software.
 I would say, that depends on the requirements, available resource, technology and etc. Suppose, if your application should be platform independent, that should be installed in Linux, Solaris etc, then Java is best option.
Lets have some question.
What kind of skilled resources do you have?
What is the development environemnt?
Where do you want to host the software? Linux or Windows?
Is client is ready to pay software license?
How much time do you have to develop the software?
etc...

First option is you should choose the technology on which your resource are expert. If you have resource of Java skilled then its risky to choose .Net.

Development environment is also another important things to be noted, if you have to use Linux OS to develop then you must use Java.

Hosting environment is another important points that is to be remember, you cant host/deploy .Net based software in Linix or Solaris etc. Php/Java can be deployed.

Cost is another most factor. If you don't have budget, all the software for java development is free. For dot Net you must purchase paid version.

For time, I guess developing in .Net is faster than Java, because .Net provide drag and drop GUI.


Please fell free to add additional information.







Java Vs Dot .Net feature Comparison

Difference between Java and Dot Net, Java Vs Dot Net 
Which is better, Java or dot Net?
Java programming language and .Net are the most used technology world wide to developer any desktop and web based software. Both technology have own advantage and disadvantage.

.Net is product of Microsoft and Java is that of SUN Micro systems
So lets compare the JAVA and .Net feature wise,
JAVA .Net
Java is Programming Language Its not programming language, its framework
Platform dependency Platform Independent Dependent, run in any Windows only
Cost Free Required paid License
GUI Swing GUI User Friendly than Java
Developed By SUN Microsoft
IDE JBuilder, Eclipse, NetBeans etc Visual Studio