Ex. No. :01 Advanced Java Programming Lab (J2EE) 25.01.2011 Simple Servlet Showing Different Styles of a Phrase 1
UML NOTATION 2
SOURCE CODE //simple servlet application showing various styles of a text import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DiffShadowsServlet extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); out.println("<html>"); out.println("<head>"); out.println("<title>servlet DiffShadowsServlet</title>"); out.println("<style>"); out.println("h1width:50%"); out.println("h4width:100%"); out.println("</style>"); out.println("</head>"); out.println("<body>"); out.println("<h1 style=filter:dropshadow()>god IS GREAT</h1>"+"dropshadow effect"); out.println("<h1 style=filter:shadow()>god IS GREAT</h1>"+"shadow effect"); 3
out.println("<h1 style=filter:fliph()>god IS GREAT</h1>"+"flip horizontal"); out.println("<h1 style=filter:flipv()>god IS GREAT</h1>"+"flip vertical"); out.println("<h1 style=filter:glow()>god IS GREAT</h1>"+"glow effect"); out.println("<h1 style=filter:wave(strength=3)>god IS GREAT</h1>"+"wave effect"); out.println("</body>"); out.println("</html>"); out.close(); 4
SAMPLE SCREEN 5
Ex. No. : 02 25.01.2011 Advanced Java Programming Lab (J2EE) Displaying Multiplication Table in Servlet for a Number Entered in Html Page (Html to Servlet Communication) 6
UML NOTATION 7
SOURCE CODE <!..Multiplication Table..!> <html> <head> <title>multiplication Table</title> </head> <body background="c:\documents and Settings\ANNAI\WebApplication1\web\circles.png"> <form action="http://localhost:8084/webapplication1/tableservlet" method="dopost" > <br> <br> <br> <b> <font color="red" size="10"> Enter the number to which you want to display the multiplication table</b> <input type="text" name="tbltxt" title="enter a number" ><br> <input type="submit" name="tblbut" value="display MULTIPLICATION TABLE"> </font> </form> </body> </html> 8
SAMPLE SCREEN 9
SOURCE CODE //Displaying Multiplication Table import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TableServlet extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html"); PrintWriter out = response.getwriter(); int i; int number=integer.parseint(request.getparameter("tbltxt")); out.println("<strong><u>"+"displaying the multiplication table for the number: "+number+"</u></strong><br>"); for(i=1;i<=10;i++) out.println("<font color=abcdef size=5>"+i+" * "+number+" = "+(i*number)+"<br>"); out.close(); 10
SAMPLE SCREEN 11
Ex. No. : 03 27.01.2011 Advanced Java Programming Lab (J2EE) Manipulating Strings in Servlet Entered in Html (Html to Servlet Communication) 12
UML NOTATION 13
SOURCE CODE <!.. String Manipulation..!> <html> <head> </head> <body BGCOLOR="BABABA"> <form method="post" action="http://localhost:8084/webapplication1/stringservlet"> ENTER THE STRING HERE-----> <input type="text" name="t1" value="" TITLE="ENTER TEXT HERE!.."> <BR>CHOOSE AN OPERATION HERE <BR> LENGTH <input type="submit" name="r1" value="finding Length"><BR> REVERSE<input type="submit" name="r2" value="reversing the String"><BR> UPPERCASE <input type="submit" name="r3" value= "To Upper Case"><BR> LOWERCASE <input type="submit" name="r4" value="to Lower Case"><BR> NUMBER OF WORDS <input type="submit" name="r5" value="counting Number of Words"> </form> </body> </html> 14
SAMPLE SCREEN 15
SOURCE CODE //String Manipulation import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class StringServlet extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html"); PrintWriter out = response.getwriter(); String str=request.getparameter("t1"); out.println("<a href=stringmaniphtml.html>backk</a><br>"); String ui; String xx[]=new String[2]; int i=0; Enumeration e=request.getparameternames();//this stores all the component names as a collection while(e.hasmoreelements()) ui=(string)e.nextelement(); xx[i]=request.getparameter(ui); i++; 16
if(xx[1].equalsignorecase("to Lower Case")) out.println("the Operation Chosen is :"+"<font color=red size=5>"+xx[1]+"</font>"); out.println("<br>the String entered is :"+"<font color=ffbbee size=5>"+xx[0]+"</font>"); out.println("<hr color=purple><br>"); out.println("<br><font size=6 color=12345>"); out.println("the RESULT IS :"+xx[0].tolowercase()); else if(xx[0].equalsignorecase("finding Length")) out.println("the Operation Chosen is :"+"<font color=red size=5>"+xx[0]+"</font>"); out.println("<br>the String entered is :"+"<font color=ffbbee size=5>"+xx[1]+"</font>"); out.println("<hr color=purple><br>"); out.println("<br><font size=6 color=12345>"); out.println("the RESULT IS :"+xx[1].length()); else if(xx[0].equalsignorecase("reversing the String")) out.println("the Operation Chosen is :"+"<font color=red size=5>"+xx[0]+"</font>"); out.println("<br>the String entered is :"+"<font color=ffbbee size=5>"+xx[1]+"</font>"); 17
out.println("<hr color=purple><br>"); out.println("<br><font size=6 color=12345>"); StringBuffer sb=new StringBuffer(xx[1]); out.println("the RESULT IS :"+sb.reverse());; else if(xx[0].equalsignorecase("to Upper Case")) out.println("the Operation Chosen is :"+"<font color=red size=5>"+xx[0]+"</font>"); out.println("<br>the String entered is :"+"<font color=ffbbee size=5>"+xx[1]+"</font>"); out.println("<hr color=purple><br>"); out.println("<br><font size=6 color=12345>"); out.println("the RESULT IS :"+xx[1].touppercase()); else if(xx[1].equalsignorecase("counting Number of Words")) out.println("the Operation Chosen is :"+"<font color=red size=5>"+xx[1]+"</font>"); out.println("<br>the String entered is :"+"<font color=ffbbee size=5>"+xx[0]+"</font>"); out.println("<hr color=purple><br>"); out.println("<br><font size=6 color=12333>"); int len=xx[0].length(); int wcount=0; for(i=0;i<len;i++) 18
if(xx[0].charat(i)==' ') wcount=wcount+1; wcount++; out.println("the RESULT IS :"+wcount); 19
SAMPLE SCREEN 20
Ex. No. : 04 02.02.2011 Advanced Java Programming Lab (J2EE) Designing a Login Form Using Html and Displaying the Contents of the Login Form along with Date and Time in Servlet (Html to Servlet Communication) 21
UML NOTATION 22
SOURCE CODE <!..login html form..!> <html> <head> <title>html authenticator</title> </head> <body> <form action="http://localhost:8084/webapplication1/trialservletonhtml" method="dopost"> <table border="5" align="center" > <th><font size=9 color=red>log IN SCREEN</font></th> <tr> <td> <b><font size=6>user Name </font></b> <input type="text" name="mytext1" value="" title="enter User Name"> <td> </tr> <tr> <td> <b><font size=6> Password </font></b><input type="password" name="mytext2" value="" Title="Enter Password"> <td> </tr> <tr> <td align="center"> <b><font size=7><centre><input type="submit" name="mysubmit" value="submit" align="middle"><input type="reset" name="myreset" value="cancel"> </font></b> 23
<td> </tr> </table> </form> </body> </html> 24
SAMPLE SCREEN 25
SOURCE CODE // Displaying the Contents of the Login Form along with Date and Time, authenticator with out //validation import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class TrialServletOnHtml extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); Date dd=new Date(); out.println(dd); Calendar c=calendar.getinstance(); out.println("<br><b><font color=blue><u><i>date TODAY IS:</i></u></b></font>"); out.println(c.get(calendar.date)+":"); out.println(c.get(calendar.month)+":"); out.println(c.get(calendar.year)); out.println("<br><b><font color=red ><u><i>time NOW IS:</i></u></b></font>"); out.println(c.get(calendar.hour)+":"); out.println(c.get(calendar.minute)+":"); out.println(c.get(calendar.second)); 26
out.println("<hr color=ff0ff0>"); out.println("<br>" ); out.println("<b><i>you are now logged into "+"<font color=red><u>"+request.getrequesturl()+"</u></font></i></b>"); out.println("<br><br>user NAME : "+request.getparameter("mytext1")); out.println("<br>password : "+request.getparameter("mytext2")); out.close(); 27
SAMPLE SCREEN 28
Ex. No. : 05 03.02.2011 Advanced Java Programming Lab (J2EE) Registering a New User and Displaying the Number of Visits Made by the Existing User using Cookies (Html to Servlet Communication) 29
UML NOTATION 30
SOURCE CODE <!.. Registering a New User and Displaying the Number of Visits Made by the Existing User( using Cookies)..!> <html> <head> <title>cs APPLICATIONS </title> </head> <body> <marquee><b><font color =brown size=70> COMPUTER INDUSTRIES </font></b> </marquee> <form action="http://localhost:8084/webapplication1/cook1servlet" method="dopost"> Enter your name to check the number of visits made by you in this site <input type="text" name="txt"> <input type="submit" name="sub" value="checker"> </body> </html> 31
SAMPLE SCREEN 32
SOURCE CODE // Registering a New User and Displaying the Number of Visits Made by the Existing // User(using Cookies) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Cook1Servlet extends HttpServlet String name; public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException PrintWriter pw=res.getwriter(); name=req.getparameter("txt"); String cnt="1"; int chk=0; String nwcount=""; Cookie c=new Cookie(name,cnt); c.setmaxage(24*60*60); res.addcookie(c); 33
Cookie[] storecookie=req.getcookies(); int i; int count; if(storecookie!=null) for(i=0;i<storecookie.length;i++) String cname=storecookie[i].getname(); String coccur=storecookie[i].getvalue(); if(name.equals(cname)) count=integer.parseint(coccur); count=count+1; nwcount=integer.tostring(count); Cookie cc=new Cookie(cname,nwcount); 34
cc.setmaxage(24*60*120); res.addcookie(cc); chk=1; if(chk==1) pw.println("welcome, <b><i><font size=5 color=red face=tahoma>"+name+" </font></i></b>you are NOT a new Visitor and you have been visiting for "+nwcount+" times"); else pw.println("welcome, <b><i><font size=5 color=red face=tahoma>"+name+" </font></i></b> You are a new visitor and you are registered"); 35
SAMPLE SCREEN 36
Ex. No. : 06 09.02.2011 Advanced Java Programming Lab (J2EE) Finding the Presence of a value and its Position in the Cookie List, otherwise Registering the Value as a Cookie (Html to Servlet Communication) 37
UML NOTATION 38
SOURCE CODE <!.. Finding the Presence of a value and its Position in the Cookie List, otherwise Registering the Value as a Cookie..!> <html> <head> <title>cookies</title> </head> <marquee direction="up"><font color="green">finding the presence of an entered value in the form of cookie and it lasts for some days</font></marquee> <body> <form action="http://localhost:8084/webapplication1/storecookieservlet" method="post"> ENTER SOMETHING TO SET AS COOKIE <input type="text" name="txt"> <input type="submit" name="sub" value="press"> </form> </body> <marquee direction="down"><font color="green">finding the presence of an entered value in the form of cookie and it lasts for some days</font></marquee> </html> 39
40
SOURCE CODE // Finding the Presence of a value and its Position in the Cookie List, otherwise Registering the //Value as a Cookie import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class StoreCookieServlet extends HttpServlet public void service(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException PrintWriter pw=res.getwriter(); String name=req.getparameter("txt"); pw.println("<b><font size=6 color=orange>the entered value to set as a cookie is String cnt="1"; :</font><font size=7>"+name+"</font></b>"); Cookie cc=new Cookie(name,cnt); cc.setmaxage(24*60*60); res.addcookie(cc); pw.println("cookie saved"); Cookie[] storedcookie=req.getcookies(); int i; int count; if(storedcookie!=null) 41
pw.println("<br>there are some cookies found here already "); pw.println("<br>cookies saved so far!..."+storedcookie.length+"<br>"); for(i=0;i<storedcookie.length;i++) String cname=storedcookie[i].getname(); String coccur=storedcookie[i].getvalue(); pw.print("<br><font color=red>"+"at location "+i+" we have "+cname+"<hr>"); if(name.equals(cname)) pw.println("<font color=green>"+"the cookie is already exixting at "+i+" is"+cname); 42
SAMPLE SCREEN 43
44
Ex. No. : 07 10.02.2011 Advanced Java Programming Lab (J2EE) Mark List Processing in Servlet with Records Taken from MS-Access (Servlet and JDBC connectivity) 45
UML NOTATION 46
Table Description Table with Records 47
SOURCE CODE //JDBC Connectivity with MS-Access, Mark List Preparation import javax.servlet.http.*; import javax.servlet.*; import java.io.*; import java.sql.*; public class JdbcQuery extends HttpServlet public void service(httpservletrequest req,httpservletresponse res) throws ServletException,IOException PrintWriter out=res.getwriter(); res.setcontenttype("text/html"); Connection c=null; Statement s=null; ResultSet rs; int regno[]=new int[50]; String sname[]=new String[50]; int mark1[]=new int[50]; int mark2[]=new int[50]; int mark3[]=new int[50]; int total[]=new int[50]; 48
float avg[]=new float[50]; String result[]=new String[50]; int i=0; int j; try // driver selection and DSN (database) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // out.println("connected"); c=drivermanager.getconnection("jdbc:odbc:trialdb"); catch(exception e) out.println("runtime Error occured, fix it and is as shown by JRS as --"+e); try s=c.createstatement(); rs=s.executequery("select * from Table3"); out.println("god Is Great"); while(rs.next()) //retrieval of records 49
regno[i]=rs.getint(1); sname[i]=rs.getstring(2); mark1[i]=rs.getint(3); mark2[i]=rs.getint(4); mark3[i]=rs.getint(5); //processing the details if(mark1[i]>40 && mark2[i]>40 && mark3[i]>40) result[i]="pass"; else result[i]="fail"; total[i]=mark1[i]+mark2[i]+mark3[i]; avg[i]=total[i]/3; i=i+1; // showing the records in the table out.println("<u> <i> <font size=12><center>student Mark List Preparation</center></font></i></U>"); out.println("<table border=2 ALIGN = CENTER>"); out.println("<th><b><font color=blue>name</b></th>"); out.println("<th><b><font color=blue>register Number </b> </th>"); out.println("<th><b><font color=blue>j2ee</b> </th>"); out.println("<th><b><font color=blue>software Engineering </b> </th>"); 50
out.println("<th><b><font color=blue>operating System </b> </th>"); out.println("<th><b><font color=blue>total</b> </th>"); out.println("<th><b><font color=blue>average</b> </th>"); out.println("<th><b><font color=blue>result</b> </th>"); for(j=0;j<i;j++) out.println("<tr>"); out.println("<td >"+regno[j]+"</td>"); out.println("<td >"+sname[j]+"</td>"); out.println("<td align=center>"+mark1[j]+"</td>"); out.println("<td align=center>"+mark2[j]+"</td>"); out.println("<td align=center>"+mark3[j]+"</td>"); out.println("<td align=center>"+total[j]+"</td>"); out.println("<td align=center>"+avg[j]+"</td>"); out.println("<td align=center><b><font color=green>"+result[j]+"</font></td>"); out.println("</tr>"); out.println("</table>"); catch(exception E) System.out.println("Error!.., while fetching record and error description is shown as StackTrace of java "+E); 51
SAMPLE SCREEN 52
Ex. No.: 08 Advanced Java Programming Lab (J2EE) Viewing the Records Stored in Oracle Table 16.02.2011 (Servlet and JDBC Connectivity ) 53
UML NOTATION 54
Table Description 55
SOURCE CODE <!..form designing with UI to view records in Oracle table..!> <html> <head> <title>view</title> </head> <body> <form action="http://localhost:8084/webapplication1/viewserv" method="get"> <input type="submit" name="b1" value="view"> </form> </body> </html> 56
SAMPLE SCREEN 57
SOURCE CODE // viewing records in the servlet taken from Oracle table import java.io.*; import java.net.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ViewServ extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); Connection c=null; Statement s=null; ResultSet rs; if(request.getparameter("b1").equals("view")) try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 58
c=drivermanager.getconnection("jdbc:odbc:trialconnect","scott","tiger"); out.println("<b><font color=green size=7>god IS GREAT</font></b>"); out.println("<br> Records form LOGIN TABLE"); s=c.createstatement(); rs=s.executequery("select * from LOGINTABLE"); int i=1; while(rs.next()) out.println("<br>"); out.println(i); out.println(rs.getstring(1)); out.println(rs.getstring(2)); out.println(rs.getint(3)); out.println("<br>"); out.println("<hr>"); out.println("<br>"); i++; //try ends here catch(exception ee) 59
out.println("error has occured"+ee); //catch ends here 60
SAMPLE SCREEN 61
Ex. No. 09 Advanced Java Programming Lab (J2EE) Inserting Values in to Oracle Table 16.02.2011 (Servlet and JDBC Connectivity) 62
UML NOTATION 63
SOURCE CODE <!.. Form design with UI to insert values into Oracle Table..!> <html> <head> <title>insert</title> </head> <body> <form action="http://localhost:8084/webapplication1/insertserv" method="get"> Enter your Name: <input type="text" name="t1" value=""> <br> Enter Password : <input type="password" name="t2" value=""> <br> Enter Lucky Number: <input type="text" name="t3" value=""> <br> <input type="submit" name="b1" value="insert"> </form> </body> </html> 64
SAMPLE SCREEN 65
SOURCE CODE //Servlet to insert values taken from html form into Oracle table import java.io.*; import java.net.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class InsertServ extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); Connection c=null; Statement s=null; if(request.getparameter("b1").equals("insert")) try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); c=drivermanager.getconnection("jdbc:odbc:trialconnect","scott","tiger"); out.println("<b><font color=red size=7>god IS GREAT</font></b>"); s=c.createstatement(); 66
String nam=request.getparameter("t1"); int lnum=integer.parseint(request.getparameter("t3")); String pwd=request.getparameter("t2"); s.executeupdate("insert INTO LOGINTABLE"+ "(name, pwrd,luckynum)" + " VALUES ('"+nam+"','"+pwd+"',"+lnum+")"); out.println("<br>record Inserted!..."); //try ends here catch(exception ee) out.println("error has occured"+ee); //catch ends here 67
SAMPLE SCREEN 68
Values from Oracle Table 69
Ex. No. 10 16.02.2011 Advanced Java Programming Lab (J2EE) Deleting a record from Oracle Table (Servlet and JDBC Connectivity) 70
UML NOTATION 71
SOURCE CODE <!..Form Design with UI to get Name to Delete the appropriate record having the name from Oracle Table..!> <html> <head> <title>delete</title> </head> <body> <form action="http://localhost:8084/webapplication1/deleteserv" method="get"> Enter Name, which you want to delete : <input type="text" name="tt" > <br> <input type="submit" name="b1" value="delete"> </form> </body> </html> 72
SAMPLE SCREEN 73
SOURCE CODE // Servlet to Delete a record from Oracle Table when name is entered through the form shown //previously import java.io.*; import java.net.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class DeleteServ extends HttpServlet protected void service(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); Connection c=null; Statement s=null; String nam=null; ResultSet rs; try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); c=drivermanager.getconnection("jdbc:odbc:trialconnect","scott","tiger"); s=c.createstatement(); 74
out.println("god IS GREAT"); nam=request.getparameter("tt"); catch(exception e) System.out.println("THE ERROR:"+e); try s.executeupdate("delete from LOGINTABLE where Name="+"'nam'"); out.println("<br>record Deleted!..."); //try ends here catch(exception ee) out.println("error has occured :"+ee); //catch ends here 75
SAMPLE SCREEN 76
Ex. No. 11 17.02.2011 Advanced Java Programming Lab (J2EE) Applet Reading the Contents from Servlet and Changing the Colors of the Contents Using User Interfaces (Servlet to Applet Communication) 77
UML NOTATION UML NOTATION 78
SOURCE CODE //Servlet having the contents //servlet program import javax.servlet.*; import javax.servlet.http.*; public class TrialServlet extends HttpServlet protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException //response.setcontenttype("application"); PrintWriter out = response.getwriter(); //The following will be sent to Applet(AppServComm) to get displayed as a concatenated String out.println("1.god IS GREAT "); out.println("2.god IS DIVINE "); out.println("3.god is LOVE "); 79
SAMPLE SCREEN 80
SOURCE CODE //applet program to read contents from the servlet and display in the TextArea import java.applet.*; import java.awt.*; import java.net.*; import java.awt.event.*; import java.io.*; public class AppServComm extends Applet implements ActionListener,ItemListener Button b; Color cc; TextArea tf; String s; String ss=new String(); StringBuffer sb=new StringBuffer(); Checkbox r,bl,g; CheckboxGroup cg; public AppServComm() this.setlayout(null); setbackground(color.red); 81
setsize(1500,500); tf=new TextArea("THIS IS AN APPLET GENERATED OUTPUT",30,40); b=new Button("I will be connecting you a servlet"); cc=new Color(100,200,200); cg=new CheckboxGroup(); r=new Checkbox("RED",cg,true); bl=new Checkbox("BLUE",cg,false); g=new Checkbox("GREEN",cg,false); b.setbounds(200,200,270,30); b.setbackground(cc); add(b); r.setbounds(200,700,150,30) ; add(r); r.additemlistener(this); bl.setbounds(400,700,150,30) ; add(bl); bl.additemlistener(this); g.setbounds(600,700,150,30) ; add(g); g.additemlistener(this); tf.setbounds(200,400,550,130); tf.setbackground(cc); add(tf); 82
b.addactionlistener(this); public void itemstatechanged(itemevent i) if(i.getsource().equals(r)) tf.setforeground(color.red); else if(i.getsource().equals(bl)) tf.setforeground(color.blue); else if(i.getsource().equals(g)) tf.setforeground(color.green); public void actionperformed(actionevent a) try if(a.getactioncommand().equals("i will be connecting you a servlet")) tf.settext("god is divine and soul"); URL url=new URL(getCodeBase(),"http://localhost:8084/WebApplication1/TrialServlet"); URLConnection con=url.openconnection(); con.setdoinput(true); 83
DataInputStream dis=new DataInputStream(con.getInputStream()); this.showstatus("this is the output from SERVLET"); while((s=dis.readline())!=null) ss=sb.append(s).tostring(); //while ends tf.settext(ss); //if ends //try ends catch(exception e) System.out.println(e); //method ends here SAMPLE SCREEN 84
85
86
Ex. No. 12 23.02.2011 Advanced Java Programming Lab (J2EE) Sending Contents from a File to Servlet via Applet and Receiving the same from Servlet and Displaying in Applet's Textarea (Applet to Servlet Communicatin, File Handling) 87
UML NOTATION 88
SOURCE CODE //sending contents from a file to Servlet via Applet and receiving the same from Servlet and displaying in Applet's TextArea import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class AppletClass extends Applet implements ActionListener Button b,bb; String s; TextArea ta; Label l; URL url; URLConnection con; DataOutputStream dos; DataInputStream dis; FileInputStream in; byte[] buf; int bytesread = 0; public AppletClass() 89
setlayout(null); b=new Button("Send to servlet"); bb=new Button("From Servlet"); ta=new TextArea(50,50); l=new Label(); s="god is Great"; b.setbounds(300,300,100,20); add(b); bb.setbounds(300,400,100,20); add(bb); l.setbounds(500,350,350,20); add(l); ta.setbounds(900,300,300,200); add(ta); b.addactionlistener(this); bb.addactionlistener(this); public void actionperformed(actionevent myevent) try showstatus("god IS GREAT"); 90
in = new FileInputStream("d:/TrialFile.txt"); buf=new byte[in.available()]; url=new URL("http://localhost:8084/WebApplication1/ServletClass"); con=url.openconnection(); con.setdooutput(true); con.setusecaches (true); con.setdefaultusecaches (true); con.setdoinput(true); dos=new DataOutputStream(con.getOutputStream()); dis=new DataInputStream(con.getInputStream()); if(myevent.getsource().equals(b)) while( (bytesread = in.read( buf )) > -1 ) dos.write( buf, 0, bytesread ); dos.flush(); dos.close(); in.close(); 91
l.setbackground(color.red); l.setforeground(color.black); l.settext("content from the file has been sent to Servlet"); showstatus("sent "+dos.size()+" bytes of data"); //if ends else if(myevent.getsource().equals(bb)) ta.settext("god IS DIVINE") ; String s=" "; StringBuffer sb=new StringBuffer(s); String ss=new String(); int word=0; String sw=new String(); while((bytesread=dis.read())>-1) if(((char)bytesread)==' ') word++; ss=s.valueof((char)bytesread); 92
sb=sb.append(ss); showstatus("number of words read="+sw.valueof(word)); System.out.println(sb.toString()); l.setbackground(color.red); l.setforeground(color.black); l.settext("content from the SERVLET is now shown here in this TextArea"); ta.settext("god IS GREAT"); ta.setbackground(color.orange); ta.setforeground(color.black); ta.settext(sb.tostring()); //try ends catch(exception e) System.out.print("MY EXCEPTION:"+e); //method ends //class ends 93
SAMPLE SCREEN TrialFile.txt 94
95
Ex. No. : 13 24.02.2011 Advanced Java Programming Lab (J2EE) Servlet program to read contents from file and write the same into another file and also display the read contents on the browser (Servlet and File Manipulation) 96
UML NOTATION 97
SOURCE CODE //Servlet program to read contents from file(trialfile.text) and write the same into another file(writefile.text) and also display the read contents on the browser with some customization import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; public class ServletClass extends HttpServlet public void service(httpservletrequest req,httpservletresponse res) //ServletContext sc = this.getservletcontext(); try PrintWriter out=res.getwriter(); FileInputStream fis=new FileInputStream("d:/TrialFile.txt"); 98
FileOutputStream tofile = new FileOutputStream("d:/WriteFile.txt" ); byte[] buff = new byte[1024]; int cnt = 0; String s=null; //writting data from Myfile.txt to Filr2.txt while( (cnt = fis.read(buff )) > -1 ) tofile.write( buff, 0, cnt ); //reading data from Filr2.txt and showing the same on the browser FileReader fr=new FileReader("d:/Filr2.txt"); int c=0; int i; out.println("<marquee direction=right><font color=yellow size=7 face=forte>god IS GREAT</marquee>"); out.println("<body bgcolor=black>"); out.println("<br><b><u><font size=4 color=blue face=algerian>contents read from a file</font></u></b><br>"); out.println("<p><font size=3 color=red face=tahoma>"); while((i=fr.read())>-1) out.println((char)i); 99
c++; out.println("<br><font size=5 color=green face=forte><u>number of characters read ="+c); catch(exception e) e.printstacktrace(); 100
SAMPLE SCREEN TrialFile.txt WriteFile.txt 101
Showing the contents on the brower 102
Ex. No. : 14 02.03.2011 Advanced Java Programming Lab (J2EE) Making a Class Serializable and Sending Data as Object from Applet to Servlet (Using Serializable Interface) 103
UML NOTATION UML NOTATION 104
SOURCE CODE //making a class serializable and sending data as object import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; public class SeriallizableAppClass extends Applet implements ActionListener Button b; String s; URL url; URLConnection con; TrialSerial ts; DataOutputStream dos; public SeriallizableAppClass() b=new Button("Send to servlet"); s="god is Great"; add(b); 105
b.addactionlistener(this); public void actionperformed(actionevent myevent) if(myevent.getsource().equals(b)) try showstatus("god IS GREAT"); url=new URL("http://localhost:8084/WebApplication1/SerializableServletClass"); con=url.openconnection(); con.setdooutput(true); con.setusecaches (true); con.setdefaultusecaches (true); ts=new TrialSerial(); s="this is the serialized data set to Servlet (SerializableServletClass): "+ts.sendstring(); ObjectOutputStream oos=new ObjectOutputStream(con.getOutputStream()); 106
oos.writeobject(ts); repaint(); oos.close(); showstatus("sent data"); catch(exception e) System.out.println("MY EXCEPTION:"+e); //if ends //method ends public void paint(graphics gg) gg.drawstring(s,300,300); class TrialSerial implements Serializable public String S; 107
public TrialSerial() S="GOD IS DIVINE SOUL AND LOVE"; String sendstring() return(s); 108
SAMPLE SCREEN 109
Ex. No. 15 Advanced Java Programming Lab (J2EE) 03.03.2011 Simple JSP showing increased font size 110
UML NOTATION 111
SOURCE CODE <%-- simple jsp to display a phrase with increasing font size --%> <%@ page import="java.io.*"%> <html> <head> <title>trial JSP</title> </head> <body bgcolor=pink> <% out.println(new java.util.date());%> <% int i; for(i=1;i<=7;i++) out.write("<br>"); out.print("<font size="+i+">god IS GREAT"); // out.println("</font>"); %> <% %> </body> </html> 112
SAMPLE SCREEN 113
Ex. No. 16 Advanced Java Programming Lab (J2EE) 09.03.2011 Incorporating HTML in JSP 114
UML NOTATION 115
SOURCE CODE <!.. This html file will be used in jsp..!> <html> <marquee><big> This is an incorporated html's view</big></marquee> <ul >Unordered List <li>lotus</li> <li>sunflower</li> <li>rose</li> <li>daisy</li> <li>pansy</li> </ul> <ol > Ordered List <li>lotus</li> <li>sunflower</li> <li>rose</li> <li>daisy</li> <li>pansy</li> </ol> </html> 116
SAMPLE SCREEN 117
SOURCE CODE <%-- including html file--%> <%-- JSP using Flowers.html --%> <%@ page import="java.io.*"%> <html> <body> <h3><font color=green>flowers</font></h3> <%out.println("<font size=7 color=violet>god IS GREAT</font>");%> <%@ include file="flowers.html" %> </body> </html> 118
SAMPLE SCREEN 119
Ex. No. 17 09.03.2011 Advanced Java Programming Lab (J2EE) Printing Fibonacci Series (HTML, JSP, Servlet communication) 120
UML NOTATION 121
SOURCE CODE <html> <head> <title></title> </head> <body bgcolor=cyan> <form action="http://localhost:8084/webapplication1/jspfibo.jsp" method="post"> Enter a value and upto that Fibonocci series will be shown: <input type="text" name="txt" value="" title="enter a number to generate Fibonocci series"><br>    <input type="submit" name="but1" value="fibonocci SERIES GENERATOR"> </form> </body> </html> 122
SAMPLE SCREEN 123
SOURCE CODE <%@ page import="java.io.*"%> <%@ page import="java.util.*"%> <%@ page import="javax.servlet.*"%> <html> <head> <title>jsp Page</title> </head> <body> <% int n=0; String s; int ov=0; int cv=1; int nv=0; if(request.getparameter("txt")!=null) n=integer.parseint(request.getparameter("txt")); if(n!=0) out.println("<b><font size=5 color=blue>here is the generated FIBONOCCI SERIES </font></b>"); 124
out.println(ov); out.println(cv); out.println("<br><b><font size=5 color=red>"); do nv=ov+cv; ov=cv; cv=nv; if(nv<n) out.println(cv); while(nv<n); out.println("</font></b>"); out.println("<br><a href=http://localhost:8084/webapplication1/htmlandjsp.html>back to the place to enter value</a>"); %> </body></html> 125
SAMPLE SCREEN 126
Ex. No. 18 Advanced Java Programming Lab (J2EE) 10.03.2011 Client Server Communication using RMI 127
UML NOTATION 128
SOURCE CODE //DECLARING INTERFACE import java.rmi.*; public interface MyInterface extends Remote void printsomething()throws RemoteException; ---------------------------------------------------------------------------------------------------------------- //IMPLEMENTING INTERFACE(REMOTE) import java.rmi.*; import java.rmi.server.*; public class MyClassMyInterface extends UnicastRemoteObject implements MyInterface public MyClassMyInterface()throws RemoteException public void printsomething()throws RemoteException System.out.println("GOD IS GREAT"); ------------------------------------------------------------------------------------------------------------ 129
// PROGRAM TO BIND THE REMOTE CLASS WITH RMI REGISTRY import java.net.*; import java.rmi.*; public class MyServer public static void main(string myc[]) try MyClassMyInterface mi=new MyClassMyInterface(); Naming.rebind("MyClass",mi); catch(exception excep) System.out.println("Throwing runtime error, please catch it -----"+excep); ------------------------------------------------------------------------------------------------------ 130
// CLIENT PROGRAM TO CALL THE REMOTE METHOD import java.rmi.*; public class MyClient public static void main(string myc[]) try String str="rmi://"+myc[0]+"/myclass"; MyInterface min=(myinterface)naming.lookup(str); min.printsomething(); catch(exception excep) System.out.println("Throwing runtime error, please catch it -----"+excep); ------------------------------------------------------------------------------------------------------------- 131
SAMPLE SCREEN 132
Running Server Program 133
Running Client Program 134
135
Ex. No. 19 16.03.2011 Advanced Java Programming Lab (J2EE) Login Form Validation using JavaBeans (Invisible Component) (HTML, JSP, JavaBeans) 136
UML NOTATION 137
SOURCE CODE <!..Login Validation..!> <html> <head> <title>login</title> </head> <body bgcolor="green"> <form action ="http://localhost:8084/webapplication1/mylogjsp.jsp" method="post"> <table border="5"> <th bgcolor="red">go Ahead</th> <tr> <Td> Enter USER NAME <input type="text" name="txt" title="user name" value="test"> </td> </tr> <tr> <Td> Enter PASSWORD <input type="password" name="txtt" title="password" value="test"> </td> </tr> 138
<tr> <td>          <input type="submit" name="mit" value="submit"> <input type="reset" name="res" value="reset"> <td> </tr> </table> </form> </body> </html> 139
SAMPLE SCREEN 140
SOURCE CODE <%!Login JSP %> <jsp:usebean id="log" class="beanpkg.mylogbean" scope="request"/> <jsp:setproperty name="log" property="*"/> <html> <head> <title>login asp</title> <head> <body> <% String fs=request.getparameter("txt"); String ss=request.getparameter("txtt"); int i; String x=log.check(); if(x=="yes valid") out.println("<font size=12 color=grey><u>secret Revealed</u></font><br>"); 141
for(i=0;i<fs.length();i++) if(i%2==0) out.println("<font color=red size="+fs+">"+fs.charat(i)); else out.println("<font color=blue size="+fs+">"+fs.charat(i)); out.println("<br>"); for(i=0;i<ss.length();i++) if(i%2==0) out.println("<font color=green size="+fs+">"+ss.charat(i)); else out.println("<font color=violet size="+fs+">"+ss.charat(i)); else out.println("fill values on your own by clearing the text boxes,<a href=http://localhost:8084/webapplication1/myloghtml.html> GO BACK TO FILL</a>"); %> </body> </html> 142
SOURCE CODE // bean class defined here package beanpkg; import java.beans.*; import java.io.serializable; public class MyLogBean extends Object implements Serializable private String str1,str2; private int n; public MyLogBean() public void settxt(string val) str1=val; public void settxtt(string val) str2=val; public String check()throws Exception 143
if((str1.equals("test")) (str2.equals("test"))) return "Not Valid"; else return "yes valid"; 144
SAMPLE SCREEN 145
146