Bits and Bytes

Learn to Live

Java EE Introduction — March 4, 2011

Java EE Introduction

What are Enterprise Applications?

Enterprise applications are the software applications developed intended to use at large scale. These applications involves large number of data accessing concurrently by many users.

What are Enterprise Beans?

Enterprise beans are the Java EE server side components that run inside the ejb container and encapsulates the business logic of an enterprise application.

 

Advertisements
Simple HelloWorldServlet — January 15, 2011

Simple HelloWorldServlet

package MyPkg

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class HelloServlet extends HttpServlet

{

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException

{

res.setContentType(“text/html”);

PrintWriter out = res.getWriter();

out.println(“<html><head><title>Hello Servlet</title></head>”);

out.println(“<body>”);

out.println(“<h1>Hello World!!!</h1>”);

out.println(“</body></html>”);

}

}

 

o/p

Hello World

HelloWorldServlet Program using NetBeans IDE —

HelloWorldServlet Program using NetBeans IDE

Duke, the Java Mascot, in the waving pose. Duk...
Image via Wikipedia

package MyPkg;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

try {

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>HelloWorld Servlet</title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“<h1>Hello World!!!</h1>”);

out.println(“</body>”);

out.println(“</html>”);

} finally {

out.close();

}

}

/**

* Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/**

* Handles the HTTP <code>POST</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/**

* Returns a short description of the servlet.

* @return a String containing servlet description

*/

@Override

public String getServletInfo() {

return “Short description”;

}

}

Output


Servlet using NetBeans IDE —

Servlet using NetBeans IDE

Start NetBeans IDE

Open New Project

Select JavaWeb– WebApplication

Give Name and Location of your Project

Select Server and Settings

Select Frameworks-Don’t select anything now.

index.jsp file is the jsp page which is displayed

Run the file using F6

Right Click on the ProjectName in left here WebApp and select New- Servlet

Give Name and Location for the Servlet

Configure Servlet Information

The Servlet get displayed- HelloWorldServlet

Uncomment the line inside processRequest method to run the servlet and edit as per your requirement

Right Click on the Servlet to Run it

Set Servlet Execution URI

O/P of the Servlet in a Browser Window. 😀