Showing posts with label OCEWCD. Show all posts
Showing posts with label OCEWCD. Show all posts

Serving Dynamic Content - CGI vs Servlets

Note: This article focuses OCEWCD students, and the term server refers the combination of web server and Servlet container like Apache Tomcat.
In a web application, clients send a request to the servers and servers return a response to the client. Once the client receives the response, the connection between client and server will get lost. So even if the same client makes another request, the server receives it as a new request from a new user, in other words, the server does not remember the client or the history of transactions with that client. If the request is for static content like an HTML file or media file, the server is much happy with that request and return the file as the response. If the request is for a dynamic content like a web page which contains weather details on that day, or current time (Notice that these details cannot be hardcoded in a static HTML file. They have to be generated on runtime) a web server cannot handle that request; it has to be handled by any other helper applications. The helper application can be either a Common Gateway Interface (CGI) or Servlet.
Read More

Java Web Application - Hello World

This tutorial shows you the way to create your first Hello World web application in Java. You need to have Eclipse IDE for Java EE Developers and Apache Tomcat, in order to develop this application. If you have not configured the Apache Tomcat in Eclipse, follow this tutorial and configure it first.

Step 1:
Go to File → New → Project.

Step 2:
Under the Web category, select the Dynamic Web Project option and click on Next.

Step 3:
Give the project name 'Hello World' and click on the Finish button.

Step 4:
Right click on the src folder of 'Hello World' project and select New → Servlet.

Step 5:
Give a package name 'com.javahelps.helloworld' and class name 'HelloWorldServlet' as shown in the screenshot.
Click on the Finish button to create the Servlet.


Step 6:
Modify the Servlet class code as provided below.
package com.javahelps.helloworld;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorldServlet
 */
@WebServlet("/saytime")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorldServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print("<html><body><h1 align='center'>" +
        new Date().toString() + "</h1></body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
Here the @WebServlet annotation defines the URL of this Servlet. The doGet method is used to serve the GET requests and doPost method is used to serve the POST requests. According to this code, this Servlet can accept both GET and POST requests but it produces the output only for GET requests. Any POST requests will cause to a blank screen in the browser. PrintWriter is used to write the current time as an HTML content to the response.

Step 7:
Right click on the WebContent folder and create a new HTML file index.html.


Step 8:
Modify the HTML file as shown below.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<a href="saytime">Click Here</a>
</body>
</html>
The hyperlink reference is pointing to the sayhello URL which is defined in the HelloWorldServlet class.

Step 9:
After saving all the changes, right click on the project and select Run As → Run on Server.

Step 10:
Select the Tomcat server and click on the Finish button to run the application.

Now your first application is available at the following link:
http://localhost:8080/Hello_World/



Find the project at Git Hub.
Read More

Introduction to Servers

This article mainly focuses the developers who wants to get the Oracle Certified Expert, Java EE 6 Web Component Developer (OCEWCD 6 - 1Z0-899) certification. In this article you will get a brief introduction of web servers, application servers and Servlet containers.

I noticed in my classes; there were some students, even do not know what kind of web development they are going to learn. They thought the OCEWCD is all about HTML, JavaScript and CSS, so this article starts with an introduction to client side applications and server side applications. There are two components in a web application. We call them as server side application and client side application. If you take an application, the fancy stuffs you see in your web browser belong to the client side. The background business logic, database access, session management and even more functional requirements are handled by server side component. Client side developers mostly use HTML, JavaScript, CSS, etc to develop the application. They are the artists and they know the way to attract the users. On the other hand, server side developers use languages like Java, Perl or PHP where the main concern is serving to the client side application and providing the functional requirements. OCEWCD exam is for the server side application developers, even though there are some client side JSP related topics.



Read More

Contact Form

Name

Email *

Message *