Java EE 6 web application hello world

Java EE 6 web application hello world in Eclipse without Maven. First servlet and simple doGet() method. Export to WAR (Web Application ARchive) file. Java web application structure explained including simple web.xml file.

Let's create a Java web application in Eclipse. In this video I will perform this without Maven. Create a New Project -> Dynamic Web Project, type project name: "HelloWorldWeb" and select Dynamic web module version = 3.0, which is Java EE 6.

Whatever you put into WebContent directory will be directly visible to client. Let's add index.jsp page with some welcome text.

JSP file says Hello world :)

Next let's create some servlet, something like HelloServlet. Map it to "/hello.html" and inside doGet() method add this code:

PrintWriter out = response.getWriter();
out.println("Hello from first servlet!");

This will show this HTML code to user.

To deploy this project to server, just simply drag and drop this project to appropriate server.

Test index.jsp page and hello.html page. 

To create a WAR file right click on this project, choose Export -> WAR file and select a destination. If you look inside created file, you will see that everything that was in WebContent is inside. Content of directories META-INF and WEB-INF is not directly visible to the client. Everything else like index.jsp is directly visible. 

Inside WEB-INF are two directories: directory "classes", where are Java class files that were in "Java resources". Right now just our servlet is inside. Inside "lib" directory can be JAR files whose classes would be accessible in this web application. Currently there are none.

Inside lots of web applications is also web.xml file, which is inside WEB-INF directory and is called deployment descriptor of a web application. This file is not required since Java EE 6 and has been mostly replaced by annotations like @WebServlet. You can still use it even in Java EE 6, though.