Kushal's Java Blog | Software Engineering Blog www.sanjaal.com/java

The Servlet Basic Structure

January 27th, 2012

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Created on 19th July 2008
 */
package com.kushal.servlets;
/*
 * This is a basic servlet class that shows how
 * servlet can be structured.
 */
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyFirstServlet extends HttpServlet {

/**
 * This is GET method. Request has parameters,
 * and we write back using response.
 */
public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    out.write("Hello, this is my first servlet");
  }

/**
 * This is another kind of request with POST
 * kind of submission. We can simply reroute
 * the request to doGetmethod as below.
 */
public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
	  doGet(request, response);
  }
}

/**
 * In order to be able to run this servlet, you have
 * to setup a proper application server and deploy
 * the servlet. Discussing those steps is out of
 * scope in this post.
 */

Related Java Blog Posts




Your Ad Here


Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!

Originally posted 2008-07-19 23:24:23.

Share

Converting An Colored Image To A Gray Scale Using Java Graphics 2D [Example Code]

January 26th, 2012

In photography and computing, a grayscale or greyscale digital image is an image in which the value of each pixel is a single sample, that is, it carries only intensity information. Images of this sort, also known as black-and-white, are composed exclusively of shades of gray, varying from black at the weakest intensity to white at the strongest.

Grayscale images are distinct from one-bit black-and-white images, which in the context of computer imaging are images with only the two colors, black, and white (also called bilevel or binary images). Grayscale images have many shades of gray in between. Grayscale images are also called monochromatic, denoting the absence of any chromatic variation.

Grayscale images are often the result of measuring the intensity of light at each pixel in a single band of the electromagnetic spectrum (e.g. infrared, visible light, ultraviolet, etc.), and in such cases they are monochromatic proper when only a given frequency is captured. But also they can be synthesized from a full color image; see the section about converting to grayscale. [Wikipedia/Creative Commons Attribution-ShareAlike License]

In the following java program, we will:

  • Read an Image from the disk location
  • Convert the image to gray scale
  • Write the grayed out image back to the disk

Stewie Griffin is my favorite character from Seth MacFarlane’s animated TV Series “The Family Guy”. I have used Stewie’s image for the gray scale conversion example. The credit of the image goes to it’s original author.

package com.kushal.graphics;
/**
 * @Author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On: 2009-09-28
 *
 * Utility to convert a colored image into gray color.
 */
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class GrayMyImage {

	public static void main(String args[]) {
		GrayMyImage imageGrayer = new GrayMyImage();
		String inputImageFilePath="C:/temp/myImage.jpg";
		String outputImageFilePath="C:/temp/myImage-grayedout.jpg";

		System.out.println("Reading input image...");
		BufferedImage inputImage = imageGrayer.readImage(inputImageFilePath);
		System.out.println("Successfully Read Image: "+inputImageFilePath);

		System.out.println("\nConverting the image to Gray colors.");
		BufferedImage grayedOut = imageGrayer.grayOut(inputImage);
		System.out.println("Successful...");

		System.out.println("\nWriting gray image to filesystems.");
		imageGrayer.writeImage(grayedOut,outputImageFilePath , "jpg");
		System.out.println("Successfully Wrote Image To: "+outputImageFilePath);
	}

	/**
	 * This method converts any input BufferedImage
	 * object to the gray color and returns it.
	 */
	public BufferedImage grayOut(BufferedImage img) {
		ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace
				.getInstance(ColorSpace.CS_GRAY), null);
		colorConvert.filter(img, img);

		return img;
	}

	/**
	 * This method reads an image from the file
	 * @param fileLocation -- > eg. "C:/testImage.jpg"
	 * @return BufferedImage of the file read
	 */
	public BufferedImage readImage(String fileLocation) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File(fileLocation));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}

	/**
	 * This method writes a buffered image to a file
	 * @param img -- > BufferedImage
	 * @param fileLocation --> e.g. "C:/testImage.jpg"
	 * @param extension --> e.g. "jpg","gif","png"
	 */
	public void writeImage(BufferedImage img, String fileLocation,
			String extension) {
		try {
			BufferedImage bi = img;
			File outputfile = new File(fileLocation);
			ImageIO.write(bi, extension, outputfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output of the program:

Reading input image...
Successfully Read Image: C:/temp/myImage.jpg

Converting the image to Gray colors.
Successful...

Writing gray image to filesystems.
Successfully Wrote Image To: C:/temp/myImage-grayedout.jpg

Original Input Image:

Image Grayed Out By The Program Above:

Related Java Blog Posts




Your Ad Here


Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!

Blog Widget by LinkWithin

Originally posted 2008-07-19 23:24:23.

Share

Next »