AJP Lab Manual
AJP Lab Manual
<html>
<head>
<title>Validate Login Screen</title>
</head>
<body>
<br><br><br><br><br>
<table border="3" align="Center">
<th colspan="2">Login Form</th>
<tr>
<td>User Name:</td>
<script>
function validate()
{
var userName = document.getElementById("txtUName");
var pwd = document.getElementById("txtPwd");
if(userName.value == '')
{
alert("Enter the username!");
userName.focus();
return;
}
else if (pwd.value == '')
{
alert("Enter the password!");
pwd.focus();
return;
}
else
{
alert("Login Successful");
return;
}
}
</script>
</body>
</html>
OUTPUT:
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<br><br><br><br><br>
<table border="3" align="Center">
<th colspan="2">Registration Form</th>
<tr>
<td>Name:</td>
<tr>
</table>
<script>
function validate()
{
var name = document.getElementById("txtName");
var phone = document.getElementById("txtPhone");
var address = document.getElementById("txtAddress");
var mail = document.getElementById("txtMail");
if(name.value == '')
{
alert("Enter the username!");
name.focus();
return;
}
if (phone.value == '')
{
alert("Enter the phone no!");
phone.focus();
return;
}
else if(isNaN(phone.value))
{
alert("Only numbers allowed in phone no");
phone.focus();
return;
}
if (address.value == '' || address.value.length < 10)
{
alert("Enter the address and length should be more than 10 chnaracter!");
address.focus();
return;
}
if(mail.value == '')
{
alert("Enter the mail!");
mail.focus();
return;
}
else
{
var email = mail.value;
var re = /\S+@\S+\.\S+/;
if(re.test(email))
alert("Thanks,Your account registered!");
else
alert("Enter the proper mail address");
mail.focus();
return;
}
}
3 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual
</script>
</body>
</html>
OUTPUT:
<html>
<frameset cols="25%,*,25%">
<frame src="Lab1.html">
<frame src="Lab2.html">
<frame src="Lab4.html">
</frameset>
</html>
OUTPUT:
<html>
<head>
<title>Calculator</title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<table border="2" align="Center">
<tr>
<tr>
<th colspan="4">Simple calculator</th>
</tr>
<td>
Operand 1:
</td>
<td>
<input type="txt1" id="txtOp1" />
</td>
<td>
<button id="btnadd" onclick="arth_fun(1)">+</button>
</td>
<td>
<button id="btnsub" onclick="arth_fun(2)">-</button>
</td>
</tr>
<tr>
<td>
Operand 2:
</td>
<td>
<input type="txt2" id="txtOp2" />
</td>
5 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual
<td>
<button id="btndiv" onclick="arth_fun(3)">/</button>
</td>
<td>
<button id="btnpro" onclick="arth_fun(4)">*</button>
</td>
</tr>
<tr>
<td>
Result:
</td>
<td>
<input type="text" id="result" />
</td>
<td>
<button id="btnmod" onclick="arth_fun(5)">%</button>
</td>
</tr>
</table>
<script >
function arth_fun(ope)
{
switch (ope)
{
case 1:
document.getElementById("result").value = op1 + op2;
break;
case 2:
document.getElementById("result").value = op1 - op2;
break;
case 4:
document.getElementById("result").value = op1 * op2;
break;
case 3:
document.getElementById("result").value = op1 / op2;
break;
case 5:
document.getElementById("result").value = op1 % op2;
break;
}
}
</script>
</body>
</html>
OUTPUT:
<html>
<head>
</head>
<body>
</body>
</html>
OUTPUT:
import java.sql.*;
public class AJPLab6
{
static final String DB_URL =
"jdbc:mysql://localhost/mydb?autoReconnect=true&useSSL=false";
static final String USER = "root";
static final String PASS = "micromaxa350";
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql = "UPDATE Student set age=25 WHERE id=2";
int rows = stmt.executeUpdate(sql);
if(rows > 0)
System.out.println("Table updated");
else
System.out.println("Table updated failed");
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
try
{
9 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}
}
OUTPUT:
7. Program to insert data into database and retrieve data from database.
import java.sql.*;
public class AJPLab7
{
//Display values
finally
{
//finally block used to close resources
try
{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}
}
OUTPUT:
// do nothing.
}
}
OUTPUT:
<!DOCTYPE html>
<html>
<body>
<div style="border:2px solid black">
<form action="AJPLab91">
User Name:<input type="text" name="userName"/>
Password:<input type="password" name="userPassword"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AJPLab91 extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AJPLab92 extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();
//Reading cookies
Cookie c[]=request.getCookies();
//Displaying User name value from cookie
pwriter.print("Name: "+c[0].getValue());
pwriter.print("<br>");
//Displaying user password value from cookie
pwriter.print("Password: "+c[1].getValue());
14 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual
pwriter.close();
}
}
OUTPUT:
<!DOCTYPE html>
<html>
<head>
<title>Generate Even And Odd Numbers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="border:2px solid black">
<form action="Lab10.jsp">
SrartRange:<input type="text" name="stratRange">
EndRange:<input type="text" name="endRange">
<input type="submit" value="Submit Range">
</form>
</div>
</body>
</html>
JSP file code to print even and odd numbers between the given range.
<%
int start = Integer.parseInt(request.getParameter("stratRange"));
int end = Integer.parseInt(request.getParameter("endRange"));
%>
<h3>Even Numbers</h3>
<%for(int i=start;i<= end;i++){%>
<%if(i %2 == 0)
out.println(i);%>
<br>
<%}%>
<h3>Odd Numbers</h3>
<%for(int i=start;i<= end;i++){%>
<%if(i %2 != 0)
out.println(i);%>
<br>
<%}%>
</html>
OUTPUT:
11. Write a program to verify the particular user and redirect to welcome page if credentials
are valid else print proper message.
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="border: 2px solid black">
<form action="Lab11.jsp">
UserName:<input type="text" name="userName">
Password:<input type="password" name="password">
17 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<%
String ipname = request.getParameter("userName");
String ippassword = request.getParameter("password");
%>
<%}else{%>
<%out.println("Invalid Credentials");%>
<%}%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><h1>Welcome to JSP</h1></div>
</body>
</html>
OUTPUT:
Html file code to show the link for Include and Forward
<!DOCTYPE html>
<html>
<head>
<title>Include/Forward Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="border:2px solid black">
<a href='include.jsp'>Include Demo</a>
<a href='forward.jsp'>Forward Demo</a>
</div>
</body>
</html>
out.println("Address : Nagnur,Davanagare"+"<br>");
out.println("Degree : BCA"+"<br>");
out.println("Marks : 550"+"<br>");
%>
</body>
</html>
OUTPUT: