0% found this document useful (0 votes)
26 views26 pages

Record Programs

Uploaded by

surendarrs064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views26 pages

Record Programs

Uploaded by

surendarrs064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SRI KRISHNA ARTS AND SCIENCE

COLLEGE An Autonomous College Affiliated to


Bharathiar University Coimbatore - 641 008.

DEPARTMENT OF COMPUTER SCIENCE ADVANCED JAVA

LAB

PRACTICAL RECORD
NAME:
ROLL NUMBER:

CLASS:

PROGRAMME:

COURSE CODE:
SRI KRISHNA ARTS AND SCIENCE
COLLEGE An Autonomous College Affiliated to
Bharathiar University Coimbatore - 641 008.
Certified bonafide record of work done by during the year 2022 – 2023.

Staff In-charge Head of the department

Submitted to Sri Krishna Arts & Science College (Autonomous) End Semester examinations held on

Internal Examiner External Examiner


DECLARATION

I hereby declare that this record of observation is based on the experiments carried out and recorded by me
during
the laboratory classes of “ ” conducted by SRI KRISHNA ARTS AND SCIENCE COLLEGE, Coimbatore-
641 008.

Date:
Name of the student : Signature of the student Roll Number :
Countersigned by staff
CONTENT
S.No. Date Title of the Experiment Page No Sign

1 Lambda Expression

2 Linked List Class

3 Reduce and Filter

4 TCP/IP Communication

5 Form using Labels, Textboxes and


Button Controls

6 JMenu and JMenuItem

7 Java Beans

8 Database Connectivity

9 Servlet application using Cookies


and Sessions

10 JSP Tags

11 JSP Custom Tags

12 Spring Application

Ex: 1 Lambda

interface NumericTest{
boolean test (int n);
}
class Lambda{
public static void main(String[] args)
{
NumericTest isEven =(n)->(n%2)==1;
if(isEven.test(10))System.out.println("10 is even");
if(isEven.test(9))System.out.println("9 is not
even"); }
}
OUTPUT:

Ex: 2 Linked List

public class linkedlist {


public static void main(String[] args)
{
LinkedList<String> Names = new
LinkedList<String>(); Names.add("Steve");
Names.add("Smith");
Names.add("Daniel");
System.out.println("Linked list:" +Names);
Names.add("Alan");
System.out.println("Added list:" +Names);
String Name = Names.get(1);
System.out.println("Accesed list:" +Name);
Names.set(2, "Surya");
System.out.println("Updating list:" +Names);
String Name1 = Names.remove(2);
System.out.println("Removed elemnt:" +Name1);
System.out.println("Final Linked list:" +Names);
}

}
OUTPUT:

Ex: 3 Reduce & Filter Method

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class stream


{
public static void main(String[] args)
{
List<Product>productList =new ArrayList<Product>();
productList.add(new Product(1,"HP Laptop", 25000f));
productList.add(new Product(2,"Dell Laptop", 30000f));
productList.add(new Product(3,"Lenovo Laptop",
28000f)); productList.add(new Product(4,"Sony Laptop",
27000f)); productList.add(new Product(5,"Apple Laptop",
90000f)); List<Float>productPriceList =
productList.stream()
.filter(p->p.price < 30000)
.map(p->p.price)
.collect(Collectors.toList());
System.out.println("Filtered stream list:"
+productPriceList); Float totalPrice = productList.stream()
.map(product->product.price)
.reduce(0.0f,(sum,price)->sum+price);
System.out.println("Reduced stream list:"
+totalPrice); }
}
OUTPUT:

Ex: 4 TCP/IP Protocol

//Gossip Server
import java.io.*;
import java.net.*;
public class GossipServer
{
public static void main(String[] args) throws
Exception {
ServerSocket sersock = new
ServerSocket(4000); System.out.println("Server
ready for chatting");
Socket sock = sersock.accept( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new
BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream,
true);

// receiving from server ( receiveRead object)


InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new
BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;


while(true)
{
if((receiveMessage = receiveRead.readLine()) !=
null) {
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}
//Gossip Client
import java.io.*;
import java.net.*;
public class GossipClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 4000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)


InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new
BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;


while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) != null)
//receive from server
{
System.out.println(receiveMessage); // displaying at DOS
prompt }
}
}
}
OUTPUT:
Ex: 5 Label, Textboxes, Button Control

import javax.swing.*;
import java.awt.event.*;

public class Prg05 extends JFrame


{
private JLabel lb1;
private JTextField tx1;
private JButton btn;
public Prg05()
{
Clicklistener click= new Clicklistener();
lb1 = new JLabel("Enter your name here:");
tx1 = new JTextField(20);
btn = new JButton("submit");
btn.addActionListener(click);
JPanel jp = new JPanel();
jp.add(lb1);
jp.add(tx1);
jp.add(btn);
add(jp);
}
public static void main(String[] args)
{
Prg05 jf = new Prg05();
jf.setTitle("This is a frame");
jf.setSize(300, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class Clicklistener implements
ActionListener {
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn)
{
btn.setText("The value is submitted");
}
}}}
OUTPUT:

Ex: 6 JMenu &JMenuItem


import javax.swing.*;
import java.awt.event.*;

public class MenuEx implements ActionListener


{
JFrame f;
JMenuBar mb;
JMenu file,edit,help;
JMenuItem cut,copy,paste,selectAll,New,open,quit;
JTextArea ta;
MenuEx()
{
f=new JFrame();
New = new JMenuItem("New");
quit = new JMenuItem("Exit");
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
selectAll=new JMenuItem("selectAll");
New.addActionListener(this);
quit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
file.add(New);
file.add(quit);

edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll
); mb.add(file);mb.add(edit);mb.add(help);
ta=new JTextArea();
ta.setBounds(5,5,360,320);
f.add(mb);
f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent
e) {
if(e.getSource()==New)
{
new MenuEx();
}
if(e.getSource()==quit)
{
System.exit(0);
}
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
public static void main(String[] args)
{
new MenuEx();
}
}
OUTPUT:
Ex: 7 Bean

