0% found this document useful (0 votes)
10 views33 pages

Advanced Java Programming - Lab Record

The document provides multiple examples of Java web technologies, including HTML to Servlet communication, Applet to Servlet communication, and JSP applications. It covers various programming aspects such as creating servlets, handling user input, and performing operations like addition and email sending. Additionally, it includes examples of using Enterprise JavaBeans (EJB) and Java Database Connectivity (JDBC) for database operations.

Uploaded by

madhumithaa733
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)
10 views33 pages

Advanced Java Programming - Lab Record

The document provides multiple examples of Java web technologies, including HTML to Servlet communication, Applet to Servlet communication, and JSP applications. It covers various programming aspects such as creating servlets, handling user input, and performing operations like addition and email sending. Additionally, it includes examples of using Enterprise JavaBeans (EJB) and Java Database Connectivity (JDBC) for database operations.

Uploaded by

madhumithaa733
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

HTML TO SERVLET COMMUNICATION

PROGRAM:

[Link]
<!DOCTYPE html>
<html>
<head>
<title>HTML to Servlet Communication</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center><h1>HTML to Servlet Communication</h1></center>
<form method= "get" action="FirstServlet">
<center> User Login Page </center>
<br><br><br>
<center> UserName<input type="text" name=fname></center>
<center> Password<input type="password"></center>
<br><br>
<center> <input type="submit" value="sumbit"><input type="reset" value="clear"></center>
</form>
</body>
</html>

[Link]
package pkg;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(name = "FirstServlet", urlPatterns = {"/FirstServlet"})
public class FirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
String name=[Link]("fname");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet FirstServlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center> <h1>Welcome\t" + name + "\t to Servlet</h1></center>");
[Link]("</body>");
[Link]("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
OUTPUT:

RESULT:
SERVLET TO HTML COMMUNICATION

PROGRAM:

[Link]
<!DOCTYPE html>
<html>
<head>
<title> Servlet to HTML Communication</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="MyServlet" method="post">
<br> <label for="userName"><b>Enter your name:</b></label>
<center> <input type="text" id="userName" name="userName"></center><br>
<center> <input type="submit" value="Submit"></center>
</form>
</body>
</html>

[Link]
package pkg;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
String userName = [Link]("userName");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet MyServlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1><center>Hello, " + userName + "!</center></h1>");
[Link]("<p><center>This message is from the Servlet.</center></p>");
[Link]("</body>");
[Link]("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
OUTPUT:

RESULT:
APPLET TO SERVLET COMMUNICATION

PROGRAM:

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class MyApplet extends Applet implements ActionListener {
Button b;
TextField tf1;
Label l1;
public void init() {
l1 = new Label("User Name:");
add(l1);
tf1 = new TextField(10);
add(tf1);
b = new Button("Send");
[Link](this);
add(b);
}
public void actionPerformed(ActionEvent ae) {
try {
String name = [Link]();
String qrystr = "?uname=" + name;
String url = "[Link] + qrystr;
URL requrl = new URL(url);
AppletContext apc = getAppletContext();
[Link](requrl);
} catch (Exception e) {
[Link]();
}
}
}

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter pw = [Link]();
String name = [Link]("uname");
Calendar cl = [Link]();
int h = [Link](Calendar.HOUR_OF_DAY);
if (h <= 12)
[Link]("Good Morning: " + name);
else if (h <= 16)
[Link]("Good Afternoon: " + name);
else if (h <= 20)
[Link]("Good Evening: " + name);
else
[Link]("Good Night: " + name);
[Link]();
}
}
OUTPUT:

RESULT:
SERVLET TO APPLET COMMUNICATION

PROGRAM :

Applet Program ([Link])


import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
public class NewApplet extends Applet {
URL url;
URLConnection servletConnection;
ObjectInputStream input;
public void init() {
try {
url = new URL("[Link]
servletConnection = [Link]();
[Link](true);
[Link](true);
[Link](false);
[Link](false);
} catch (Exception e) {
[Link]();
}
}
public void paint(Graphics g) {
try {
input = new ObjectInputStream([Link]());
[Link]([Link]);
[Link]("Servlet-Applet Communication", 50, 50);
String str = (String) [Link]();
[Link]([Link]);
[Link]("Message from Servlet: " + str, 20, 100);
[Link]();
} catch (Exception e) {
[Link]();
}
}
}

Servlet Program ([Link])


import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
@WebServlet("/NewServlet")
public class NewServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
ObjectOutputStream output = null;
try {
output = new ObjectOutputStream([Link]());
String st = "Applet Receiving";
[Link](st);
[Link]();
} catch (Exception e) {
[Link]();
} finally {
if (output != null) {
[Link]();
}
}
}
}
OUTPUT:

