Advanced Java Assignment: Name: Merin Biju Roll No. 17 Class: TYBBA (CA)
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>
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*/
primeserver.java
/*Server Program*/
import java.net.*;
import java.io.*;
//Accept method
Socket s = ss.accept();
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());
}
}
}
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.
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.
<%
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>