Java Programming part-3

                                                           Advance java


jdbc:

jdbc is nothing but java database connectivity
it is an api provided by java,it helps to access the data base.
jdbc supports sql statements
manipulation of database is done through servlets.

difference between jdbc and odbc:

jdbc: components that are used for establishing the connection with the database using jdbc
1.jdbc api
2.jdbc driver manager
3.jdbc-odbc bridge
jdbc api classes are supported by the java package

import java.sql.*;
>jdbc driver manager which connects your >java application or servlet to jdbc driver.

class.forname(“oracle.jdbc.driver.oracledriver”);
connection con=drivermanager.getconnection(“jdbc:oracle:thin:@l;ocalhost:1521:xe”,”scott”,”tiger”);

Example program on jdbc in Java

import java.sql.*;
class firstjdbc
{
public static void main(string args[])throws exception
{
try
{
class.forname("oracle.jdbc.driver.oralcedriver");
connection con=drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:ww","system","jyothi");
statement st=con.createstatement();
resultset rs=st.executequery("select * from jyo");
while(rs.next())
{
int n=rs.getint("sno");
string s=rs.getstring("name");
 system.out.println("my rno is"+n+"my name is"+s);
}
st.close();
con.close();
}
catch(exception e)
{
system.out.println(e);
}
system.out.println("thank y");
}
}

Example program on prepared statement in Java

import java.util.*;
import java.sql.*;
class prepstmt
{
public static void main(string args[])throws exception
{
scanner s=new scanner(system.in);
system.out.println("enter roll no");
int n=s.nextint();
try
{
class.forname("oracle.jdbc.driver.oralcedriver");
connection con=drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:ww","system","jyothi");
statement st=con.createstatement();
resultset rs=st.executequery("select * from jyo");
while(rs.next())
{
int n=rs.getint("sno");
string s=rs.getstring("name");
system.out.println("my rno is"+n+"my name is"+s);
}
st.close();
con.close();
}
catch(exception e)
{
system.out.println(e);
}
system.out.println("thank y");
}
}

Example program on jdbc in Java

import java.sql.*;
class jdbc2
{
public static void main(string args[])throws exception
{
try
{
class.forname("oracle.jdbc.driver.oralcedriver");
connection con=drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:ww","system","jyothi");
statement st=con.createstatement();
resultset rs=st.executequery("select * from jyo");
while(rs.next())
{
int n=rs.getint("sno");
string s=rs.getstring("name");
int n1=rs.getint("sal");
system.out.println("my rno is"+n+"my name is"+s+"salary"+n1);
}
st.close();
con.close();
}
catch(exception e)
{
system.out.println(e);
}
system.out.println("thank y");
}
}
servlets:

servlets are small java programs that run on servers and servlets are server side programs.
client side programming is mainly based on applet programming and scripting language
server side programs are servlets and jsp’s
1.user enters url on some web browser.
2.browser generates http request and sends it to web server
3.this web server maps this request to requested file.
4.the file is returned to the browser as a response
5.servelts are simple java programs that run on servers
6.they are also called as http servlet.

uses
:
1.process and store data submitted by html form.
2.to provide dynamic contents ex:retrieving and updating data base.
3.servlets  can be used in cookies and session tracking.
cgi: common gate way interface
in this each instance is answered in a separate process by a separate instance
cgi is written in perl or c.
not more secured compared to servlets.

web servers:

communication with web servers can be with the help of web server.
ex: apache web server, micro soft iis(internet info services)
google gws(google web server),microsoft personal web server(pws)
client makes a request by sending url(uniform resource locator)

url is divided in to 3 parts

1.name of protocol
2.name of web server
3.request for desired web page

life cycle of servlet

1.init()
2.service()
3.destroy()

init(): this method is called only when the servlet is loaded in the memory for the first tome.using this method initialization parameters can also be passed to the servlet in order to configure itself.

service(): server can invoke service for request using service().servlets read the data by http request with the help of service().

destroy(): finally server unloads the servlet from the memory using destroy().
some special methods which can be used to handle http request.

they are
1.dodelete ()
2.doget()
3.dopost()
4.dohead()
5.dooption()
6.dopost()
7.dotrace()

session tracking

session tracking are two types
1.stateful
2.stateless

stateful: part of data is exchanged between client and server and these protocols always keep track of communication sessions.

stateless: neither client nor server keeps track of state of communication sessions

