Advertisement
Guest User

Ajp

a guest
Oct 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.78 KB | None | 0 0
  1. FACULTY OF SCIENCE AND HUMANITIES
  2. DEPARTMENT OF COMPUTER SCIENCE
  3. KATTANKULATHUR -603 203
  4.  
  5. LAB RECORD
  6.  
  7. NAME
  8.  
  9. : _____________________________________________
  10.  
  11. REGNO
  12.  
  13. : _____________________________________________
  14.  
  15. CLASS
  16.  
  17. : B.SC. COMPUTER SCIENCE
  18.  
  19. SEMESTER, MONTH & YEAR : V Semester – October 2018
  20.  
  21. SUBJECT CODE & TITLE
  22.  
  23. : UCS15504 - ADVANCED JAVA PROGRAMMING
  24. LABORATORY
  25.  
  26. October 2018
  27.  
  28. FACULTY OF SCIENCE AND HUMANITIES
  29. DEPARTMENT OF COMPUTER SCIENCE
  30.  
  31. CERTIFICATE
  32. This is to certify that this is a bonafide record of practical work done by
  33.  
  34. NAME
  35.  
  36. : _____________________________________________
  37.  
  38. REGNO
  39.  
  40. : _____________________________________________
  41.  
  42. CLASS
  43.  
  44. : _____________________________________________
  45.  
  46. SEMESTER, MONTH & YEAR : V Semester – October 2018
  47. SUBJECT CODE & TITLE
  48.  
  49. : UCS15504 - ADVANCED JAVA PROGRAMMING
  50. LABORATORY
  51.  
  52. During the Academic Year 20182019 and Submitted for B.Sc. Degree practical
  53. Examination held on ___________________________.
  54.  
  55. LECTURER INCHARGE
  56.  
  57. INTERNAL EXAMINER
  58.  
  59. H.O.D.
  60.  
  61. EXTERNAL EXAMINER
  62.  
  63. TABLE OF CONTENTS
  64. Ex. No.
  65.  
  66. Date
  67.  
  68. Title
  69.  
  70. Page No.
  71.  
  72. 1.
  73.  
  74. 25-06-18
  75.  
  76. Displaying records From a DB
  77. Table
  78.  
  79. 2
  80.  
  81. 2.
  82.  
  83. 28-06-18
  84.  
  85. Inserting records to a database table
  86.  
  87. 5
  88.  
  89. 3.
  90.  
  91. 02-07-18
  92.  
  93. Updating records of a database table
  94.  
  95. 9
  96.  
  97. 4.
  98.  
  99. 09 -07-18
  100.  
  101. Deleting records from a database tale
  102.  
  103. 12
  104.  
  105. 5.
  106.  
  107. 12-07-18
  108.  
  109. Retrieving Metadata of a database
  110.  
  111. 15
  112.  
  113. 6.
  114.  
  115. 16-07-18
  116.  
  117. Exploring InetAddress Class
  118.  
  119. 20
  120.  
  121. 7.
  122.  
  123. 19-07-18
  124.  
  125. TCP Date Time Server
  126.  
  127. 23
  128.  
  129. 8.
  130.  
  131. 23-07-18
  132.  
  133. TCP Hello Server
  134.  
  135. 27
  136.  
  137. 9.
  138.  
  139. 30-07-18
  140.  
  141. RMI – Sum of Two Numbers
  142.  
  143. 32
  144.  
  145. 10.
  146.  
  147. 06-08-18
  148.  
  149. Factorial with RMI
  150.  
  151. 36
  152.  
  153. 11.
  154.  
  155. 09-08-18
  156.  
  157. Current Date And Time -Servlet
  158.  
  159. 41
  160.  
  161. 12.
  162.  
  163. 13-08-18
  164.  
  165. Generating Multiplication Table Servlet
  166.  
  167. 44
  168.  
  169. 13.
  170.  
  171. 21-08-18
  172.  
  173. HitCount of A WebPage
  174.  
  175. 48
  176.  
  177. 14.
  178.  
  179. 27-08-18
  180.  
  181. JDBC – Servlet Data Insertion
  182.  
  183. 51
  184.  
  185. 15.
  186.  
  187. 03-09-18
  188.  
  189. Generating prime numbers
  190.  
  191. 55
  192.  
  193. 16.
  194.  
  195. 10-09-18
  196.  
  197. Message Applet
  198.  
  199. 60
  200.  
  201. 17.
  202.  
  203. 12-09-18
  204.  
  205. Shape Applet
  206.  
  207. 62
  208.  
  209. 18.
  210.  
  211. 20-09-18
  212.  
  213. Keyboard Events Applet
  214.  
  215. 65
  216.  
  217. 19.
  218.  
  219. 26-09-18
  220.  
  221. Face Applet
  222.  
  223. 68
  224.  
  225. Staff Signature
  226.  
  227. JDBC
  228.  
  229. 1
  230.  
  231. Ex.No: 1
  232.  
  233. Displaying records From a DB Table
  234.  
  235. Date: 25-06-18
  236.  
  237. Aim:To write a java program to connect and retrieve records from a database table using
  238. a select query with the help of JDBC API.
  239.  
  240. Procedure:Step-1: Create a database using Apache’s Java DB , create a table and add a
  241. few records.
  242. Step-2: Start the DB Server.
  243. Step-3: .Create a java application in net beans. Create a java class in the
  244. source package (SRC).
  245. Step-4: Import the SQL package and load the EmbeddedDriver class of Apache.
  246. Create a connection between the database and the program.
  247. Step-5: Create the Statement object; pass the select query as the parameter.
  248. Step-6: Execute the Query and get the result into the ResultSet object.
  249. Step-7: Traverse through the ResultSet.
  250. Steps-8: Compile& Run the program.
  251.  
  252. 2
  253.  
  254. CODING:import java.sql.*;
  255. public class Main {
  256. public static void main(String[] args) {
  257. String n;
  258. int r;
  259. try
  260. {
  261. Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
  262. Connection con= DriverManager.getConnection("jdbc:derby://localhost:1527/Student",
  263. "nbuser","nbuser" );
  264.  
  265. Statement st=con.createStatement();
  266. ResultSet rs=con.executeQuery("select * from student");
  267. while(rs.next()){
  268. n=rs.getString("student name");
  269. r=rs.getInt("regno");
  270. System.out.println("Name "+n+"\t "+"regno "+r);
  271. }
  272. con.close();
  273. } catch(Exception e) {
  274. e.printStackTrace();
  275. }
  276. }
  277. }
  278.  
  279. 3
  280.  
  281. OUTPUT:-
  282.  
  283. Result:Thus the result is displayed.
  284.  
  285. 4
  286.  
  287. Ex.No: 2
  288.  
  289. Inserting records to a database table
  290.  
  291. Date: 28-06-18
  292.  
  293. Aim:To write a java program to connect and insert records in database table with the
  294. help of JDBC API.
  295.  
  296. Procedure:Step-1: Create a database using Apache’s Java DB , create a table and add a
  297. few records.
  298. Step-2: Start the DB Server.
  299. Step-3: Create a java application in net beans. Create a java class in the
  300. source package (SRC).
  301. Step-4: Import the SQL package and load the EmbeddedDriver.
  302. Create a connection between the database and the program.
  303. Step-5: Create the Prepared Statement object; pass the insert query as
  304. the parameter.
  305. Step-6: Execute the executeUpdate() method.
  306. Steps-7: Compile& Run the program.
  307.  
  308. 5
  309.  
  310. CODING:import java.sql.*;
  311. import java.util.*;
  312. public class Main {
  313. public static void main(String[] args) {
  314. String studname,gender,dept;
  315. int regno;
  316. Scanner in=new Scanner(System.in);
  317. System.out.println("Enter student name:");
  318. studname=in.nextLine();
  319. System.out.println("Enter department:");
  320. dept=in.nextLine();
  321. System.out.println("Enter gender:");
  322. gender=in.nextLine();
  323. System.out.println("Enter reg number:");
  324. regno=in.nextInt();
  325. try
  326. {
  327. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  328. Connection con=DriverManager.getConnection(
  329. "jdbc:derby://localhost:1527/Student","nbuser","nbuser");
  330.  
  331. PreparedStatement st=con.prepareStatement("Insert into
  332. student (studname,regno,dept,gender) values(?,?,?,?)");
  333. st.setString(1,studname);
  334. st.setInt(2,regno);
  335. st.setString(3,dept);
  336. st.setString(4,gender);
  337. st.executeUpdate();
  338.  
  339. 6
  340.  
  341. System.out.println("updated");
  342. con.close();
  343. }
  344. catch(Exception e)
  345. {
  346. e.printStackTrace();
  347. }
  348. }
  349. }
  350.  
  351. 7
  352.  
  353. OUTPUT:-
  354.  
  355. Result:Thus the result is displayed.
  356.  
  357. 8
  358.  
  359. Ex.No:3
  360.  
  361. Updating records of a database table
  362.  
  363. Date: 02-07-18
  364.  
  365. Aim:To write a java program to connect and update records of a database table with
  366. the help of JDBC API.
  367.  
  368. Procedure:Step-1: Create a database using Apache’s Java DB, create a table and add a few
  369. records.
  370. Step-2: Start the DB Server.
  371. Step-3: Create a java application in net beans. Create a java class in the
  372. source package (SRC).
  373. Step-4: Import the SQL package and load the EmbeddedDriver. Create a connection
  374. between the database and the program.
  375. Step-5: Create the Prepared Statement object; pass the update query as
  376. the parameter.
  377. Step-6: Execute the executeUpdate() method.
  378. Steps-7: Compile& Run the program.
  379.  
  380. 9
  381.  
  382. CODING:import java.sql.*;
  383. import java.util.*;
  384. public class Main {
  385. public static void main(String[] args) {
  386. int rno, newaddr;
  387. Scanner in=new Scanner(System.in);
  388. System.out.println("Enter the reg number");
  389. rno=in.nextInt();
  390. System.out.println("Enter the contact number");
  391. newaddr=in.nextInt();
  392. try {
  393. Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
  394. Connection
  395. con=DriverManager.getConnection("jdbc:derby://localhost:1527/Student","nbuser","nb
  396. user");
  397. PreparedStatement pst=con.prepareStatement("Update student set contact=?
  398. where regno=? ");
  399.  
  400. pst.setInt(1,newaddr);
  401. pst.setInt(2,rno);
  402. pst.executeUpdate();
  403. System.out.println("updated");
  404. con.close();
  405. } catch(Exception e){
  406. e.printStackTrace();
  407. }
  408. }
  409. }
  410.  
  411. 10
  412.  
  413. OUTPUT:-
  414.  
  415. Result:Thus the result is displayed.
  416.  
  417. 11
  418.  
  419. Ex.No: 4
  420.  
  421. Deleting records from a database tale
  422.  
  423. Date: 09 -07-18
  424.  
  425. Aim:To write a java program to connect and delete records from a database with the
  426. help of JDBC API.
  427.  
  428. Procedure:Step-1: Create a database using Apache’s Java DB, create a table and add a few
  429. records.
  430. Step-2: Start the DB Server.
  431. Step-3: Create a java application in net beans. Create a java class in the
  432. source package (SRC).
  433. Step-4: Import the SQL package and load the EmbededDriver. Create a connection
  434. between the database and the program.
  435. Step-5: Create the Prepared Statement object; pass the update query as
  436. the parameter.
  437. Step-6: Execute the executeUpdate() method.
  438. Steps-7: Compile& Run the program.
  439.  
  440. 12
  441.  
  442. CODING:import java.sql.*;
  443. import java.util.*;
  444. public class Main {
  445. public static void main(String[] args) {
  446. int rno;
  447. Scanner in=new Scanner(System.in);
  448. System.out.println("enter reg no:");
  449. rno=in.nextInt();
  450. try
  451. {
  452. Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
  453. Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/Student",
  454. "nbuser","nbuser ");
  455.  
  456. PreparedStatement st=con.prepareStatement("Delete from student
  457. where regno=?");
  458. st.setInt(1,rno);
  459. st.executeUpdate();
  460. System.out.println("deleted");
  461. con.close();
  462. }
  463. catch(Exception e)
  464. {
  465. e.printStackTrace();
  466. }
  467. }
  468.  
  469. }
  470.  
  471. 13
  472.  
  473. OUTPUT:-
  474.  
  475. Result:Thus the result is displayed.
  476.  
  477. 14
  478.  
  479. Ex.No: 5
  480.  
  481. Retrieving Metadata
  482.  
  483. Date: 12-07-18
  484.  
  485. Aim:To write a java program to connect and retrieve Metadata from a database using
  486. a select query with the help of JDBC API.
  487.  
  488. Procedure:Step-1: Create a database using Apache’s Java DB,create a table and add a few
  489. records.
  490. Step-2: Configure the DSN.
  491. Step-3: .Create a java application in net beans. Create a java class in the
  492. source package (SRC).
  493. Step-4: Import the SQL package and load the EmbeddedDriver. And create a
  494. connection between the database and the program.
  495. Step-5: Create the DatabaseMetaData object and use its built in methods to
  496. display the product name, product Name and major version respectively.
  497.  
  498. Step-6: Get result into the ResultSets object.
  499. Step-7: Traverse through the ResultSets.
  500. Steps-8: Compile& Run the program.
  501.  
  502. 15
  503.  
  504. CODING:import java.sql.*;
  505. import java.util.*;
  506. public class Main {
  507. public static void main(String[] args) {
  508. String catalog=null;
  509. String schema=null;
  510. String mytable=null;
  511. String[] types=null;
  512. try
  513. {
  514. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  515. Connection con=DriverManager.getConnection(
  516. "jdbc:derby://localhost:1527/Student","nbuser","nbuser ");
  517. DatabaseMetaData dm = con.getMetaData();
  518.  
  519. System.out.println("Product name:"+ dm.getDatabaseProductName());
  520. System.out.println("Product version:"+ dm.getDatabaseProductVersion());
  521.  
  522. System.out.println("Driver version:"+ dm.getDriverMajorVersion());
  523. ResultSet rs= dm.getTables(catalog,schema,mytable,types);
  524. while(rs.next())
  525. {
  526. System.out.println(rs.getString(3));
  527. }
  528. Scanner in=new Scanner(System.in);
  529. System.out.println("Any specific table?");
  530. mytable=in.nextLine();
  531. ResultSet rs2=su.getTables(catalog,schema,mytable,types);
  532.  
  533. 16
  534.  
  535. while(rs2.next())
  536. {
  537. System.out.println(rs2.getString(4));
  538.  
  539. System.out.println(rs2.getInt(5));
  540. }
  541. con.close();
  542. }
  543. catch(Exception e)
  544. {
  545. e.printStackTrace();
  546. }
  547. }
  548. }
  549.  
  550. 17
  551.  
  552. OUTPUT:-
  553.  
  554. Result:Thus the result is displayed.
  555.  
  556. 18
  557.  
  558. NETWORKING
  559.  
  560. 19
  561.  
  562. Ex.No: 6
  563.  
  564. Exploring InetAddress Class
  565.  
  566. Date: 16-07-18
  567.  
  568. Aim:To write a java program to IP of a few given sites using InetAddress class.
  569.  
  570. Procedure:Step-1: Create a java application in net beans. Create a java class in the
  571. source package (SRC).
  572. Step-2: Import the net package.
  573. Step-3: Create the InetAddress objects and use getLocalHost (), getAllByName()
  574. and getByName () to retrieve and display the ip addresses.
  575.  
  576. Step-4: Traverse through the maddr[] array for getAllByName().
  577. Steps-5: Compile& Run the program.
  578.  
  579. 20
  580.  
  581. CODING:import java.net.*;
  582. public class Main {
  583. public static void main(String[] args) {
  584. try
  585. {
  586. InetAddress addr=InetAddress.getLocalHost();
  587. System.out.println("local"+addr);
  588. addr=InetAddress.getByName("www.srmuniv.ac.in");
  589. System.out.println(addr);
  590. InetAddress maddr[]=InetAddress.getAllByName("www.gmail.com");
  591.  
  592. for(int i=0;i<maddr.length;i++)
  593. System.out.println(maddr[i]);
  594. }
  595. catch(Exception e)
  596. {
  597. e.printStackTrace();
  598. }
  599.  
  600. }
  601. }
  602.  
  603. 21
  604.  
  605. OUTPUT:-
  606.  
  607. Result:Thus the result is displayed.
  608.  
  609. 22
  610.  
  611. Ex.No:7
  612.  
  613. TCP Date Time Server
  614.  
  615. Date: 19-07-18
  616.  
  617. Aim:To write a TCP server application that will give the current date and time when
  618. it receives a client request.
  619.  
  620. Procedure:Step-1: Create a java application in net beans. Create a java class in the
  621. source package (SRC).
  622. Step-2:Import the net, util and io package.
  623. Step-3: Write the server program in which create a server socket such that
  624. takes the request and returns the current date and time.
  625. Step-4: Create a java application in net beans. Create a java class in the
  626. source package (SRC).
  627. Step-5:Import the net and io package.
  628. Step-6: Write the client program in which create socket such that takes the
  629. request from the local host and displays it.
  630. Step-7: Compile both the server and client program.
  631. Step-8: Run the server program first and then the client program.
  632.  
  633. 23
  634.  
  635. CODING:SERVER CODE:import java.io.*;
  636. import java.net.*;
  637. import java.util.*;
  638. public class Main {
  639. public static void main(String[] args)throws IOException
  640. { ServerSocket server=new ServerSocket(8888);
  641. while(true)
  642. {
  643. System.out.println("Waiting for client to
  644. connect.."); Socket socket=server.accept();
  645. PrintWriter out=new
  646. PrintWriter(socket.getOutputStream(),true);
  647. out.println("Hello"+new Date()); }
  648. }
  649. }
  650.  
  651. 24
  652.  
  653. CLIENT CODE:import java.io.*;
  654. import java.net.*;
  655.  
  656. public class Main {
  657. public static void main(String[] args) {
  658. try {
  659. Socket socket=new Socket("localhost",8888);
  660.  
  661. BufferedReader in=new BufferedReader(new
  662. InputStreamReader(socket.getInputStream()));
  663. String msg=in.readLine();
  664. System.out.println("Server"+msg);
  665. }
  666. catch(IOException ioe)
  667. {ioe.printStackTrace();}
  668. }
  669. }
  670.  
  671. 25
  672.  
  673. OUTPUT:SERVER:-
  674.  
  675. CLIENT:-
  676.  
  677. Result:Thus the result is displayed.
  678.  
  679. 26
  680.  
  681. Ex.No: 8
  682.  
  683. TCP Hello Server
  684.  
  685. Date: 23-07-`18
  686.  
  687. Aim:To write a TCP server that display a welcome message to a client
  688.  
  689. Procedure:Step-1: Create a java application in net beans. Create a java class in the
  690. source package (SRC).
  691. Step-2:Import the net, util and io package.
  692. Step-3: Write the server program in which create a server socket such that
  693. takes the request and returns the current date and time.
  694. Step-4: Create a java application in net beans. Create a java class in the
  695. source package (SRC).
  696. Step-5:Import the net and io package.
  697. Step-6: Write the client program in which create socket such that takes the
  698. request from the local host and displays it.
  699. Step-7: Compile both the server and client program.
  700. Step-8: Run the server program first and then the client program.
  701.  
  702. 27
  703.  
  704. CODING:SERVER CODE:-
  705.  
  706. package HelloSocket;
  707. import java.io.*;
  708. import java.net.*;
  709. import java.util.*;
  710. public class HelloServer {
  711. public static void main(String[] args) throws Exception {
  712. ServerSocket serv = new ServerSocket(8888);
  713. while(true)
  714. {
  715. System.out.println("Waiting for Server");
  716. Socket s = serv.accept();
  717. PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
  718. pw.println("Hello World");
  719. } }
  720. }
  721.  
  722. 28
  723.  
  724. CLIENT CODE:-
  725.  
  726. package HelloSocket;
  727. import java.io.*;
  728. import java.net.*;
  729. public class HelloClient {
  730. public static void main(String[] args)throws Exception {
  731. Socket s = new Socket("LocalHost", 8888);
  732. BufferedReader in = new BufferedReader(new
  733. InputStreamReader(s.getInputStream()));
  734. String msg = in.readLine();
  735. System.out.println("from Server:" + msg);
  736. }
  737. }
  738.  
  739. \\
  740.  
  741. 29
  742.  
  743. OUTPUT:-
  744.  
  745. Result:Thus the result is displayed.
  746.  
  747. 30
  748.  
  749. RMI
  750.  
  751. 31
  752.  
  753. Ex.No: 9
  754.  
  755. RMI – Sum of Two Numbers
  756.  
  757. Date: 30-07-18
  758.  
  759. Aim:To write a java programs in which a server and client communicate with each
  760. other through RMI and calculate the sum of the 2 given numbers.
  761.  
  762. Procedure:Step-1: Create a java application in net beans. Create a java interface in the
  763. source package (SRC).
  764. Step-2: Write the interface which extends the Remote of the rmi package.
  765. Step-3: Create a java class in the source package (SRC) and Import the
  766. rmi, rmi.registry and rmi.server package.
  767. Step-4: Write the server program and create the registry in which implements
  768. the interface and extends UnicatRemoteObject.
  769. Step-5: Create a java class in the source package (SRC) and import the rmi
  770. and util package.
  771. Step-6: Write the client program in which creates the object of the interface
  772. and call the function with the object.
  773. Step-7: Compile the interface, server and client program.
  774. Step-8: Run the server program first and then the client program.
  775.  
  776. 32
  777.  
  778. CODING:REMOTE INTERFACE:import java.rmi.*;
  779. public interface serinterface extends Remote
  780. {
  781. public int add(int x,int y)throws RemoteException;
  782. }
  783. SERVER CODE:import java.rmi.*;
  784. import java.rmi.server.*;
  785. import java.rmi.registry.*;
  786. public class ImplAdd extends UnicastRemoteObject implements serinterface
  787. { public ImplAdd() throws RemoteException
  788. {
  789. super();
  790. }
  791. public int add(int x,int y)throws RemoteException
  792. {
  793. return x+y;
  794. }
  795. public static void main(String[] args)throws RemoteException
  796. { ImplAdd obj=new ImplAdd();
  797. Registry r=java.rmi.registry.LocateRegistry.createRegistry(1099);
  798. r.rebind("server",obj);
  799.  
  800. System.out.println("Ready .....");
  801. }
  802. }
  803.  
  804. 33
  805.  
  806. CLIENT CODE:import java.rmi.*;
  807. import java.util.*;
  808. import java.rmi.Naming;
  809. public class rmiclient {
  810. public rmiclient() {
  811. }
  812. public static void main(String[] args){
  813. try
  814. {
  815. serinterface
  816. obj=(serinterface)Naming.lookup("server"); int a,b;
  817. Scanner in=new Scanner(System.in);
  818. System.out.println("Enter 2 number:");
  819. a=in.nextInt();
  820. b=in.nextInt();
  821. int sum=obj.add(a,b);
  822. System.out.println("Sum="+sum);
  823. }
  824. catch(Exception e)
  825. {
  826. e.printStackTrace();
  827. }
  828. }
  829.  
  830. }
  831.  
  832. 34
  833.  
  834. OUTPUT:SERVER :-
  835.  
  836. CLIENT:-
  837.  
  838. Result:Thus the result is displayed.
  839.  
  840. 35
  841.  
  842. Ex.No: 10
  843.  
  844. Factorial with RMI
  845.  
  846. Date: 06-08-18
  847.  
  848. Aim:To write a java programs in which a server and client communicate with each
  849. other through RMI and calculate the factorial.
  850.  
  851. Procedure:Step-1: Create a java application in net beans. Create a java interface in the
  852. source package (SRC).
  853. Step-2: Write the interface which extends the Remote of the rmi package.
  854. Step-3: Create a java class in the source package (SRC) and Import the
  855. rmi, rmi.registry and rmi.server package.
  856. Step-4: Write the server program and create the registry in which implements
  857. the interface and extends UnicatRemoteObject.
  858. Step-5: Create a java class in the source package (SRC) and import the rmi
  859. and util package.
  860. Step-6: Write the client program in which creates the object of the interface
  861. and call the function with the object.
  862. Step-7: Compile the interface, server and client program.
  863. Step-8: Run the server program first and then the client program.
  864.  
  865. 36
  866.  
  867. CODING:INTERFACE CODE:import java.rmi.*;
  868. public interface ser extends Remote {
  869. public int fact(int x)throws RemoteException;
  870. }
  871. SERVER CODE:import java.rmi.*;
  872. import java.rmi.server.*;
  873. import java.rmi.registry.*;
  874. public class serf extends UnicastRemoteObject implements
  875. ser{ public serf()throws RemoteException { super();
  876.  
  877. }
  878. public int fact(int x)throws RemoteException
  879. {
  880. if(x==1)
  881. return x;
  882. else
  883. {
  884. int f=1;
  885. for(int i=1;x>i;i++)
  886. {
  887. f*=i;
  888. }
  889.  
  890. 37
  891.  
  892. return f;
  893. }
  894. }
  895. public static void main(String[] args)throws Exception
  896. {
  897. serf obj=new serf();
  898. Registry r=java.rmi.registry.LocateRegistry.createRegistry(1099);
  899.  
  900. r.rebind("server",obj);
  901. System.out.println("Ready...");
  902. }
  903. }
  904. CLIENT CODE:import java.rmi.*;
  905. import java.util.*;
  906. import java.rmi.Naming;
  907. public class rmiclient {
  908. public static void main(String[] args) throws
  909. Exception{ ser obj=(ser)Naming.lookup("server");
  910. int a;
  911. Scanner in=new Scanner(System.in);
  912. System.out.println("Enter one number:");
  913. a=in.nextInt();
  914. int fac=obj.fact(a);
  915. System.out.println("Factroial="+fac);
  916. }
  917.  
  918. public rmiclient() {
  919. }
  920.  
  921. 38
  922.  
  923. }
  924.  
  925. OUTPUT:SERVER :-
  926.  
  927. CLIENT:-
  928.  
  929. Result:Thus the result is displayed.
  930.  
  931. 39
  932.  
  933. SERVLETS
  934.  
  935. 40
  936.  
  937. Ex.No: 11
  938.  
  939. Current Date And Time
  940.  
  941. Date: 09-08-18
  942.  
  943. Aim:To write a Servlet that will display the current date and time.
  944.  
  945. Procedure:Steps-1. Create a new servlet.
  946. Steps-2. Import the HTTPServlet, util and text packages.
  947. Steps-3. In the processRequest() method declare a object of Date class
  948. and DateFormat class.
  949. Steps-4. Using the format() function print the date.
  950. Steps-5. Compile& Run the program..Current Date will be displayed
  951. on the browser.
  952.  
  953. 41
  954.  
  955. CODING:import java.io.*;
  956. import java.net.*;
  957. import java.util.*;
  958. import javax.servlet.*;
  959. import javax.servlet.http.*;
  960. public class tservlet extends HttpServlet {
  961. protected void processRequest(HttpServletRequest request,
  962. HttpServletResponse response)
  963. throws ServletException, IOException {
  964. response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
  965. out.println("Date:"+new Date());
  966. out.close();
  967. }
  968. }
  969.  
  970. 42
  971.  
  972. OUTPUT:-
  973.  
  974. Result:Thus the result is displayed.
  975.  
  976. 43
  977.  
  978. Ex.No: 12
  979.  
  980. Generating Multiplication Table
  981.  
  982. Date: 13-08-18
  983.  
  984. Aim:To write a servlet that displays multiplication table for the number given by user.
  985.  
  986. Algorithm:Step-1: Create a HTML file that displays a single text box and a Submit button.
  987. Step-2: Create a new servlet.
  988. Step-3: Run the servlet and get its URL, put the URL of the servlet in the
  989. forms action attribute.
  990. Step-4: In the processRequest method of servlet get the value entered in
  991. the HTML form by the user using getParameter() method.
  992. Step-5: Convert the value into integer value and generate multiplication table.
  993. Steps-6: Compile the Servlet.
  994. Step-7: Run the HTM file enter any value and submit, multiplication table for
  995. the value will be printed on browser.
  996.  
  997. 44
  998.  
  999. CODING:JAVA CODE:import java.io.*;
  1000. import java.net.*;
  1001. import javax.servlet.*;
  1002. import javax.servlet.http.*;
  1003. public class mulservlet extends HttpServlet {
  1004. public void processRequest(HttpServletRequest request,
  1005. HttpServletResponse response)
  1006. throws ServletException, IOException {
  1007. response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
  1008. int n=Integer.parseInt(request.getParameter("txt1"));
  1009. for(int i=1;i<=12;i++)
  1010. {
  1011. out.println(n+"*"+i+"="+n*i);
  1012. out.println("<br>");
  1013. }
  1014. }}
  1015.  
  1016. 45
  1017.  
  1018. HTML CODE:<html>
  1019. <head>
  1020. <title>test</title>
  1021. </head>
  1022. <body>
  1023. enter any number:
  1024. <form method="get" action="http://localhost:8084/mulservlet/mulservlet">
  1025. <input type="text" name="txt1" />
  1026.  
  1027. <br>
  1028. <input type="submit" name="submit"
  1029. value="submit"/> </form>
  1030. </body>
  1031. </html>
  1032.  
  1033. 46
  1034.  
  1035. OUTPUT:-
  1036.  
  1037. Result:Thus the result is displayed.
  1038.  
  1039. 47
  1040.  
  1041. Ex.No: 13
  1042.  
  1043. HitCount of A WebPage
  1044.  
  1045. Date: 21-08-18
  1046.  
  1047. Aim:To Write a Servlet that counts the no of times a web page has
  1048. been refreshed/reloaded
  1049. Procedure:Step-1: Create a new servlet.
  1050. Step-2: Create an object of the HttpSession class and get the session.
  1051. Step-3: Declare a new integer variable called hit and read/store the
  1052. session attribute “hitcount”.
  1053. Step-4: If hit is null make its value as 1 and put it back to the session
  1054. else increment hit and put it back to the session.
  1055. Steps-5: Print the value of hit and the session id.
  1056. Step-6 : Compile & run the program out put will be printed on browser and
  1057. every time the browser is refreshed a new hit value will be displayed.
  1058.  
  1059. 48
  1060.  
  1061. CODING:-
  1062.  
  1063. import java.io.*;
  1064. import java.net.*;
  1065. import javax.servlet.*;
  1066. import javax.servlet.http.*;
  1067. public class hitman extends HttpServlet {
  1068. protected void processRequest(HttpServletRequest request,
  1069. HttpServletResponse response)
  1070. throws ServletException, IOException {
  1071. response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
  1072. HttpSession ses=request.getSession(true);
  1073. Integer cnt=(Integer)ses.getValue("hit");
  1074. if(cnt==null)
  1075. {cnt=new Integer(1);}
  1076. else
  1077. cnt=new Integer(cnt.intValue()+1);
  1078. ses.putValue("hit",cnt);
  1079. out.println("session id:"+ses.getId()+"<br>");
  1080. out.println("page view:"+cnt+"times");
  1081. out.close();
  1082. }
  1083. }
  1084.  
  1085. 49
  1086.  
  1087. OUTPUT:-
  1088.  
  1089. Result:Thus the result is displayed.
  1090.  
  1091. 50
  1092.  
  1093. Ex.No: 14
  1094.  
  1095. Data Insertion Using Prepared Statement
  1096.  
  1097. Date: 27-08-18
  1098.  
  1099. Aim:To write a servlet program to insert data into database with the help of JDBC
  1100. and tabulate the information.
  1101.  
  1102. Procedure:Step-1: Create a HTML form and add controls for getting information from
  1103. the user.
  1104. Step-2: Give the URL of the servlet that has to process the information as
  1105. the parameter for the forms action tag.
  1106. Step-3: Create a new Servlet. Import the HttpServlet and the java.sql package.
  1107. Step-4: Load the sun.jdbc.odbc.JdbcOdbcDriver.
  1108. Step-5: Create a connection to the DB, with the help of the DSN created.
  1109. Step-6: Create the Statement object.
  1110. Step-7: Based on the action requested by the user execute the Insert or Delete
  1111. or Select Query with the help of the Statement object created.
  1112. Step-8: If the query executed was a Select query then get the result into
  1113. the ResultSet object.
  1114. Step-9: Traverse through the ResultSet.
  1115. Step-10: Print the records one by one in the table.
  1116.  
  1117. 51
  1118.  
  1119. CODING:JAVA CODE:import java.io.*;
  1120. import java.net.*;
  1121. import java.sql.*;
  1122. import javax.servlet.*;
  1123. import javax.servlet.http.*;
  1124. public class inser extends HttpServlet {
  1125. public void processRequest(HttpServletRequest request,
  1126. HttpServletResponse response)
  1127. throws ServletException, IOException {
  1128. response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
  1129. int
  1130. n=Integer.parseInt(request.getParameter("txt2"));
  1131. String na=request.getParameter("txt1"); try
  1132. {
  1133. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  1134. Connection nu=DriverManager.getConnection("jdbc:odbc:fuckservlet");
  1135.  
  1136. PreparedStatement bi=nu.prepareStatement("Insert
  1137. into student(studname,regno) values(?,?);");
  1138. bi.setString(1,na);
  1139. bi.setInt(2,n);
  1140. bi.executeUpdate();
  1141. nu.close();
  1142. }
  1143.  
  1144. 52
  1145.  
  1146. catch(Exception e)
  1147. {
  1148. e.printStackTrace();
  1149. }
  1150. out.close();
  1151. }
  1152. }
  1153. HTML CODE:<html>
  1154. <head>
  1155. <title>Insert</title>
  1156. </head>
  1157. <body>
  1158. <form method="get" action="http://localhost:8084/insertservlet/inser">
  1159.  
  1160. Enter name:
  1161. <input type="text" name="txt1" />
  1162. <br>
  1163. Enter regno:
  1164. <input type="text" name="txt2" />
  1165. <br>
  1166. <input type="submit" name="submit"
  1167. value="submit"/> </form>
  1168. </body>
  1169. </html>
  1170.  
  1171. 53
  1172.  
  1173. OUTPUT:-
  1174.  
  1175. Result:Thus the result is displayed.
  1176.  
  1177. 54
  1178.  
  1179. Ex.No: 15
  1180.  
  1181. Generating prime numbers
  1182.  
  1183. Date:03-09-18
  1184.  
  1185. Aim:To write a servlet that displays prime for the number given by user.
  1186.  
  1187. Algorithm:Step-1: Create a HTML file that displays a single text box and a Submit button.
  1188. Step-2: Create a new servlet.
  1189. Step-3: Run the servlet and get its URL, put the URL of the servlet in the
  1190. forms action attribute.
  1191. Step-4: In the processRequest method of servlet get the value entered in
  1192. the HTML form by the user using getParameter() method.
  1193. Step-5: Convert the value into integer value and generate multiplication table.
  1194. Steps-6: Compile the Servlet.
  1195. Step-7: Run the HTM file enter any value and submit, multiplication table for
  1196. the value will be printed on browser.
  1197.  
  1198. 55
  1199.  
  1200. CODING:JAVA CODE:import java.io.*;
  1201. import java.net.*;
  1202. import javax.servlet.*;
  1203. import javax.servlet.http.*;
  1204. public class pser extends HttpServlet {
  1205. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  1206.  
  1207. throws ServletException, IOException {
  1208. int p=0;
  1209. response.setContentType("text/html;charset=UTF-8");
  1210. PrintWriter out = response.getWriter();
  1211. int n=Integer.parseInt(request.getParameter("txt1"));
  1212. for(int i=1;i<n;i++) {
  1213.  
  1214. for(int j=2;j<i;j++) {
  1215. p=i%j;
  1216. if(p==0){
  1217. break;
  1218. }
  1219. }
  1220. if(p==1){
  1221. out.println(i + " ");
  1222. }
  1223. }
  1224. out.close();
  1225. }
  1226. }
  1227.  
  1228. 56
  1229.  
  1230. HTML CODE:<html>
  1231. <head>
  1232. <title>test</title>
  1233. </head>
  1234. <body>
  1235. enter any number:
  1236. <form method="post" action="http://localhost:8084/primeservlet/pser">
  1237. <input type="text" name="txt1" />
  1238.  
  1239. <br>
  1240. <input type="submit" name="submit"
  1241. value="submit"/> </form>
  1242. </body>
  1243. </html>
  1244.  
  1245. 57
  1246.  
  1247. OUTPUT:-
  1248.  
  1249. Result:Thus the result is displayed.
  1250.  
  1251. 58
  1252.  
  1253. APPLET
  1254.  
  1255. 59
  1256.  
  1257. Message Applet
  1258.  
  1259. Ex.No:16
  1260. Date:10-09-18
  1261.  
  1262. Aim:-
  1263.  
  1264. To write an Applet that will display a simple message
  1265. Algorithm:-
  1266.  
  1267. Step -1: Create a class called SimpleApplet which extends the Applet class.
  1268. Step -2: Import the java.applet, java.awt packages.
  1269. Step -3: In the init() method set the required background and foreground color.
  1270. Step -4: Inside the paint() method of the applet use the drawstring() method to
  1271. display string of your choice.
  1272. Step -5: Compile the class and use the appletviewer to view the output.
  1273.  
  1274. 60
  1275.  
  1276. CODING:-
  1277.  
  1278. package applettest;
  1279. import java.awt.*;
  1280. import java.applet.Applet;
  1281. public class Simple extends Applet {
  1282. String str="Welcome to SRM University.";
  1283. public void init() {
  1284. setBackground(Color.orange);
  1285. setForeground(Color.blue);
  1286.  
  1287. }
  1288. public void paint(Graphics g){
  1289. g.drawString(str,10,100);
  1290. }
  1291.  
  1292. }
  1293. OUTPUT:-
  1294.  
  1295. RESULT :The applet is displayed with the welcome message
  1296.  
  1297. 61
  1298.  
  1299. Ex.No : 17
  1300. Date : 12-09-18
  1301.  
  1302. Shape Applet
  1303.  
  1304. AIM:
  1305. To write an Applet that will display a different shapes and lines.
  1306.  
  1307. PROCEDURE:
  1308. Step 1: Create a class called ShapeApplet which extends the Applet class.
  1309. Step 2: Import the java.applet, java.awt packages.
  1310. Step 3: Inside the paint() method of the applet use the various built in methods of
  1311. the Graphics class to display Shapes.
  1312. Step 4: Compile the class and use the appletviewer to view the output.
  1313.  
  1314. 62
  1315.  
  1316. CODE:package applettest;
  1317. import java.applet.Applet;
  1318. import java.awt.*;
  1319. public class ShapeApplet extends Applet {
  1320. public void paint(Graphics g)
  1321. {
  1322. g.drawLine(10,10,50,50);
  1323. g.drawRect(10,60,40,30);
  1324. g.setColor(Color.blue);
  1325. g.fillRect(60,10,30,80);
  1326. g.setColor(Color.green);
  1327. g.drawRoundRect(10,100,80,50,10,10);
  1328. g.setColor(Color.yellow);
  1329. g.fillRoundRect(20,110,60,30,5,5);
  1330. g.setColor(Color.red);
  1331. g.drawLine(100,10,230,140);
  1332. g.drawLine(100,140,230,10);
  1333. g.drawString("Line Rectangles Demo",65,180);
  1334. g.drawOval(230,10,200,150);
  1335. g.setColor(Color.blue);
  1336. g.fillOval(245,25,100,100);
  1337. }
  1338. }
  1339.  
  1340. 63
  1341.  
  1342. OUTPUT:-
  1343.  
  1344. RESULT :
  1345. The applet is displayed with different geometrical shapes
  1346.  
  1347. 64
  1348.  
  1349. Ex.No : 18
  1350. Date : 20-09-18
  1351.  
  1352. Keyboard Events Applet
  1353.  
  1354. AIM:To write an Applet that will respond to all the key press events.
  1355. PROCEDURE:Step 1: Create a class which extends the Applet class and implements the
  1356. KeyBoard listener Interface.
  1357. Step 2: Import the java.applet, java.awt packages.
  1358.  
  1359. Step 3: Inside the init() method of the applet add the listeners to capture all
  1360. keyboard events.
  1361. Step 4: Override all the KeyBoard Listener methods.
  1362. Step 5: Inside these methods required information can be displayed in the status
  1363. bar of the applet using the showStatus() method and also paint method
  1364. can be invoked using the repaint() method.
  1365. Step 6: Compile the class and use the appletviewer to view the output.
  1366.  
  1367. 65
  1368.  
  1369. CODE :-
  1370.  
  1371. public class EventApplet extends Applet implements KeyListener{
  1372. int X=20,Y=30;
  1373. String msg="";
  1374. public void init()
  1375. {
  1376. addKeyListener(this);
  1377. requestFocus();
  1378. setBackground(Color.green);
  1379. setForeground(Color.blue);
  1380. }
  1381. public void keyPressed(KeyEvent k)
  1382. {
  1383. showStatus("KeyDown");
  1384.  
  1385. }
  1386. public void keyReleased(KeyEvent k)
  1387. {
  1388. showStatus("Key Up");
  1389. }
  1390. public void keyTyped(KeyEvent k)
  1391. {
  1392. msg+=k.getKeyChar();
  1393. repaint();
  1394. }
  1395. public void paint(Graphics g)
  1396. {
  1397. g.drawString(msg,X,Y);
  1398. }
  1399. }
  1400.  
  1401. 66
  1402.  
  1403. OUTPUT :-
  1404.  
  1405. RESULT :The applet that handles keyboard events is successfully executed.
  1406.  
  1407. 67
  1408.  
  1409. Ex.No : 19
  1410. Date : 26-09-18
  1411.  
  1412. Face Applet
  1413.  
  1414. AIM:To write an Applet that will display a face.
  1415.  
  1416. PROCEDURE:Step 1: Create a class which extends the Applet class.
  1417. Step 2: Import the java.applet, java.awt packages.
  1418. Step 3: Inside the paint() method of the applet use the various built in methods of
  1419. the Graphics class to display the required image.
  1420. Step 4: Compile the class and use the appletviewer to view the output.
  1421.  
  1422. 68
  1423.  
  1424. CODE :import java.applet.Applet;
  1425. import java.awt.Graphics;
  1426. public class FaceAppl extends Applet {
  1427. public void paint(Graphics g){
  1428. g.drawOval(40,40,120,150); //Head
  1429. g.drawOval(57,75,30,20); //Left eye
  1430. g.drawOval(110,75,30,20); //Right eye
  1431. g.fillOval(68,81,10,10); //Pupil (left)
  1432. g.fillOval(121,81,10,10); //Pupil (right)
  1433. g.drawOval(85,100,30,30); //Nose
  1434. g.fillArc(60,125,80,40,180,180); //Mouth
  1435. g.drawOval(25,92,15,30); //Left ear
  1436. g.drawOval(160,92,15,30); //Right ear
  1437. }
  1438. }
  1439.  
  1440. OUTPUT :-
  1441.  
  1442. RESULT :The applet is successfully executed.
  1443.  
  1444. 69
  1445.  
  1446. 70
  1447.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement