Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Advanced Java Assignment

Name: Merin Biju


Roll No. 17
Class: TYBBA (CA)

1) Write a JSP program to check whether given number is Perfect or not. (Use Include
directive).

HTML File:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="index.jsp" method="post">
Enter Number:
<input type="text" name="num">
<input type="submit" value="submit">
</form>
</body>
</html>

JSP File:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<%
{
int i,Sum = 0;
int num = Integer.parseInt(request.getParameter("num"));
for(i = 1 ; i < num ; i++)
{
if(num % i == 0)
Sum = Sum + i ;
}

if (Sum == num)
out.println("\nPerfect Number");
else
out.println("\n Not Perfect Number");
}
%>

Message.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
The given number is
</body>
</html>

2) Write a java program in multithreading using applet for drawing flag.


import java.awt.*;
import java.awt.event.*;
class MoveText extends Frame implements Runnable
{
Label l1;
Thread t;
int x,y,side;
public MoveText()
{
setLayout(null);
l1=new Label(" Hello Java");
l1.setFont(new Font("",Font.BOLD,14));
l1.setForeground(Color.red);
setSize(400,400);
setVisible(true);
t=new Thread(this);
t.start();
x=5;
y=200;side=1;
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void run()
{
try
{
if(side==1)
{
t.sleep(50);
l1.setBounds(x+=5,y-=5,70,15);
add(l1);
if(y==20)
side=2;
}
if(side==2)
{
t.sleep(50);
l1.setBounds(x+=5,y+=5,70,15);
add(l1);
if(y==200)
side=3;
}
if(side==3)
{
t.sleep(50);
l1.setBounds(x-=5,y+=5,70,15);
add(l1);
if(y==390)
side=4;
}
if(side==4)
{
t.sleep(50);
l1.setBounds(x-=5,y-=5,70,15);
add(l1);
if(x==0)
{
side=1;
x=0;y=200;
}
}
}catch(Exception e)
{
System.out.println(e);
}
run();
}
public static void main(String args[])
{
new MoveText();
}
}

3) Write a socket program in Java to check whether given number is prime or not.
Display result on client terminal.

primeclient.java

import java.net.*;
import java.io.*;

/*Client Program*/

class primeclient //Class


{
public static void main(String args[]) //main method
{
try
{
Socket cs = new Socket("LocalHost", 1000); //Ip address and Port are
its parameters (Parameterized constructor)

BufferedReader infu = new BufferedReader(new


InputStreamReader(System.in)); //Characters are accepted as stream in a form
of buffer

System.out.println("Enter a number : ");//To ask enter a number

int a = Integer.parseInt(infu.readLine()); //Store the entered


number by user in an integer variable

DataOutputStream out = new DataOutputStream(cs.getOutputStream());


//Required to print the information

out.writeInt(a);//Display the entered number

DataInputStream in = new DataInputStream(cs.getInputStream());

System.out.println(in.readUTF()); //Get the result from server

cs.close(); //Close your socket connection


}
catch(Exception e)
{
System.out.println(e.toString()); //Throw the error message
}
}
}

primeserver.java

/*Server Program*/

import java.net.*;
import java.io.*;

class primeserver //Class name should be same as program file name


{
public static void main(String args[]) //Main Method
{
//To detect any operation error or logical erros
try
{

ServerSocket ss = new ServerSocket(1000); //Server endpoint

//Print server is started


System.out.println("Server Started...............");

//Accept method
Socket s = ss.accept();

//For accepting characters take information


DataInputStream in = new DataInputStream(s.getInputStream());

int x = in.readInt(); //Store the number in x variable


//To print information Characters UTC Encoded
DataOutputStream otc = new DataOutputStream(s.getOutputStream());

int y = x/2; //store in y variable

if(x == 1 || x == 2 || x == 3)
{
otc.writeUTF(x + "is Prime"); //Directly Print Prime if
numbers are 1 2 3
System.exit(0);
}
for(int i = 2; i <= y; i++)
{
if(x % i != 0) //Divide the number and the remainder is
your mod (If remainder is no 0 it means it is a prime number)
{
otc.writeUTF(x + " is Prime");
}
else
{
otc.writeUTF(x + " is not Prime");
}
}
}
catch(Exception e) //Catch your exception
{
//Print if any error occurs
System.out.println(e.toString());
}
}
}

6) Write a java program to display IPAddress and name of client machine.

Server1.java
import java.io.*;
import java.net.*;
class server1
{
public static void main(String a[]) throws Exception
{
ServerSocket ss = new ServerSocket(1000);
System.out.println("Server is waiting for client : ");
Socket s =ss.accept();
System.out.println("Client is connected");
}
}

client1.java
import java.io.*;
import java.net.*;
class client1
{
public static void main(String a[]) throws Exception
{
Socket ss = new Socket("localhost",1000);
String hostName = ss.getInetAddress().getHostName();
System.out.println("IP Address = "+ss.getInetAddress());
System.out.println("Client name = "+ss.getInetAddress().getHostName());
}
}

Output:

7) Write a JSP program to display the details of College (CollegeID, Coll_Name, Address) in
tabular form on browser.

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<body>
<%@ page import="java.sql.*" %>
<%! int collegeId;
String collegeName,collegeAdd; %>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/advJava","root","password"
);
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from college");
%>
<table border="1" width="40"> <tr> <td>collegeId</td> <td>collegeName</td>
<td>collegeAdd</td> </tr> <% while(rs.next()) { %> <tr><td><%= rs.getInt("collegeId")
%></td> <td><%= rs.getString("collegeName") %></td> <td><%= rs.getString("collegeAdd")
%> </tr><%
}
cn.close();
}catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>

8) Write a JSP script to accept username and password from user, if they are same then
display “LoginSuccessfully” message in Login.html file, otherwise display “Login
Failed” Message in Error.html file.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login JSP Page</title>
</head>

<%
if("POST".equalsIgnoreCase(request.getMethod()) && request.getParameter("submit")!
=null)

{
String user_name= request.getParameter("username");
String password= request.getParameter("password");
if("admin".equalsIgnoreCase(user_name) &&
"admin123".equalsIgnoreCase(password))
{
response.sendRedirect("save.html");
}
else
{
response.sendRedirect("error.html");
}
}

%>
<body>
<h2>Login Form</h2>

<form method="post">
User Name: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<button type="submit" name="submit">Submit</button>
<button type="reset">Reset</button>
</form>
</body>
</html>

You might also like