cookies: in cookies session tracking techniques the data of every client will be given back to the client browser along with the objects of cookie class, so that the client side browser will receive those cookies and store temporarily at client side.

cookie is a pair of name and value

syntax:

cookie c1=new cookie(“name”,value);
if you want to send this cookie object too the client browser than these cookie objects should be stored into response object as shown below.
res.addcookie(ob);
getcookies():is the method of http servlet request interface this method automatically read all the cookie objects.that is cookie objects available in the request objects,into the heap memory at some address at server side.these addresses are stored into one array of cookie class.

ex: c[0].getname();
      c[0].getvalue();
url rewriting: in this technique the data of every individual client will be given back to the client browser along with an anchoring message with some parameter name and values. so that the client browser will display the respective anchoring message on the client browser by storing those parameters names and values in the “ram” temporary at client side.
if the user at the client side on the anchoring message then the client browser automatically find those parameter names and values along with the request from the client side to server servlet application. so all these parameters names and values are automatically destroy at client side when the client browser is closed.

hiddenform fields

in this technique the data of every client will be given back to the client browser along with on e form with some hidden text fields and values so that the browser at client side will display the respective form by hiding those text fields and values means they are not visible to the user at client side.if the user at client side click on a button of this form when the client browser automatically send the values of hidden text fields along with some parameter names from client side to server side all these hidden values of the hidden text fields in the form are automatically destroyed whenever the client close the browser page.

Program on simple servlet in Java

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;
import javax.servlet.http.httpsession;
public class firstses extends httpservlet
{  
public void doget(httpservletrequest request, httpservletresponse response)throws servletexception, ioexception
    {
         response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        try
        {
    string s1,s2;
    s1=request.getparameter("uname");
    s2=request.getparameter("pwd");
   httpsession sn= request.getsession();
    sn.setattribute("sname",s1);
    sn.setattribute("spwd",s2);
    out.println("<a href=secondses>click here</a>");   
               
        }
              catch(exception e)
      {
          out.println("exception"+e);
               
    }
}
}

Program on hidden1 forms in Java

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 hidden1 extends httpservlet { 
      
    public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
               response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        try
        {
          string s1,s2;
          s1=request.getparameter("sname");
          s2=request.getparameter("spwd");
          out.println("welcome to hidden1");
          out.println("<form method=get action=hidden2>");
          out.println("<input type=hidden name=sname value='"+s1+"'>");
          out.println("enter ur age");
          out.println("<input type=text name=sage>");
          out.println("<input type=submit value=submit>");
          out.println("</form>");
                
        }
        catch(exception e)
        {
        out.println("exception"+e);
        }
            
    }
  }
  
Example on program on hidden2 in Java

imort 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 hidden2 extends httpservlet {
 
   public  void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
      response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
       try
       {
        string s1,s2,s3;
        s1=request.getparameter("sname");
        s3=request.getparameter("sage");
        out.println("welcome to hidden2");
        out.println("name is"+s1);
        out.println("age is"+s3);
        }
       catch(exception e)
       {
           out.println("exception"+e);
       }
       
}
}

Example  program on sessions in Java

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;
import javax.servlet.http.httpsession;
public class secondses extends httpservlet {
 
   public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception
     { 
              response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        string s3,s4;
        try
        { 
        httpsession sn=request.getsession();
         s3=(string) sn.getattribute("sname");
         s4=(string) sn.getattribute("spwd");
         out.println("name is"+s3);
         out.println("pwd is"+s4);
        }
catch(exception e)
      {
    out.println("exception"+e);
        }
    }
    }

Example  program on url1 rewriting in Java

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 url1 extends httpservlet {
    public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
       
        response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        try
        {
            string s1,s2;
            s1=request.getparameter("sname");
            s2=request.getparameter("spwd");
            out.println("welcome to url1");
            out.println("name is"+s1);
            out.println("password is"+s2);
            out.println("<a href='url2?sname="+s1+"&spwd="+s2+"'>click1</a>");
        }
       
        catch(exception e)
        {
            out.println("exception"+e);
        }
    }
}

Example  program on url2 rewriting in Java

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 url2 extends httpservlet {
      
    public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
         response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        try
        {
            string s1,s2;
            s1=request.getparameter("sname");
            s2=request.getparameter("spwd");
            out.println("welcome to url2");
             out.println("name is"+s1);
            out.println("password is"+s2);
            out.println("<a href='url3?sname="+s1+"'>click2</a>");
           
        }
        catch(exception e)
        {
            out.println("exception"+e);
        }    
     
    }
}

