Getting Request,Response in Grails Service class

March 26, 2012 4 comments

Here is the way to get the Request , Response & ServletContext in the Grails service class.

import org.codehaus.groovy.grails.web.util.WebUtils

…….

def webUtils = WebUtils.retrieveGrailsWebRequest()

//Getting the Request object

webUtils.getCurrentRequest()

//Getting the Response object

webUtils.getCurrentResponse()

//Getting the ServletContext object

webUtils.getServletContext()

You done it man.. 😉

Categories: Grails

Reading property file using ResourceBundle

November 29, 2011 Leave a comment

Now it’s very easy to read the .properties file using ResourceBundle utility.

Step 1 : Create a properties file, i.e. key.properties

name=Amit
email=amitpatelinfo@gmail.com

Step 2 : Create a Property reader class. i.e.  PropertyReader.java

package com.ap.util;

import java.util.ResourceBundle;
/**
* Read the properties file.
* @author Amit Patel
*
*/

public class PropertyReader {

private static final ResourceBundle keyBundle = ResourceBundle.getBundle(“key”);

public static String getValueFromKeyFile(String key){

try{

return keyBundle.getString(key);

}catch(Exception ex){

return null;

}

}

}

Step 3 : Use the PropertyReader to read the property file.

String email= PropertyReader.getValueFromKeyFile(“email”);

That’s it. .. Wow you read the property file using few line of code.. Great..!

Categories: Uncategorized

Increasing Tomcat’s memory

February 15, 2010 Leave a comment

Hi all,

If memory is not a problem on your server then allocate a large amount of memory, such as 512M or more. Also use the server VM option. EG: (-server -Xms512M -Xmx512M). Usually it is better to set both min and max heap size to the same in server applications.

Accordingly, if you’re using Tomcat (and you’d like to allocate a lot of memory), you can edit the catalina.(bin | sh) file found in the bin directory of your Tomcat installation.

For example, on a Windows machine (where I currently find myself these days), I added the following statement around line 71:

set JAVA_OPTS=%JAVA_OPTS% -server -Xms512M -Xmx512M -XX:MaxPermSize=256m.

Thanks,
Amit

PermGen space in Grails

February 15, 2010 6 comments

Hi ,

To resolve the permgen space in grails.

1) Open the Command prompt

2) fire the below command on cmd prompt.

set JAVA_OPTS=-XX:MaxPermSize=128m -XX:PermSize=128m -Xms1024m -Xmx1024m -XX:-UseGCOverheadLimit

3) Go to the project directory and fire run-app.

Getting Started with Grails

February 15, 2010 Leave a comment

Groovy provides a highly-expressive and fully object-oriented language for building Grails applications. Groovy allows the developer to convey ideas concisely and naturally, in a manner that’s easy to read and understand. And, Java developers will appreciate its familiar syntax and its seamless Java integration, knowing that they can invoke any Java class from within Groovy, and vice versa.

Installing Grails

1) Download Grails from following URL.

http://grails.org/Download

2) Grails Installation

  • Extract the archive into an appropriate location; typically C:\grails on Windows.
  • Create a GRAILS_HOME environment variable that points to the path where you extracted the archive (eg C:\grails on Windows)
  • If you have not set the JAVA_HOME environment variable yet, create JAVA_HOME environment variable that points to the path where you have installed Java
  • Append a reference to the “bin” directory within the Grails directory to your PATH variable (eg %GRAILS_HOME%\bin on Windows).
  • Type “grails” at the command line, if a help message is displayed you are ready to start using Grails!

3) Grails Command-Line Help

>grails

help:
Usage: grails [target]
Targets:

“create-app” – Create a new grails app
“create-controller” – Create a new controller
“create-service” – Create a new service
“create-domain-class” – Create a new domain class
“create-taglib” – Create a new tag library class
“create-test-suite” – Create a new test suite
“create-job” – Create a quartz scheduled job
“generate-controller” – Generates a controller from a domain class
“generate-views” – Generates the views from a domain class
“generate-all” – Generates all artifacts from a domain class
“test-app” – Run current app’s unit tests
“run-app” – Run the application locally and wait
“create-webtest” – Create the functional test layout
“run-webtest” – Run the functional tests for a running app
“shell” – Opens the Grails interactive command line shell
“console” – Opens the Grails interactive swing console
“war” – Create a deployable Web Application Archive (WAR

4) Create “Hello World!” Application

So let’s get started creating our first Grails application. To complete your journey through
creating a “Hello World!” application you’re going to step through the following tasks:

  • Execute the grails create-app command to create a basic Grails application.
  • Create something called a controller that will handle a web request.
  • Use the controller to display some text.
  • Run the Grails application and view what you achieved in a web browser.

5) To create our “hello” application we need to run the create-app target. This will prompt
you for the name of the application you wish to create. Enter the word hello and hit the return.

>grails create-app
init-props:
create-app:
[input] Enter application name:
hello

Upon completion, the target will have created the “hello” Grails application and the
necessary directory structure.

Grails Directory Structure

6)  start the Grails application by running the following target

run-app

7)  Run application in the browser.

http://localhost:8080/hello

Hello Friends!

August 8, 2009 Leave a comment



One of the best blog in the world which give a straight forward solution in any technology of JAVA. Technology includes Core Java , J2EE , Spring , Hibernate , Wicket , JSF , Struts , Groovy , Grails and many more.


I am sure you will get 100% solutions and knowledge of the Products.


You can also see some start up kit for all technology and also see some demo.


The main goal for the blog is to transfer my knowledge in the world.