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 java.io.*;
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 {
@Override
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
PrintWriter out = res.getWriter(); /* getting writer yo send dynamic data to client */
out.println(
"<html><head><title>Servlet Demo</title></head><body> <h1><center>Hello World</center></h1></body></html>"); //sending html data
out.close(); //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.
import java.io.*;
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 {
@Override
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
PrintWriter out = res.getWriter(); /* getting writer yo send dynamic data to client */
out.println(
"<html><head><title>Servlet Demo</title></head><body> <h1><center>Hello World</center></h1></body></html>"); //sending html data
out.close(); //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.
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment