Sunteți pe pagina 1din 43

SCHOOL OF COMPUTING SCIENCES

DEPARTMENT OF INFORMATION TECHNOLOGY










ADVANCE JAVA PROGRAMMING LAB
RECORD NOTE
2009-2012


NAME : ___________________________________________

REGISTER NO : ___________________________________________

SUB CODE : 08PBCA61












SCHOOL OF COMPUTING SCIENCES

DEPARTMENT OF INFORMATION TECHNOLOGY










CERTIFICATE
This is to certify that the bonafide record of practical work done
by___________________________________ Register No.____________________ Of III BCA, VELS
University, Pallavaram, Chennai, during the year of 2011-2012.



STAFF-IN-CHARGE HEAD OF THE DEPARTMENT



Submitted for III BCA Practical Examination held on ___________________, in the
Department of IT at VELS University, Pallavaram, Chennai - 117.




INTERNAL EXAMINER EXTERNAL EXAMINER
1



INDEX



S.NO



DATE


CONTENTS


PAGE
NO


SIGNATURE


1.


10.1.2012


TCP/IP Server-Client Communication



2



2.


19.1.2012

HTML to Servlet Communication


6



3.


02.02.2012

Creating Web Services with RMI



10



4.


07.02.2012

Performing Java Data Base
Connectivity



15



5.


16.02.2012

JSP to JDBC Communication.



18



6.


23.02.2012

Designing Online Application with JSP



24



7.


01.03.2012


Creating JSP program using Java
Bean



28



8.


15.03.2012

E-Mail Application using JSP



31



9.


20.03.2012

Performing Session Management
Using JSP



35



2



1. TCP/IP Client Server Communication.
Date: 10.01.2012
Aim:
To execute a program on TCP/IP Client Server Communication
Procedure:

Step 1: Open the Notepad.
Step 2: Type the program and save the TCPServer.java.
Step 3: Open the cmd prompt and compile the program then Run the program.
Step 4: Open the Notepad.

Step 5: Type the program and save the TCPClient.java.

Step 6: Open the cmd prompt and compile the program then Run the program

Step 7: Enter the String in the Client Program

Step 8: Got Output from the Server for Lowercase Convert into uppercase the output
displayed on Client Program.

Step 8: Stop the Program.
.





3





TCP Server:

import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket=new ServerSocket(5000);
while(true)
{
Socket connectionSocket=welcomeSocket.accept();
BufferedReader inFromClient=new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
clientSentence=inFromClient.readLine();
DataOutputStream outToClient=new
DataOutputStream(connectionSocket.getOutputStream());
System.out.println(clientSentence);
capitalizedSentence=clientSentence.toUpperCase()+'\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}

4





TCP Client:
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser=new BufferedReader(new
InputStreamReader(System.in));
Socket clientSocket=new Socket("localhost",5000);
System.out.println("enter the string");
DataOutputStream outToServer=new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer=new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence=inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence=inFromServer.readLine();
System.out.println("FROM SERVER TO UPPERCASE:"+modifiedSentence);
clientSocket.close();
}
}


5







OUTPUT















Result:

Thus the program has been successfully executed and output is verified.

6





2. HTMLTO SERVLET
Date: 19.01.2012
Aim:

To design a web application with JSP to Servlet Communication

Procedure:

Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: Open the saved application and write the coding in JSP Page
Step 3: Create new servlet page in same web application and write
coding
Step 4: Save all and run the application

Step 5: Enter the details in JSP page and output will be displayed in
servlet page after execution

Step 6: Stop the execution
7



JSP Page

<html>
<head>
<title>jsp page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#02599a">
<center> <b><h1><font color="black">LOGIN PAGE</font> </h1></b>
<form action="testservlet" method="post">
First Name:<input type="text" name="FirstName" size="20"><br>
Sur Name:<input type="text" name="SurName" size="20"><br>
<input type="submit" value="submit"><br>
</center> </form>
</body>
</html>

















8



Servlet Page

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "testservlet", urlPatterns = {"/testservlet"})
public class testservlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String Firstname =request.getParameter("FirstName").toString();
String Surname =request.getParameter("SurName").toString();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet testservlet</title>");
out.println("<p>welcome"+Firstname+""+Surname+"</p>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet testservlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
} }
9




OUTPUT











Result:

Thus the program has been successfully executed and output is verified.

10





3. CREATING WEB SERVICES WITH RMI
Date: 02.02.2012
Aim:
To create web services using concept of RMI.

Procedure:


Step 1: Select ->Notepad ->create new document
Step 2: Write a code and import the Interface class
Step 3: write a code for client side as well as server side
Step 4: Save the entire file in one folder as java file
Step 5: set path of JDK in command prompt and compile the
program
Step 6: Run the program.
Step 7: Stop the execution.
.
11






Calculator Interface (Calculator.java)

public interface Calculator extends java.rmi.Remote
{
public long add(long a,long b)throws java.rmi.RemoteException;
public long sub(long a,long b)throws java.rmi.RemoteException;
public long mul(long a,long b)throws java.rmi.RemoteException;
public long div(long a,long b)throws java.rmi.RemoteException;
}

Calculator Implementation (CalculatorImpl.java)

public class CalculatorImpl extends
java.rmi.server.UnicastRemoteObject implements Calculator
{
public CalculatorImpl()throws java.rmi.RemoteException
{
super();
}
public long add(long a,long b)throws java.rmi.RemoteException
{
return a+b;
}
public long sub(long a,long b)throws java.rmi.RemoteException
{
return a-b;
}
public long mul(long a,long b)throws java.rmi.RemoteException
{
return a*b;
}
public long div(long a,long b)throws java.rmi.RemoteException
{
return a/b;
}
}
12





Calculator Server: (CalculatorServer.java)

import java.rmi.*;
public class CalculatorServer
{
public CalculatorServer()
{
try
{
Calculator c=new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorServer",c);
}
catch(Exception e)
{
System.out.println("Trouble :"+e);
}
}
public static void main(String args[])
{
new CalculatorServer();
}
}

