You are on page 1of 15

Advance Java Class Assignments

Lecture No -1 Assignment No-1


Objective Write a program on JSP Commands to be used 1. Including external files Output:

Advance Java Class Assignments

Source Index.jsp

Advance Java Class Assignments

<html> <head> <title></title> </head> <body> <%@include file="top.html"%> <h2>Directory Structure in Servlets & JSP</h2> <img src="images/DirectoryStructure.JPG"> <%@include file="bottom.html"%> </body> </html> Top.html <h1>JSP Lecture</h1> <a href="">Home</a> <a href="">About us</a> <a href="">Contact us</a> <a href="">Examples</a> <a href="">Help</a> <hr color="black"> Bottom.html <hr color="black"> &copy;2008 JSP Lecture

Advance Java Class Assignments

Lecture No -1 Assignment No-2


Objective Write a program on JSP Commands to be used Output:

Source HelloWorldServlet.java import javax.servlet.*;

Advance Java Class Assignments

import javax.servlet.http.*; import java.io.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Tell the Web server that the response is HTML. response.setContentType("text/html"); // Get the PrintWriter for writing out the response. PrintWriter out = response.getWriter(); // Write the HTML back to the browser. out.println("<html>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("<img src='images/DirectorStructure.jpg' alt='display mansi'/>"); out.println("</body>"); out.println("</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request,response); } }

Advance Java Class Assignments

Lecture No -1 Assignment No-3


Objective Write a program on JSP using database Commands to be used Output:

Source SearchProducts.jsp <%@page import="java.sql.*"%> <html>

Advance Java Class Assignments

<head> <title></title> </head> <body> <form name="f1" action="" method="get"> <h3>Product Search</h3> <input type="text" name="txtSearch"> <input type="submit" name="s1" value="Search"> <hr color="black"> <% String product=request.getParameter("txtSearch"); if(product!=null) { Connection con; Statement stmt; ResultSet rs; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:xyz","",""); stmt=con.createStatement(); rs=stmt.executeQuery("select * from product where ProductName like '" + product + "%'" ); %> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Product ID</th> <th>Product Name</th> <th>Category</th> <th>Unit Price</th> <th>Qunatity Per Unit</th> <th>&nbsp;</th> </tr> <% while(rs.next()) { int productid=rs.getInt("ProductID"); out.println("<tr>"); out.println("<td>" + productid + "</td>"); out.println("<td>" + rs.getString("ProductName") + "</td>"); out.println("<td>" + rs.getString("Category") + "</td>"); out.println("<td>" + rs.getInt("UnitPrice") + "</td>"); out.println("<td>" + rs.getInt("QuantityPerUnit") + "</td>"); out.println("<td><a href='Products.jsp?pid=" + productid + "' target='_blank'>update</a></td>"); out.println("</tr>");

Advance Java Class Assignments

} out.println("</table>"); } %> </form> </body> </html>

Advance Java Class Assignments

Lecture No -1 Assignment No-4


Objective Write a program to insert, update table data Commands to be used 1. Output:

Source <%@page import="java.sql.*"%> <html> <head> <title></title>

Advance Java Class Assignments

<% String String String String String

productid=request.getParameter("pid"); product=request.getParameter("txtProduct"); category=request.getParameter("txtCategory"); unitprice=request.getParameter("txtPrice"); qty=request.getParameter("txtQty");

Connection con; Statement stmt; ResultSet rs; PreparedStatement pstmt; if(product !=null && productid!=null) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:xyz","",""); String qry="update product set ProductName='" + product + "',Category='" + category + "',UnitPrice=" + unitprice + ",QuantityPerUnit=" + qty + " where productid=" + productid ; pstmt=con.prepareStatement(qry); pstmt.executeUpdate(); out.println("Record Updated Successfully..."); } else if(product!=null) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:xyz","",""); String qry="insert into product(ProductName,Category,UnitPrice,QuantityPerUnit) values ('" + product + "','" + category + "'," + unitprice + "," + qty + ")" ; pstmt=con.prepareStatement(qry); pstmt.executeUpdate(); out.println("Record Added Successfully..."); } else if(productid!=null) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:xyz","",""); stmt=con.createStatement(); rs=stmt.executeQuery("select * from product where productid=" + productid); rs.next(); product=rs.getString("ProductName"); category=rs.getString("Category");

10

Advance Java Class Assignments

unitprice=String.valueOf(rs.getInt("UnitPrice")); qty=String.valueOf(rs.getInt("QuantityPerUnit")); } %> </head> <body> <form name="f1" method="post"> <table border="0" cellspacing="0" cellpadding"3"> <tr> <td>Product Name</td> <td><input type="text" name="txtProduct" value="<%=product%>"> </tr> <tr> <td>Category</td> <td><input type="text" name="txtCategory" value="<%=category%>"> </tr> <tr> <td>Unit Price</td> <td><input type="text" name="txtPrice" value="<%=unitprice%>"> </tr> <tr> <td>Qunatity/Unit</td> <td><input type="text" name="txtQty" value="<%=qty%>"> </tr> <tr> <td>&nbsp;</td> <td><input type="submit" value="Save"> </tr> </table> </form> </body> </html>

11

Advance Java Class Assignments

Lecture No -2 Assignment No-1


Objective Write a program to Arithmetic operations (i.e. +,-,/,&) Commands to be used 1. RMI Output:

12

Advance Java Class Assignments

Source RInt.java import java.rmi.Remote; import java.rmi.RemoteException; public interface RInt extends Remote { public double calculate(double v1,double v2,int choice)throws RemoteException; } RClient.java import java.rmi.Naming; import java.io.*; public class RClient { public RClient(){}

13

Advance Java Class Assignments

public static void main(String args[]) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { double n1,n2,ans; int choice; do { System.out.println("CALCULATION MENU"); System.out.println("-------------------------------"); System.out.println("Enter 1 For Addition"); System.out.println("Enter 2 For Subtraction"); System.out.println("Enter 3 For Multiplication"); System.out.println("Enter 4 For Division"); System.out.println("Enter 5 To Exit"); System.out.println("\n"+"Enter Your Choice: "); choice=Integer.parseInt(br.readLine()); if(choice==5)System.exit(0); if(choice<=0 || choice>5) { System.out.println("Invalid Selection"); } if(choice>0 && choice<5) { System.out.println("Enter first Number"); n1=Double.parseDouble(br.readLine()); System.out.println("Enter second Number"); n2=Double.parseDouble(br.readLine()); RInt oRint=(RInt)Naming.lookup("addObj"); ans=oRint.calculate(n1,n2,choice); System.out.println("----------------------------------"); System.out.println("Processed Result is: "+ans); System.out.println("----------------------------------"); } }while(true); } catch(Exception e) { e.printStackTrace(); } } }

14

Advance Java Class Assignments

RServer.java import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.rmi.registry.LocateRegistry; import java.rmi.Naming; public class RServer extends UnicastRemoteObject implements RInt { public RServer()throws RemoteException{} public static void main(String args[]) { try { RServer oRserver=new RServer(); LocateRegistry.createRegistry(1009); Naming.rebind("addObj",oRserver); System.out.println("SERVER READY....."); } catch(Exception e) { e.printStackTrace(); } } public double calculate(double v1,double v2,int choice)throws RemoteException { double result=0; switch(choice) { case 1: result=v1+v2; break; case 2: result=v1-v2; break; case 3: result=v1*v2; break; case 4: result=v1/v2; break; default: System.out.println("Invalid Choice Request!"); break; } return result; } }

15

You might also like