Posts

Downloading a file from web application

Image
      This post explains developing a web application which allows requestor to download a file. Here web app will have a servlet which streams requested file to requestor. follow the step to develop it using NetBean 6.8 1. Create a web application [ refer previous posts ] 2.Create a Servlet and add the code given below in doGet method      String filePath =null;      int length =0;        //accept file name as parameter         String fileName = request.getParameter("FILE_NAME");         InputStream is = getServletContext().getClassLoader().getResourceAsStream("/"+fileName);         if(is == null){             PrintWriter writer = response.getWriter();   ...

RESTful WS using Jersey Part 3 – Java Client

This post is with reference of Restful WS using Jersey Part 2 – Parameterized approach to provide a java client for accessing Restful services The Complete code for Java Client /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package demo.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; /** * * @author Yogesh */ public class JavaClient {     public static void main(String[] args) {         String restUrlStr = " http://localhost:17286/HelloWorldRestWSPrj/resources/hello/Yogesh?query=age";         URLConnection conn = null;         try {             URL restUrl = new URL(restUrlStr);   ...

RESTful WS using Jersey Part 2 – Parameterized approach

Image
This post is in sequel  RESTful WS using Jersey Part 1 - Hello World . It shows how to utilize parameterized urls as well as query string. Please refer Part 1 to know developing restful services from scratch. This post utilizes same example. Parameters can be passed as part of path as well as url query string. Jersey provides two annotations @PathParam   and @QueryParam for extracting parameters from path and query respectively. We have modified getText method to accept both kind of parameters as below 1. Complete resource class will be package demo.resorces; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; /** * REST Web Service * * @author Yogesh */ @Path("hello/{UserName}") p...

RESTful WS using Jersey Part 1 - Hello World

Image
Here is the simple tutorial for RESTful Web Service using jersey implementation for RESTful Web Services. Java EE 6 has introduced jersey API as implementation for JSR-311 as API for RESTful Web Services. For ease of development,deployment and testing i have used NetBean 6.8 which supports JEE 6. Jersey is an annotation based implementation. Here are the few important annotations used in this tutorial @path(“/path/”) - a relative URI path indicating where the Java class will be hosted @GET - a request method designator and corresponds to HTTP Get method @POST - a request method designator and corresponds to HTTP Post method @Produces("text/plain") - specify the MIME media types of representations a resource can produce @Consumes("text/plain") -specify the MIME media types of representations a resource can consume @PathParam("User_Name") - a type of parameter that you can extract for use in your resource class @QueryPar...

Hello World EJB 3.1 Application using Netbean

Image
This post explains developing simple Hello world application in Net Bean . It explains ease of EJB 3.1 development using  annotations in you java code. Coding, deploying and using an enterprise bean never been so easy. Here we are going to develop a Stateless Session bean and going to invoke it from a Servlet. A Remote Interface for bean package demo.session; import javax.ejb.Remote; /** * @author Yogesh */ @Remote public interface HelloEJBRemote {    String getMessage(); }   Here @remote annotation is being used for demoting Remote Interface for EJB Module. It declares one public method which is by default business method. Bean Implantation Class - package demo.session; import javax.ejb.Stateless; /** * @author Yogesh */ @Stateless public class HelloEJB implements HelloEJBRemote { public String getMessage() { return "Hello ";     } ...

Graph Searching (BFS&DFS) Implementation

Image
Here is the adjacency matrix representation based implementation of Graph with both graph searching techniques . Following graph has been taken as example Adjacency matrix representation is below int array[][] = {{0,1,1,0,0,0},                      {1,0,0,1,1,0},                      {1,0,0,0,0,1},                      {0,1,0,0,0,0},                      {0,1,0,0,0,0},                      {0,0,1,0,0,0}}; Complete Code is as follows package demo.graph;...

First hand on Servlet 3.0 using Annotation

Image
   This tutorial gives introduction to coding and deploying Servlet as per 3.0 specification. Servlet 3.0 specification introduced Annotations that makes deploying servlet, Lister and Filter easier ever before. Here is the Servlet with Annotation ready to deploy without any web.xml. Here @webservlet annotation is being used to define servlet name, url pattern and init parameters. This annatation is equivalent to following entry in web.xml < servlet > <servlet-name> HelloServlet </servlet-name> <servlet-class> demo.HelloServlet </servlet-class> <init-param> <param-name> name </param-name> <param-value> Yogesh </param-value> </init-param> </servlet> < servlet-mapping > <servlet-name> HelloServlet </servlet-name> <url-pattern> /HelloServlet </url-pattern> </servlet-mapping> Complete Code : /* * To change this tem...