Sunday, October 7, 2012
Today I am going to post a hello world servlet program that uses latest Servlet3.0 technology. For all new users of this technology, I would like to mention that deploying your web-app has been made easy. With this technology you can use annotations to set the URL of your web-app now, which was only possible through deployment descriptor in their previous versions. So here I will post the simplest code to show how to use this method.

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

/*http://localhost:8080/def/a/b/simple.html is the complete url when your server is running at port 8080 and your app directory is def */

@WebServlet("/a/b/simple.html") //this is relative URL
public class SimpleServlet extends HttpServlet {

    public void doGet(HttpServletRequest req,HttpServletResponse res){

        PrintWriter out = res.getWriter();
/* getting writer yo send dynamic data to client */
            "<html><head><title>Servlet Demo</title></head><body> <h1><center>Hello World</center></h1></body></html>"); 
//sending html data
//closing writer
        }catch(Exception e){ e.printStackTrace();  }


Note that we are not mapping servlet in DD but using annotation for that. The URL we are giving is a relative one and false. Client cannot access the file directly as it is inside WEB-INF directory. Also it is written in such a way that client will think its a static html page whereas it is a dynamically generated html by servlet behind the curtain. So we are achieving url hiding and deployment in a very easier way. So try it out guys.


Post a Comment

Total Pageviews

Subscribe via Email


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.