Calculator Client: (CalculatorClient.java)

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient
{
public static void main(String args[])
{
try
{
Calculator c=(Calculator)Naming.lookup("rmi://localhost/CalculatorServer");
System.out.println(c.sub(4,3));
System.out.println(c.add(4,5));
System.out.println(c.mul(3,6));
System.out.println(c.div(9,3));
}
13






catch(MalformedURLException murle)
{
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch(RemoteException re)
{
System.out.println("RemoteException");
System.out.println(re);
}
catch(NotBoundException nbe)
{
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch(java.lang.ArithmeticException ae)
{
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
14




OUTPUT




























Result:
Thus the program has been successfully executed and output is verified.
15



4. PERFORMING JAVA DATABASE CONNECTIVITY

Date: 07.02.2012
Aim: To perform Java DataBase Connectivity (JDBC) using java
Application.

Procedure:



Step 1: select notepad ->create new document
Step 2: write a code and save as java file
Step 3: Create a table in Database.
Step 4: Create new DSN name and connect the java file with
Database

Step 5: compile the program and run the application
Step 6: Add the data.
Step 7: Stop the execution
16





Java Coding: (jdbc1.java)

import java.sql.*;
public class jdbc1
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test");
Statement stat = con.createStatement();
stat.executeUpdate("create table emp(no int,name varchar(10))");
stat.executeUpdate("insert into emp values(1,'aaa')");
stat.executeUpdate("insert into emp values(2,'bbb')");
stat.executeUpdate("insert into emp values(3,'ccc')");
ResultSet rs = stat.executeQuery("select * from emp");
while(rs.next())
{
int no = rs.getInt(1);
String name = rs.getString(2);
System.out.println(no + "\t" + name);
System.out.println();
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
17




OUTPUT




































Result:
Thus the program has been successfully executed and output is verified.
18


5.JSP to JDBC Communication
Date: 16.02.2012

Aim:
To perform Java Database Connectivity (JDBC) using JSP.


Procedure:

Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: Open the saved application and write the coding in JSP Page.

Step 3: Open the MS Access and Create a Database and save the created table

Step 4: Next open the Control Panel Select->Administrative tools->Data Sources
(ODBC)->and connect the database using Access Driver.

Step 5: Enter the details in JSP page and output will be displayed.

Step 6: Stop the execution















19

CODINGS: index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp with jdbc connection</title>
<%@page language="java" import="java.sql.*" %>
<%
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver).newInstance();
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
try{
String url="jdbc:odbc:book";
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(Exception e){
System.out.println(e.getMessage());
}
if(request.getParameter("action")!=null){
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
stmt.executeUpdate("insert into book_details(book_name,author)
values('"+bookname+"','"+author+"')");
rst=stmt.executeQuery("select * from book_details");
%>
</head>
<body>
20

<center>
<h2>book list</h2>
<table border="1" cellspacing="0" cellpadding="0">
<tr>1
<td><b>S.no</b></td>
<td><b>Book name</b></td>
<td><b>author</b></td>
</tr>
<%
int no=1;
while(rst.next()){
%>
<tr>
<td><%=no%></td>
<td><%=rst.getString("book_name")%></td>
<td><%=rst.getString("author")%></td>
</tr>
<%no++;
}
rst.close();
stmt.close();
con.close();
%>
</table>
</center>
</body>
</html>
<%}else{%>
<html>
21

<head>
<title>Book Entry FormDocument</title>
<script language="javascript">
function validate(objform){
if(objform.bookname.value.length==0){
alert("please enter the book name!");
objform.bookname.focus();
return false;
}
if(objform.author.value.length==0){
alert("please enter the author name!");
objform.author.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<form action="index.jsp" method="post" name="entry" onsubmit="return
Validate(this)">
<input type="hidden" value="list" name="action">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>
<table>
<tr>
<td colspan="2" align="center">
22

<h2>book entry form</h2></td>
</td> </tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Book Name:</td>
<td><input name="bookname" type="text" size="50"></td>
</tr>
<tr>
<td>author:</td><td>
<input name="author" type="text" size="50"></td></td></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit"></td></tr>
</table></td></tr>
</table>
</input>
</form>
</center>
</html>
<%}%>

23

OUTPUT:





Result:
Thus the program has been successfully executed and output is verified.

24

6. DESIGNING ONLINE APPLICATIONS WITH JSP

Date:23.02.2012
Aim:
To Design online application by using JSP Page

Procedure:

Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: write coding in JSP page
Step 3: create new html page and write coding
Step 4: Save all and run the application
Step 5: Enter the details in HTML page and output will be
displayed in JSP Page
Step 6: Stop the execution
25





HTML CODING

<html>
<head>
<Title>DESIGNING ONLINE APPLICATIONS WITH JSP</title>
</head>
<Body>
<Form Method=Post Action="online.jsp">
<br><br><center><h1>DESIGNING ONLINE APPLICATIONS WITH
JSP</h1><br><br>
<h3>
<pre>
What's your name?
What's your e-mail address?
What's your age?
<br>
<br>
<Input Type=Text Name=username><br>
<Input Type=text Name=email><br>
<Input Type=Text Name=age><br><br><br>
<Input Type=Submit Value="Submit"></pre></h3>
</center>
</form>
</body>
</html>
26





JSP CODING


<html>
<head>
<title>
OUR JSP PAGE
</title>
</head>
<body>
<center>
<h1>
OUR DETAILS
</h1><br><br>
<%
out.println("Your Name
is:<b>"+request.getParameter("username")+"</b><br>");
out.println("Your Email Address
is:<b>"+request.getParameter("email")+"</b><br><br>");
out.println("Your Age is:<b>"+request.getParameter("age")+"</b><br><br>");
%>
</center>
</body>
</html>
27





OUTPUT





























Result:
Thus the program has been successfully executed and output is verified.
28




7. CREATING JSP PROGRAM USING JAVABEAN


Date: 01.03.2012
Aim:
To create JSP Program by using Java Bean.

Procedure:


Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: write a code in JSP Page.

Step 3: Build the program and run the application.
Step 4: Stop the execution.
29






Jsp Coding

<html>
<body>
<%@page language="java"%>
<% out.println("Today Date and time is"); %>
<br>
Date:<%=new java.util.Date()%>
<br>
</body>
</html>
30






OUTPUT


























Result:
Thus the program has been successfully executed and output is verified.

31







8. E-MAIL APPLICATION WITH JSP
Date:15.03.2012
Aim:
To create E-Mail Application by using HTML and JSP page.

Procedure:

Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: write a code in JSP Page
Step 3: Create new HTML Page and write a code
Step 4: Save all and run the application
Step 5: Enter the detail in HTML page and output will be displayed
in JSP Page
Step 6: Stop the execution
32





JSP PAGE
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page</title>
</head>
<body>
<%
String email=request.getParameter("email");
%>
<%
if(email==null||email.equals(""))
{
%>
Please Enter an email Address:
<%
}
else
{
%>
<blx: emailhost="www.gmail.com" from="premkumar01@gmail.com">
<blx: emailTo>
<%=email%>
</blx:emailTo></br>
Thank you for registering With us </br>
You Registered the following Name: </br>
<%=request.getParameter("username")%></br> Your
Registration was received at
<%=new java.util.Date()%>
Attached,Please find a content file:
<blx: email Attach File="C:\Users\Nandhu\Desktop/wtrecord"
content type="text/plain" name="content.txt"/>
</blx:email>
<%= email %>
<%

} %>
</body>
</html>
33





HTML Page
<html>
<head>
<title> hi</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="post" action="index.jsp">
<pre>
<b>PLEASE ENTER NAME:</b>
<input type="text" name="username" size="20">
<br><b>Please Enter E-mail Address:</b>
<input type="text" name="email" size="20"><br>
<b><input type="submit" value="submit">
</pre>
</form>
</body>
</html>
34


OUTPUT

































Result:
Thus the program has been successfully executed and output is verified
35

9. PERFORMING SESSION MANAGEMENT USING JSP
Date:20.03.2012
Aim:
To Perform Session Management Using JSP.
Procedure:
Step 1: Select->Net Beans IDE->New Project->Java web
(Categories)->web applications (Project)
Step 2: write a code in JSP Page
Step 3: Create another three JSP Page and write the code.
Step 4: Save all and run the application
Step 5: Enter the detail in JSP page and output will be displayed in JSP page.
Step 6: Stop the execution.








36

CODING
(INDEX.JSP)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="name.jsp">
<font size="5">Enter your name: <input type="text"
name="name"></font><br></br>
<font size="5">Enter your password:<input type="password"
name="pwd"></font><br></br>
<input type="submit" name="submit" value="submit">
</input>
</form>
</body>
</html>






37

(NAME.JSP)
<%
String name=request.getParameter("name");
String password=request.getParameter("pwd");
if(name.equals("admin")&&password.equals("admin"))
{
session.setAttribute("username", name);
response.sendRedirect("nextpage.jsp");
}
else
{
response.sendRedirect("error.jsp");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title>JSP Page</title>
</head>
<body>
</body>
</html>

38

(NEXTPAGE.JSP)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title>JSP Page</title>
</head>
<body>
<font size="6">hello</font><%=session.getAttribute("username")%>

</body>
</html>

(ERROR.JSP)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title>JSP Page</title>
</head>
<body>
<b>user name and password is incorrect</b>
</body>
</html>

39

OUTPUT:


40







Result:
Thus the program has been successfully executed and output is verified.
41

S-ar putea să vă placă și