Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Product database:
create table Product
(
pid number (4),
pname varchar2 (15),
price number (6, 2)
);

web.xml:
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>DatabaServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/getdata</url-pattern>
</servlet-mapping>
</web-app>

DarabaServ.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class DatabaServ extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  String pid1 = req.getParameter("prodata_pid");
  String pname = req.getParameter("prodata_pname");
  String price1 = req.getParameter("prodata_price");
  int pid = Integer.parseInt(pid1);
  float price = Float.parseFloat(price1);
  try {
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:
    Hanuman ","
    scott ","
    tiger ");
    PreparedStatement ps = con.prepareStatement("insert into Product values (?,?,?)"); ps.setInt(1, pid); ps.setString(2, pname); ps.setFloat(3, price); int i = ps.executeUpdate(); pw.println("<h4>" + i + " ROWS INSERTED..."); con.close();
   } catch (Exception e) {
    res.sendError(503, "PROBLEM IN DATABASE...");
   }
  }
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
   doGet(req, res);
  }
 }
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DataServ extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  String name = req.getParameter("persdata_eurn");
  String cname = req.getParameter("persdata_eurc");
  pw.println("<center><h3>HELLO..! Mr/Mrs. " + name + "</h3></center>");
  pw.println("<center><h3>Your COURSE is " + cname + "</h3></center>");
 }
 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  doGet(req, res);
 }

web.xml:
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>DataServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/getdata</url-pattern>
</servlet-mapping>
</web-app>

NOTE:
1. Whenever we are not entering the data in html form that data will be treated as empty space.
2. Query string represents the data which is passed by client to the servlet through html form. URI stands for Uniform Resource Indicator which gives the location of servlet where it is available. URL stands for Uniform Resource Locator which gives at which port number, in which server a particular JSP or servlet is running. ht represents a context path which can be either a document root or a war file.
web.xml:

<web-app>
<context-param>
<param-name>v1</param-name>
<param-value>10</param-value>
</context-param>
<context-param>
<param-name>v2</param-name>
<param-value>20</param-value>
</context-param>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>Serv1</servlet-class>
<init-param>
<param-name>v3</param-name>
<param-value>30</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>pqr</servlet-name>
<servlet-class>Serv2</servlet-class>
<init-param>
<param-name>v4</param-name>
<param-value>40</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/firstserv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pqr</servlet-name>
<url-pattern>/secondserv</url-pattern>
</servlet-mapping>
</web-app>

Serv1.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Serv1 extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  ServletConfig config = getServletConfig();
  ServletContext ctx = config.getServletContext();
  String val1 = ctx.getInitParameter("v1");
  String val2 = ctx.getInitParameter("v2");
  String val3 = config.getInitParameter("v3");
  String val4 = config.getInitParameter("v4");
  int sum = Integer.parseInt(val1) + Integer.parseInt(val2);
  pw.println("<h3> Value of v1 is " + val1 + "</h3>");
  pw.println("<h3> Value of v2 is " + val2 + "</h3>");
  pw.println("<h3> Value of v3 is " + val3 + "</h3>");
  pw.println("<h3> Value of v4 is " + val4 + "</h3>");
  pw.println("<h2> Sum = " + sum + "</h2>");
 }
}


Serv2.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Serv2 extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  ServletContext ctx = getServletContext();
  Enumeration en = ctx.getInitParameterNames();
  while (en.hasMoreElements()) {
   Object obj = en.nextElement();
   String cpname = (String) obj;
   String cpvalue = ctx.getInitParameter(cpname);
   pw.println("</h2>" + cpvalue + " is the value of " + cpname + "</h2>");
  }
 }
}


import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class DbServ extends HttpServlet {
 ServletConfig sc = null;
 public void init(ServletConfig sc) throws ServletException {
  super.init(sc);
  this.sc = sc;
 }
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  String dname = sc.getInitParameter("dname");
  String url = sc.getInitParameter("url");
  String tab = sc.getInitParameter("tab");
  try {
   Class.forName(dname);
   Connection con = DriverManager.getConnection(url, "scott", "tiger");
   Statement st = con.createStatement();
   ResultSet rs = st.executeQuery("select * from " + tab);
   while (rs.next()) {
    pw.println("<h2>" + rs.getString(1) + "" + rs.getString(2) + "" + rs.getString(3) + "</h2>");
   }
   con.close();
  } catch (Exception e) {
   res.sendError(503, "PROBLEM IN DATABASE...");
  }
 }
}



web.xml:


<web-app>

<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>DbServ</servlet-class>
<init-param>
<param-name>dname</param-name>
<param-value>oracle.jdbc.driver.OracleDriver ()</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:Hanuman</param-value>
</init-param>
<init-param>
<param-name>tab</param-name>
<param-value>emp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/dbdata</url-pattern>
</servlet-mapping>
</web-app>
1. Simple:-
Java is a simple programming language because:
a- Java technology has eliminated all the difficult and confusion oriented concepts like pointers, multiple inheritance in the java language.
b- The c,cpp syntaxes easy to understand and easy to write. Java maintains C and CPP syntax mainly hence java is simple language.
c- Java tech takes less time to compile and execute the program.


2. Object Oriented:-
Java is object oriented technology because to represent total data in the form of object. By using object reference we are calling all the methods, variables which is present in that class. The total java language is dependent on object only hence we can say java is a object oriented technology.


3. Platform Independent :-
Compile the Java program on one OS (operating system) that compiled file can execute in any OS(operating system) is called Platform Independent Nature. The java is platform independent language. The java applications allows its applications compilation one operating system that compiled (.class) files can be executed in any operating system.





4. Architectural Neutral:-

Java tech applications compiled in one Architecture (hardware----RAM, Hard Disk) and that Compiled program runs on any hardware architecture(hardware) is called Architectural Neutral.

5. Portable:-
In Java tech the applications are compiled and executed in any OS(operating system) and any Architecture(hardware) hence we can say java is a portable language.

6. Robust:-
Any technology if it is good at two main areas it is said to be ROBUST

1 Exception Handling
2 Memory Allocation

JAVA is Robust because

a. JAVA is having very good predefined Exception Handling mechanism whenever we are getting exception we are having meaning full information.
b. JAVA is having very good memory management system that is Dynamic Memory (at runtime the memory is allocated) Allocation which allocates and deallocates memory for objects at runtime.

7. Secure:-
To provide implicit security Java provide one component inside JVM called Security Manager.

To provide explicit security for the Java applications we are having very good predefined library in the form of java.Security.package.

Web security for web applications we are having JAAS(Java Authentication and Authorization Services) for distributed applications.

8. Dynamic:-
Java is dynamic technology it follows dynamic memory allocation(at runtime the memory is allocated) and dynamic loading to perform the operations.

9. Distributed:-
By using JAVA technology we are preparing standalone applications and Distributed applications.

Standalone applications are java applications it doesn’t need client server architecture. web applications are java applications it need client server architecture.

Distributed applications are the applications the project code is distributed in multiple number of jvm’s.

10. Multithreaded: -
Thread is a light weight process and a small task in large program.

If any tech allows executing single thread at a time such type of technologies is called single threaded technology.

If any technology allows creating and executing more than one thread called as Multithreaded technology called JAVA.

11. Interpretive:-
JAVA tech is both Interpretive and Completive by using Interpretator we are converting source code into byte code and the interpretator is a part of JVM.

12. High Performance:-
If any technology having features like Robust, Security, Platform Independent, Dynamic and so on then that technology is high performance.