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.

 

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. 😀

Java Logo — December 4, 2010
Java Application showing Swings and Event Handling-CalFactorial.java — October 30, 2010

Java Application showing Swings and Event Handling-CalFactorial.java

Hi All

Here is a Simple Java Application based on Swings and Event Handling which calculates the Factorial of a Number in a given text box and display it in another text box on clicking the Calculate Factorial Button.

No validations have been performed.

Level-Beginner

Program – CalFactorial.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalFactorial extends JFrame implements ActionListener  {
 JLabel l1,l2;
 JButton b1;
 JTextField tf1, tf2;
 CalFactorial()  {
 l1 = new JLabel("Number : ");
 l2 = new JLabel("Factorial : ");
 tf1 = new JTextField(10);
 tf2 = new JTextField(10);
 b1 = new JButton("Calculate Factorial");
 setLayout(new FlowLayout());
 add(l1); add(tf1);
 add(l2); add(tf2);
 add(b1);
 b1.addActionListener(this);
 }
public void actionPerformed(ActionEvent ae)  {
 String s = ae.getActionCommand();
 if(s.equals("Calculate Factorial"))  {
 String str = tf1.getText();//getting the number from Number TextField
 int num= Integer.parseInt(str);
 int fact = 1;
 //Calculating the Factorial
 for(int i=num; i>=1; i- -){
 fact = fact * i;
 }
 tf2.setText(""+fact);
 }
}
 public static void main(String[] args)  {
 CalFactorial cr = new CalFactorial();
 cr.setTitle("Swings Sample Program-CalFactorial");
 cr.setSize(400,300);
 cr.setVisible(true);
 cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

Save this program as CalFactorial.java

To Compile type at the prompt

>javac CalFactorial.java

To run the program type

>java CalFactorial

Snapshots

Here it look like this

Here Factorial of 7 is displayed.

Java Application showing Swings and Event Handling-CalCube.java — October 28, 2010

Java Application showing Swings and Event Handling-CalCube.java

Hi All

Here is a Simple Java Application based on Swings and Event Handling which calculates the Cube of a Number in a given text box and display it in another text box on clicking the Calculate Cube Button.

No validations have been performed.

Level-Beginner

Program – CalCube.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CalCube extends JFrame implements ActionListener {
JLabel l1,l2;
JButton b1;
JTextField tf1, tf2;
CalCube() {
l1 = new JLabel("Number : ");
l2 = new JLabel("Cube : ");
tf1 = new JTextField(10);
tf2 = new JTextField(10);
b1 = new JButton("Calculate Cube");
setLayout(new FlowLayout());
add(l1); add(tf1);
add(l2); add(tf2);
add(b1);
b1.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if(s.equals("Calculate Cube"))  {
String str = tf1.getText();
int num= Integer.parseInt(str);
tf2.setText(""+num*num*num);
}
}


public static void main(String[ ] args) {
CalCube cr = new CalCube();
cr.setTitle("Swings Sample Program-CalulateCube");
cr.setSize(400,300);
cr.setVisible(true);
cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Save this program as CalCube.java

To Compile type at the prompt

>javac CalCube.java

To run the program type

>java CalCube

Snapshots

Here it look like this


Enter the Number whose cube you want to calculate in the Number Text Field and then Click on the Calculate Cube Button.


Here Cube of 2 has been calculated and displayed.

First Java Program — October 25, 2010

First Java Program

This is the Hello World Java Program. Save this Program as Hello.java

class Hello{
      public static void main(String[] args){
         System.out.println("Hello World :) !!!!");
       }
}

To compile this program write at command prompt

javac Hello.java

To run this program type

java Hello

It will not do anything great only display Hello World 🙂 !!!! at the console but this is the 1st step for any Java Programmer.

We will get deep into Java soon 🙂

Snapshot

Hello World Java Program