Java Source Code
1. Write a simple Hello World java program. |
---|
package eprogramminglab; public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World"); } } // End HelloWorld |
2. Write a simple Hello World java program. |
---|
public class HelloWorld { public static int a=10; // Static (class level) variable public int b=20; // Non-static (class level, without static) variable int c=30; // Local variable public static void main (String[] args) { int lvl1 = 100; HelloWorld obj = new HelloWorld(); // Static variable can be called directly System.out.println(a); // Non-static variable should be called by using object System.out.println(obj.b); // Local variable System.out.println(obj.c); // Local variable System.out.println(lvl1); System.out.println("M1: "); m1(); System.out.println("M2: "); obj.m2(); System.out.println("M3: "); m3(); System.out.println("M4: "); // obj.m4(); System.out.println("M5: "); // obj.m5(); System.out.println("M6: "); // obj.m6(); } // End main // Static method static void m1() { System.out.println("Static Method: "); } // End m1 // Non static method void m2() { System.out.println("Nonstatic Method: "); } // End m2 // Void method static void m3() { System.out.println("Void Method: "); } // End m3 // Non void method static int m4(int val) { System.out.println("Nonvoid Method: "); int x = 5, y = 6; val = x*y; return m4(val); } // End m4 // Parameterized method static int m5(int a, int b) { System.out.println("Parameterized Method: "); return m5(a, b); } // End m5 // Non-parameterized method static int m6() { System.out.println("Nonparameterized Method: "); return m6(); } // End m6 } // End HelloWorld |
3. Write a simple program to call static method. |
---|
package eprogramminglab; public class EprogrammingLab { public static int a = 10; public int b; public static void main (String[] args) { System.out.print("M1: "); m1(); } static void m1() { System.out.println("Static Method:"); System.out.print("a = " + a); EprogrammingLab obj = new EprogrammingLab(); System.out.println(", b = " + obj.b); } } package eprogramminglab; public class EprogrammingLab { public static int a = 10; int b; public static void main (String[] args) { EprogrammingLab obj1 = new EprogrammingLab(); System.out.print("M1: "); obj1.m1(); } void m1() { System.out.println("Calling Non Static Method:"); System.out.print("a = " + a); EprogrammingLab obj2 = new EprogrammingLab(); System.out.println(", b = " + obj2.b); } } |
Q. Create a simple java program to show servlet to servlet communication. |
---|
package com.Servlet2ServletCommunication; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); pw.print(" |
package com.Servlet2ServletCommunication; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); pw.print(" |
|