Example  program on url3 in Java

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 url3 extends httpservlet {  
  
    public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
     
         response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        try
        {
            string s3;
            s3=request.getparameter("sname");
            out.println("welcome to url3");
            out.println("name is "+s3);
        }
        catch(exception e)
        {
            out.println("exception"+e);
        }  
   
    }   
       
    }

jsp’s:  java server page

 jsp technology is used to design server side applications.
this technology is based on servelts technology.
the user at server side should write the required jsp code
that is plain text, html tags according to application requirements

architecture of jsp application:

the jsp engine automatically translate requested jsp file into one java file according to the jsp code vailable in jsp file.
the jsp engine automatically compile the above translated java file and generate one class file.this class file is just nothing but servlet class.
the jsp engine automatically create the servlet object of the servlet class, and process the  client request and send the response to requested client browser.

jsp tags:

1.if the user at server side want to insert java code in the jsp file then we must use the following jsp tags
jsp directive tags:

this jsp directive tags are used to insert import statements in the jsp file.
syntax:
<%@ page import=”java.sql.*”%>

jsp diclarative tags:

this  declarative tags are used to insert the required attributes body of the user defined methods into the class scope.
<% !  int a; 
       public void f1()
       {
       ………..
       ………...
       }
%>
jsp scriplets tags:

 these tags are used to insert any executable code into jsp servlet method
<%  string s1=request.getparameter(“name”);
    ………….
…………..
%>

jsp expression tags:

these tags are used to send the value of an expression as a response to the client browser.
<% =name %>

jsp action tags:

 these are some predefined jsp action tags that is ready made jsp action tags are available at the jsp engine. so that the user at server side can directly use these tags in any jsp’s file whenever the jsp action tag is encountered the jsp engine automatically perform the respective predefined function all these tags should start with one prefix called jsp followed by the name of the action tag.
< jsp:forward>……………..</jsp:forward  >
< jsp:include >……………..</ jsp:include >
< jsp:param >………………</ jsp:param >
all these tags can be used outside the script lets of any jsp file then the jsp engine will insert the corresponding ready made java code of these tags into jsp service method.

Example program on JSP in Java

<html>
<body>
<form method=”get” action=print.jsp>
uname<input type=”text” name=”uname”>
pwd<input type=”text” name=”pwd”>
<input type=”submit” value=”submit”>
</form>
</body>
</html>
print.jsp
<%@ page import=”javax.servlet.*” %>
<html><body> welcome
<% string s1=request.getparameter(“uname”);
       string s2=request.getparameter(“pwd”);
      %>
<%=s1%>
  u r name is       
<%=s1%>
       
        u r pwd is
<%=s2%>
bye
</body>
</html>
ex2:
<html>
<body>
<form method=”get” action=first.jsp>
sname<input type=”text” name=”sname”>
saddr<input type=”text” name=”saddr”>
sage<input type=”text” name=”sage”>
<input type=”submit” value=”submit”>
</form>
</body>
</html>
       
first.jsp
<%@ page import=”javax.servlet.*” %>
<html><body> welcome
<% string s1=request.getparameter(“sage”);
 int ag=integer.parseint(s1);
   if(age>18)
{
      %>
<jsp: forward page=”major.jsp”>
<jsp:param name=”msg”value=”welcome”>
<jsp:param name=”wish”value=”how are you”>
</jsp: forward>
<%
}
else
{
%>
<jsp: include page=”minor.jsp”>
<jsp:param name=”msg1”value=”sorry”>
<jsp: include >
}
%>

major.jsp
<% @ page import=”javax.servlet.*”>
<% string s1=request.getparameter(“sname”);
     string s1=request.getparameter(“sddr”);
    string s1=request.getparameter(“msg”);
    string s1=request.getparameter(“wish”);
   %>
welcome
<%=s1%>
u r address is < %=s2%>
<%=s4%>
minor.jsp
<% @ page import=”javax.servlet.*”>
<% string s1=request.getparameter(“sname”);
     string s1=request.getparameter(“sddr”);
    string s1=request.getparameter(“msg”);
    string s1=request.getparameter(“wish”);
   %>
welcome
<%=s1%>
u r address is < %=s2%>
<%=s4%>

                                                       For Part2  Click here

For Frequently asked Java Programs Click here

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment