Você está na página 1de 21

Advanced Java Programming Lab Manual

1. Program to validate Login Form

<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>

<td><input type="text" id = "txtUName"></td>


</tr>
<tr>
<td>User Password:</td>
<td><input type="password" id = "txtPwd">
</td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="Login" onclick="validate()"></td>
<tr>
</table>

<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>

1 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

Need to save program with .html extension Like Lab1.html

OUTPUT:

2. Program to validate Registration Form

<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>

<td><input type="text" id = "txtName" style="text-align:centre"></td>


</tr>
<tr>
<td>Phone No:</td>
<td><input type="text" id = "txtPhone"> </td>
</tr>
<tr>
<td>Address:</td>
<td><textarea rows="4" cols="30" id = "txtAddress"></textarea> </td>
</tr>
<tr>
<td>Mail Id:</td>
<td><input type="text" id = "txtMail"> </td>
</tr>
<tr>
<td colspan="2" ><input type="submit" value="Register" onclick="validate()"></td>

2 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

<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:

3. Program to validate Frames

<html>

<frameset cols="25%,*,25%">
<frame src="Lab1.html">
<frame src="Lab2.html">
<frame src="Lab4.html">
</frameset>

</html>

OUTPUT:

4 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

4. Program to implement simple calculator

<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)
{

var op1 = parseInt(document.getElementById("txtOp1").value);


var op2 = parseInt(document.getElementById("txtOp2").value);

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:

6 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

5. Program to implement lists

<html>
<head>

</head>
<body>

<h3> Un Ordered List</h3>


<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>

<h3> Ordered List</h3>


<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>

<h3> Descriptive or Definition List List</h3>


<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>

</body>
</html>

7 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

OUTPUT:

8 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

6. Program to update student Info using JDBC Connection

Database and table creation

create database mydb;


use mydb;

create table Student


(
Id int,
Name varchar(30),
Age int,
marks int
);

Data Insertion to the table Student

insert into Student values(1,'Kallesh',22,250);


insert into Student values(2,'Rekha',22,250);

Java program to update student info

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
{

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);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "Insert Into Student values(3,'Sumanth',22,650)";
int rows = stmt.executeUpdate(sql);
if(rows > 0 )
System.out.println("Row inserted");
sql = "SELECT * FROM Student";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next())
{
int id = rs.getInt("Id");
String name = rs.getString("Name");
int age = rs.getInt("Age");
int marks = rs.getInt("Marks");

//Display values

10 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

System.out.println("Id: " + id);


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + marks);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}

finally
{
//finally block used to close resources
try
{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}

}
}

OUTPUT:

11 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

8. Program to demonstrate dynamic html using java servlet

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class AJPLab81 extends HttpServlet
{
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Dynamic html generation";
}

public void doGet(HttpServletRequest request,HttpServletResponse response)


throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>" + message + "</h1>");
out.println("<h2>" + "Name : Kallesh" + "</h2>" );
out.println("<h2>" + "Address : SSLayout,Davangere" + "</h2>" );
out.println("<h2>" + "Phone : 9986261746" + "</h2>" );
out.println("<h2>" + "Email : kallesh@gmail.com" + "</h2>" );
out.println("</body>");
out.println("</html>");
}
public void destroy()
{
12 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual

// do nothing.
}
}

web.xml configuration file

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


<web-app>
<servlet>
<servlet-name>AJPLab81</servlet-name>
<servlet-class>AJPLab81</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AJPLab81</servlet-name>
<url-pattern>/AJPLab81</url-pattern>
</servlet-mapping>
</web-app>

OUTPUT:

9. Program to demonstrate cookies in java servlets.

Html File Code

<!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>

Input values collection and cookie creation code

13 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

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();

String name = request.getParameter("userName");


String password = request.getParameter("userPassword");
pwriter.print("UserName:"+name);
pwriter.print("<br>");
pwriter.print("Password: "+password);

//Creating two cookies


Cookie c1=new Cookie("userName",name);
Cookie c2=new Cookie("userPassword",password);

//Adding the cookies to response header


response.addCookie(c1);
response.addCookie(c2);
pwriter.print("<br><a href='AJPLab92'>Click here for view cookie
details</a>");
pwriter.close();

}
}

Cookie content displaying code

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();

}
}

web.xml configuration file

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


<web-app>
<servlet>
<servlet-name>AJPLab91</servlet-name>
<servlet-class>AJPLab91</servlet-class>
</servlet>
<servlet>
<servlet-name>AJPLab92</servlet-name>
<servlet-class>AJPLab92</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AJPLab91</servlet-name>
<url-pattern>/AJPLab91</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AJPLab92</servlet-name>
<url-pattern>/AJPLab92</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

OUTPUT:

15 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

10. Write a program to print even and odd numbers.

Html file code to accept starting and ending range.

<!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.

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Even and Odd Numbers</title>
</head>

<%
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>
<%}%>

16 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

</html>

OUTPUT:

11. Write a program to verify the particular user and redirect to welcome page if credentials
are valid else print proper message.

Html file code to accept login credentials

<!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

<input type="submit" value="Login">


</form>
</div>
</body>
</html>

JSP file code to validate credentials and displaying welcome page.

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


<!DOCTYPE html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>

<%! String name = "raghu",password = "pwdadmin"; %>

<%
String ipname = request.getParameter("userName");
String ippassword = request.getParameter("password");
%>

<% if((name.equals(ipname)) && (password.equals(ippassword))){%>


<%response.sendRedirect("welcome.html");%>

<%}else{%>
<%out.println("Invalid Credentials");%>
<%}%>

Html file code for welcome page

<!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:

18 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

12. Write a program to describe jsp:param,jsp:include and jsp:forward action.

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>

19 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)


Advanced Java Programming Lab Manual

</body>
</html>

JSP file code to demonstrate forward action

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Forward</title>
</head>
<body>
<jsp:forward page="stuDetails.jsp">
<jsp:param name="name" value="Kallesh" />
</jsp:forward>
</body>
</html>

JSP file code to demonstrate include action

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Include</title>
</head>
<body>
<h3>Employee Details</h3>
<jsp:include page="empDetails.jsp" flush="true" />
</body>
</html>

JSP file code to display student details

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Forward Demo</title>
</head>
<body>
<h3>Student Details</h3>
<%

out.println("Name :" + request.getParameter("name")+"<br>");


20 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)
Advanced Java Programming Lab Manual

out.println("Address : Nagnur,Davanagare"+"<br>");
out.println("Degree : BCA"+"<br>");
out.println("Marks : 550"+"<br>");

%>

</body>
</html>

JSP file code to display Employee details

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
out.println("Name : Shree Hari"+"<br>");
out.println("Address : Kashipura,Davanagare"+"<br>");
out.println("Degree : MCA"+"<br>");
out.println("Salary : 55000"+"<br>");
%>
</body>
</html>

OUTPUT:

21 Raghu Gurumurthy, MCA (Site : www.raghug.in, Phone no: 9060130871)

Você também pode gostar