Leaderboard1

Leaderboard2

Wednesday, May 21, 2014

How To Install Maven On Windows

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, and dependency management.

Steps:

1. Download Apache Maven

Download Apache Maven from here. Extract zip contents. Before configuring Maven make sure Java_Home( jdk path) must be set in Environment variables.

2. Add MAVEN_HOME

As you run Maven from command prompt, so it's path should be present in Environment variables.

Set MAVEN_HOME = E:\apache-maven-3.2.1 , make sure path should be the extracted maven folder.

3. Add Path

Add Maven bin path so that you can run it from anywhere.











4. Verify

Run this command on command prompt:

mvn -version

You will get following:

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T23:07:52+05:30)
Maven home: D:\apache-maven-3.2.1\bin\..
Java version: 1.7.0_40, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_40\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

RESTful Web Service with Java: Tutorial

RESTful Web Services

This post will demonstrate how to create a RESTful Web Service and client using Jersey framework which extends JAX-RS API.

REST fundamentals
  • Everything in REST is considered as a resource.
  • Every resource is identified by an URI.
  • Uses uniform interfaces. Resources are handled using POST, GET, PUT, DELETE operations which are similar to Create, Read, update and Delete(CRUD) operations.
  • Be stateless. Every request is an independent request. Each request from client to server must contain all the information necessary to understand the request.
  • Communications are done via representations. E.g. XML, JSON.

Download Jersey zip bundle from here.

You will need these jars:







  • asm-3.1.jar
  • jersey-client-1.17.1.jar
  • jersey-core-1.17.1.jar
  • jersey-server-1.17.1.jar
  • jersey-servlet-1.17.1.jar
  • jsr311-api-1.1.1.jar

  • Now create a new class UserInfo.java.



    package com.restws;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("UserInfoService")
    public class UserInfo {

     @GET
     @Path("/name/{i}")
     @Produces(MediaType.TEXT_XML)
     public String userName(@PathParam("i") String i){
      
      String name = i;
      return "" + "" + name + "" + "";
      
     }

     @GET
     @Path("/age/{j}")
     @Produces(MediaType.TEXT_XML)
     public String userAge(@PathParam("j") int j){
      
      int age = j;
      return "" + "" + age + "" + "";
      
     }

    Now create web.xml file in WEb-INF folder.

    }
      To run the project, right click on it and click on run as ->run on server.
    http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Akash

    Now create MyClient.java:

    package com.restclient;

    import javax.ws.rs.core.MediaType;
    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.ClientResponse;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;

    public class MyClient {

      public static final String BaseURI = "http://localhost:8080/RESTfulWS";
     public static final String PATH_NAME = "/MyService/name/";
     public static final String PATH_AGE = "/MyService/age/";

      public static void main(String... args) {

       String name = "Akash";
      int age = 26;

       ClientConfig config = new DefaultClientConfig();
      Client client = Client.create(config);
      WebResource resource = client.resource(BaseURI);

       WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
      System.out.println("Client Response \n"
        + getClientResponse(nameResource));
      System.out.println("Response \n" + getResponse(nameResource) + "\n\n");

       WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
      System.out.println("Client Response \n"
        + getClientResponse(ageResource));
      System.out.println("Response \n" + getResponse(ageResource));
     }

      /* Returns client response */
     private static String getClientResponse(WebResource resource) {
      return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)
        .toString();
     }

      /* Returns the response as XML */
     private static String getResponse(WebResource resource) {
      return resource.accept(MediaType.TEXT_XML).get(String.class);
     }
    }


    Run MyClient.java as Java applicaiton, you will get following output:

    Client Response
    GET http://localhost:8080/RESTfulWS/rest/MyService/name/Akash returned a response status of 200 OK
    Response
    Akash

    Client Response
    GET http://localhost:8080/RESTfulWS/rest/MyService/age/26 returned a response status of 200 OK
    Response
    26

    Thursday, December 20, 2012

    Tomcat: How to send the log messages to a File

    Sometimes it's needed to send logs to separate file without mixing  them in server log files.. Thats makes it easy to understand & prevent disk overflow situation. We can use log4g.jar for this.

    Log4j File Appenders – example for Logging messages to a file:
    In most cases, either Rolling File Appender or Daily Rolling File Appender classes are used in production scenarios for logging messages into a file. A Rolling File Appender will send all the log messages to the configured file, until the file reaches a certain size. Once the log file reaches a maximum size, the current file will be backed up and a new log file will be created (thus the name Rolling File Appender). The Daily Rolling File Appender is also similar to Rolling File Appender except that the rolling happens at given frequency. Below is an example for using RollingFileAppender
    1. Add Log4j logging support to your project.
    2. In your log4j.properties file, add below lines:
      log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
      log4j.appender.rollingFile.File=D:/myapp/mylog.log
      log4j.appender.rollingFile.MaxFileSize=2MB
      log4j.appender.rollingFile.MaxBackupIndex=2
      log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
      log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
    3. As you can see from the above log4j  configuration, we are creating a new log4j appender in the name ofrollingFile and setting it’s options. I guess most of the options are self explanatory. The MaxBackupIndex tells the log4j to keep a maximum of 2 backup files for the log mylog.log. If the log file is exceeding theMaximumFileSize, then the contents will be copied to a backup file and the logging will be added to a new empty mylog.log file. From the above configuration, there can be a maximum of 2 backup files can be created.
    4. Next we need to direct our logs to go into this log4j appender.
      log4j.rootLogger = INFO, rollingFile
    5. That’s it. From now on, when you log something from your Java class, the log message will go into the mylog.log file.
     Copy this log4j.properties file in webcontent. Give its path in servlet class. See below one.

    public void init(ServletConfig config) throws ServletException {
    //1.
    logger.info("XML_FetcherInitServlet is initializing log4j");
    String log4jLocation = config.getInitParameter("log4j-properties-location");

    ServletContext sc = config.getServletContext();

    if (log4jLocation == null) {
    System.err.println("*** No log4j-properties-location init param, so initializing log4j with BasicConfigurator");
    BasicConfigurator.configure();
    } else {
    String webAppPath = sc.getRealPath("/");
    String log4jProp = webAppPath + log4jLocation;
    File log4jPropsFile = new File(log4jProp);
    if (log4jPropsFile.exists()) {
    System.out.println("Initializing log4j with: " + log4jProp);
    PropertyConfigurator.configure(log4jProp);
    } else {
    System.err.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator");
    BasicConfigurator.configure();
    }
    }
    logger.debug("RealPath" + sc.getRealPath("/"));
    //3.
    super.init(config);
    }

    If log4j.properties not found then default will be used by server..

    Monday, December 17, 2012

    How To Find Your Windows 7 Product Key

    To Use "ProduKey" to See Product Key Number in Windows 7

    NOTE: This option shows you how to use the free program ProduKey to see what the product key number is from within a Windows 7 that it has already been entered in (ex: activated). ProduKey will not show the product key number for Windows 7 Enterprise though.
    1. Download, extract, and run the free ProduKey program.

    2. You will now see Windows product key listed.
    Product Key Number for Windows 7 - Find and See-produkey.jpg

    Friday, December 14, 2012

    Change Stack Overflow profile picture

    Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. Avatars help identify your posts on blogs and web forums
    It's tied to the gravatar that your Email address uses.
    • Go to Gravatar.com
    • create a profile and upload a picture.
    • Add that Email address to your Stack Overflow profile.
    • Wait a few hours for it to propogate.