RESULT:
DESIGNING ONLINE APPLICATION USING JSP

PROGRAM:

[Link]
<!DOCTYPE html>
<html>
<head>
<title>Addition of two numbers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<iframe src="[Link]" width="700" height="500"></iframe>
</body>
</html>

[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Addition Program</title>
</head>
<body>
<h2>Add Two Numbers</h2>
<form action="[Link]" method="post">
Enter Number 1: <input type="text" name="n1"><br><br>
Enter Number 2: <input type="text" name="n2"><br><br>
<input type="submit" value="Add">
</form>
</body>
</html>

[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Addition of two numbers</title>
</head>
<body>
<%
int a = [Link]([Link]("n1"));
int b = [Link]([Link]("n2"));
int sum = a + b;
[Link]("<h3>Result: " + a + " + " + b + " = " + sum + "</h3>");
%>
</body>
</html>
OUTPUT:

RESULT:
CREATING JSP PROGRAM USING JAVABEANS

PROGRAM:

[Link]
<!DOCTYPE html>
<html>
<head>
<title>Simple Interest Calculation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<iframe src="[Link]" width="700" height="500"></iframe>
</center>
</body>
</html>

[Link]
package beans;
public class SimpleInterest {
private double principle;
private double rate;
private double time;
public SimpleInterest() {}
public void setPrinciple(double principle)
{
[Link] = principle;
}
public void setRate(double rate)
{
[Link] = rate;
}
public void setTime(double time) {
[Link] = time;
}
public double getInterest(){
return (principle * rate * time) / 100;
}
}
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="si" class="[Link]" scope="page"/>
<jsp:setProperty name="si" property="*"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
c<title>Simple Interest Calculator</title>
</head>
<body>
<form method="post">
<h1>SIMPLE INTEREST CALCULATION</h1>
Principle: <input type="text" name="principle" ><br><br>
Rate (%): <input type="text" name="rate"><br><br>
Time (years): <input type="text" name="time"><br><br>
<input type="submit" value="Calculate">
</form>
<%
String p = [Link]("principle");
String r = [Link]("rate");
String t = [Link]("time");
if (p != null) {
%>
<p>Principle Entered: <b><%= p %></b></p>
<p>Rate Entered: <b><%= r %></b></p>
<p>Time Entered: <b><%= t %></b></p>
<h3>Result:</h3>
Simple Interest = <%= [Link]() %>
<% } %>
</body>
</html>

OUTPUT:
RESULT:

WORKING WITH ENTERPRISE JAVABEANS


PROGRAM:

[Link]
package [Link];
import [Link];
@Stateless
public class MultiplierBean {
public int multiply(int a, int b) {
return a * b;
}
}
[Link]
package [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
@WebServlet("/MultiplierServlet")
public class MultiplierServlet extends HttpServlet {
@EJB
MultiplierBean multiplier;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
[Link]("text/html");
PrintWriter out = [Link]();
int a = 7;
int b = 6;
int product = [Link](a, b);
[Link]("<html><body>");
[Link]("<h2>Simple EJB Multiplication Example</h2>");
[Link]("<p>Product of " + a + " × " + b + " = " + product + "</p>");
[Link]("</body></html>");
}
}

OUTPUT:
RESULT:

PERFORMING JAVA DATABASE CONNECTIVITY

PROGRAM:
import [Link].*;
public class JDBCExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "";
try {
[Link]("[Link]");
Connection conn = [Link](url, username, password);
[Link]("Connected to database!");
String insertSQL = "INSERT INTO users (name, email) VALUES (?, ?)";
PreparedStatement pstmt = [Link](insertSQL);
[Link](1, "Alice");
[Link](2, "alice@[Link]");
[Link]();
[Link]("Data inserted!");
String selectSQL = "SELECT * FROM users";
Statement stmt = [Link]();
ResultSet rs = [Link](selectSQL);
[Link]("\nData from users table:");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
String email = [Link]("email");
[Link](id + " | " + name + " | " + email);
}
[Link]();
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}

OUTPUT:
RESULT:

CREATING AND SENDING EMAIL WITH JAVA

PROGRAM:

import [Link];
import [Link].*;
import [Link].*;
public class SendEmail {
public static void main(String[] args) {
final String from = "alice@[Link]";
final String password = "cs2025";
String to = "receiveremail@[Link]";
String host = "[Link]";
Properties props = new Properties();
[Link]("[Link]", "true");
[Link]("[Link]", "true");
[Link]("[Link]", host);
[Link]("[Link]", "587");
Session session = [Link](props,new [Link]() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
Message message = new MimeMessage(session);
[Link](new InternetAddress(from));
[Link]([Link],[Link](to));
[Link]("Java Mail Test");
[Link]("Hello! This is a test email sent from Java using NetBeans.");
[Link](message);
[Link]("Email sent successfully...");
} catch (MessagingException e) {
[Link]();
}
}
}

OUTPUT:
RESULT:

CREATING WEB SERVICES WITH RMI

PROGRAM:

[Link]
package newpackage;
import [Link];
import [Link];
public interface Adder extends Remote {
int add(int a, int b) throws RemoteException;
}

[Link]
package newpackage;
import [Link];
import [Link];
public class AdderImpl extends UnicastRemoteObject implements Adder {
protected AdderImpl() throws RemoteException {
super();
}
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
[Link]
package newpackage;
import [Link];
import [Link];
public class Client {
public static void main(String[] args) {
try {
Registry registry = [Link]("localhost", 1399);
Adder stub = (Adder) [Link]("AdderService");
int result = [Link](5, 3);
[Link]("Result:5+3 = " + result);
} catch (Exception e) {
[Link]();
}
}
}
[Link]
package newpackage;
import [Link];
import [Link];
public class Server {
public static void main(String[] args) {
try {
AdderImpl obj = new AdderImpl();
Registry registry = [Link](1399);
[Link]("AdderService", obj);
[Link]("Server is running...");
} catch (Exception e) {
[Link]();
}
}
}

OUTPUT:
RESULT:

BUILDING WEB APPLICATIONS

PROGRAM:

[Link]:
<!DOCTYPE html>
<html>
<head>
<title>Simple Web App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Welcome to My Web Application</h2>
<form action="HelloServlet" method="post">
Enter your name: <input type="text" name="uname">
<input type="submit" value="Say Hello">
</form>
</body>
</html>

[Link]
package pkg;
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
@WebServlet(name = "HelloServlet", urlPatterns = {"/HelloServlet"})
public class HelloServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
String name = [Link]("uname");
[Link]("<!DOCTYPE html>");
[Link]("<html><body>");
[Link]("<h2>Hello, " + name + "!</h2>");
[Link]("<p>Welcome to your first Java Web Application.</p>");
[Link]("</body></html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
OUTPUT:
RESULT:

FINDING SIMPLE INTEREST USING SESSION MANAGEMENT.

PROGRAM:
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Simple Interest using Session</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Simple Interest Calculator</h2>
<form action="SimpleInterestServlet" method="post">
Principal: <input type="text" name="p"><br><br>
Rate: <input type="text" name="r"><br><br>
Time: <input type="text" name="t"><br><br>
<input type="submit" value="Calculate">
</form>
</body>
</html>

[Link]

import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
@WebServlet("/SimpleInterestServlet")
public class SimpleInterestServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
double p = [Link]([Link]("p"));
double r = [Link]([Link]("r"));
double t = [Link]([Link]("t"));
HttpSession session = [Link]();
[Link]("p", p);
[Link]("r", r);
[Link]("t", t);
double si = (p * r * t) / 100;
[Link]("text/html");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet SimpleInterestServlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h2>Simple Interest Result</h2>");
[Link]("Principal: " + p + "<br>");
[Link]("Rate: " + r + "<br>");
[Link]("Time: " + t + "<br>");
[Link]("<b>Simple Interest = " + si + "</b>");
[Link]("</body>");
[Link]("</html>");
}
}
}

OUTPUT:
RESULT:

You might also like