//Person.java

import java.io.Serializable;
public class Person
{
public String firstName;
public String lastName;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
}

//BeanEx

public class BeanEx


{
public static void main(String[] args)
{
Person person = new Person();
person.setFirstName("Stephen");
person.setLastName("Mark");
System.out.println("Java Bean
Data:"+person.getFirstName() + " " +person.getLastName());
}
}
OUTPUT:
Ex: 8 JDBC Application & JDBC Driver

import java.sql.Connection;
import java.util.Scanner;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcdemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
String model,cc,mileage,color,price;
System.out.println("Enter Bike MODELNAME, CC,
MILEAGE, COLOR, PRICE");
Scanner sc = new Scanner(System.in);
model = sc.next();
cc = sc.next();
mileage = sc.next();
color = sc.next();
price = sc.next();

String databaseURL =
"jdbc:ucanaccess://Database1.accdb";
try {

Connection conn =
DriverManager.getConnection(databaseURL);
System.out.println("Connected to ms acess");
String sql = "INSERT INTO BIKETABLE
(MODEL_NAME,CC,MILEAGE,COLOR,PRICE) VALUES (?,?,?,?,?)";

PreparedStatement p = conn.prepareStatement(sql);
//Statement p = conn.createStatement();
p.setString(1, model);
p.setString(2, cc);
p.setString(3, mileage);
p.setString(4, color);
p.setString(5, price);

//Statement statement = conn.createStatement();


int rows = p.executeUpdate();
if(rows>0)
{
System.out.println("A Vehicle data is inserted");

}
sql ="SELECT * FROM BIKETABLE";
Statement st = conn.createStatement();
ResultSet result = st.executeQuery(sql);
System.out.println("\n MODELNAME CC MILEAGE
COLOR PRICE\n ");

while(result.next())
{
model=result.getString("MODEL_NAME");
cc=result.getString("CC");
mileage=result.getString("MILEAGE");
color=result.getString("COLOR");
price=result.getString("PRICE");

System.out.println(model + "\t" + cc + "\t" +


mileage + "\t" + color + "\t" + price);
}

conn.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}

}
OUTPUT:
Ex: 9 Servlet Application using Cookies and

Sessions //FirstServlet.java

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class FirstServlet extends HttpServlet {

public void doPost(HttpServletRequest request,


HttpServletResponse response){
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("userName");
out.print("Welcome "+n);

Cookie ck=new Cookie("uname",n);//creating cookie


object response.addCookie(ck);//adding cookie in the
response

//creating submit button


out.print("<form action='SecondServlet'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");

out.close();

}catch(Exception e){System.out.println(e);}
}
}

//SecondServlet

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* Servlet implementation class SecondServlet
*/
public class SecondServlet extends HttpServlet {

public void doGet(HttpServletRequest request,


HttpServletResponse response){
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());

out.close();

}catch(Exception e){System.out.println(e);}
}
}

//index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServlet" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
OUTPUT:
Ex: 10 JSP Tags

//login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-
8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h3> Login here </h3>
<form action="user_login" method="post">
<table style="width: 20%">
<tr> <td>UserName</td>
<td><input type="text" name="username" /></td></tr>
<tr> <td>Password</td>
<td><input type="password" name="password"
/></td></tr> </table>
<input type="submit" value="Login" /></form>
</body>
</html>

//user_login.java

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

public class user_login extends HttpServlet {


public user_login() {
super();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String username =
request.getParameter("username"); String password =
request.getParameter("password");
if(username.isEmpty() || password.isEmpty() )
{
RequestDispatcher requ =
request.getRequestDispatcher("login.jsp");
requ.include(request, response);
}
else
{
RequestDispatcher requ =
request.getRequestDispatcher("login_2.jsp");
requ.forward(request, response);
}
}

//login_2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-


8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>User Logged In</title>
</head>
<body>
<table style="width: 20%">
<tr><td>
<% String username = request.getParameter("username");
%> <a>Welcome user, you have logged in.</a></td></tr>
<tr></tr><tr><td></td><td><a
href="login.jsp"><b>Logout</b></a></td></tr>
</table>
</body>
</html>
OUTPUT:
Ex: 11 JSP Custom Tags

//MyTagHandler.java

import java.util.Calendar;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.tagext.TagSupport;

public class MyTagHandler extends TagSupport{

public int doStartTag() throws JspException {


JspWriter out=pageContext.getOut();//returns the instance
of JspWriter
try{
out.print(Calendar.getInstance().getTime());//printing date and
time using JspWriter
}catch(Exception e){System.out.println(e);}
return SKIP_BODY;//will not evaluate the body content of the
tag }
}

//index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-


8" pageEncoding="UTF-8"%>
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Current Date and Time is: <m:today/>

</body>
</html>

//mytags.tld(created using xml file)

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>

<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>today</name>
<tag-class>com.Prg11.MyTagHandler</tag-
class> </tag>
</taglib>
OUTPUT:

Ex: 12 Spring Application

//HelloWorld.java(Class file)

public class HelloWorld {


private String message;

public void setMessage(String message){


this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}

//Bean.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"


xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans 3.0.xsd">

<bean id = "helloWorld" class =


"com.Prg12_1.HelloWorld"> <property name = "message"
value = "Hello World!"/>
</bean>

</beans>

//MainApp.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {


public static void main(String[] args) { ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld)
context.getBean("helloWorld"); obj.getMessage(); } }
OUTPUT:

You might also like