Java File
Java File
Java File
import java.applet.*;
import java.awt.*;
</applet> */
String n;
String id;
n = getParameter("t1");
id = getParameter("t2");
rawString("Name is : "
+ n, 100,100);
g.drawString("Id is:
}
Ex2: Write a program to pass two numbers and pass result to an applet.
import java.awt.*;
import java.applet.*;
</APPLET>*/
JAVA PROGRAMMING
String str;
int a,b,result;
str=getParameter("a");
a=Integer.parseInt(str);
str=getParameter("b");
b=Integer.parseInt(str);
result=a+b;
str=String.valueOf(result);
}
Ex3: Hai.java
import java.applet.*;
import java.awt.*;
*/
Output:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
addMouseListener(this);
addMouseMotionListener(this);
mouseY = 10;
msg
= "Mouse clicked.";
repaint(); }
// save
coordinates
mouseX
= 0;
mouseY = 10;
repaint(); }
// save
coordinate s mouseX
= 0;
mouseY
10;
msg
= "Mouse exited.";
repaint();
// save coordinates
mouseX =
me.getX();
mouseY =
me.getY(); msg =
"Down"; repaint();
1. save
coordinates
mouseX =
me.getX();
mouseY =
repaint();
//save coordinates
mouseX = me.getX();
// show status
}
// Display msg in applet window at current X,Y location.public void paint(Graphics g) {
Output :-
Mouse clicked.
Mouse clicked.
Mouse clicked.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
addKeyListener(this);
showStatus("Key Down");
repaint();
// Display keystrokes.
g.drawString(msg, X, Y);
}
Output:-
ddddddddddddddddddddddd
Key Up
ddddddddddddddddddddddd
Key Down
Files and Streams
Example: Write a program to read data from the keyboard and
write it to myfile.txt file.
import java.io.*;
class Test{
public static void main(String args[])
{
DataInputStream dis=new DataInputStream(System.in);
FileOutputstream fout=new FileOutputStream("myfile.txt");
System.out.println("Entertext @ at the end:”);
char ch;
while((ch=(char)dis.read())!=‟@‟)
fout.write(ch);fout.close();
}
}
Output: javac Test.java
Java Test
Example: Write a program to read data from myfile.txt using
FileInputStream and display it on monitor.
import java.io.*;
class ReadFile
{
public static void main(String args[])
{
FileInputStream fin=new FileInputStream("myfile.txt");
System.out.println(“File Contents:”);
int ch;
while((ch=fin.read())!=-1)
{
System.out.println((char)ch);
}
fin.close();
}
}
Output: javac ReadFile.java
java ReadFile
Simple example of AWT by inheritance
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30); // setting button position
add(b); //adding button into frame
setSize(300,300); //frame size 300 width and 300 height
setLayout(null); //no layout manager
setVisible(true); //now frame will be visible
}
public static void main(String args[]){
First f=new First();
}
}
Simple example of AWT by association
import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
First2 f=new First2();
}
}
Output:-
Click me
Demonstrate Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="ButtonDemo" width=250 height=150>
</applet> */
public class ButtonDemo extends Applet implements
ActionListener
{
String msg = "";
Button yes, no, maybe;
public void init()
{
yes = new Button("Yes");no = new Button("No");
maybe = new Button("Undecided");add(yes);
add(no); add(maybe);
yes.addActionListener(this); no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str =
ae.getActionCommand();
if(str.equals("Yes"))
{
msg = "You pressed Yes.";
}
else if(str.equals("No"))
{
msg = "You pressed No.";
}
else
{
msg = "You pressed Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 6, 100);
}
}
Output:-
Yes No Undecided
├── classes/
│ ├── HelloServlet.class
└── web.xml
Place the compiled .class file in WEB-INF/classes.
Output:
<html>
<head><title>Hello Servlet</title></head>
<body>
<h1>Hello, World!</h1>
<p>This is a simple Java servlet example.</p>
</body>
</html>
2 -Servlet to Display Current Date and Time
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Set response content type
response.setContentType("text/html");
Servlet code
.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Set response content type
response.setContentType("text/html");
Output:-
After submitting the form with the name John Doe and email
john@example.com, the response will be:
<html>
<head><title>Form Data</title></head>
<body>
<h1>Form Data Received</h1>
<p>Name: John Doe</p>
<p>Email: john@example.com</p>
</body>
</html>
File Upload Servlet
HTML
<form action="file-upload" method="post"
enctype="multipart/form-data">
<label for="file">Choose a file:</label>
<input type="file" id="file" name="file">
<br><br>
<button type="submit">Upload</button>
</form>
Servlet code
.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.*;
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Output:-
<html>
<head><title>File Upload</title></head>
<body>
<h1>File Uploaded Successfully</h1>
<p>File Name: example.txt</p>
</body>
</html>
Servlet with Redirect
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Redirect to another website
response.sendRedirect("https://www.example.com");
}
}
Output;-
When you access the servlet, it redirects the browser to
https://www.example.com.
JDBC
1. JDBC Program to Fetch Data from a Database
Table
import java.sql.*;
// SQL query
String query = "SELECT id, name, age FROM users";
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output
If the program successfully inserts a new record into the
users table:
Connected to the database!
1 row(s) inserted!
3. JDBC Program to Update Data
import java.sql.*;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:-
If the age of Alice is updated:
Connected to the database!
1 row(s) updated!
4. JDBC Program to Delete Data
import java.sql.*;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:-
If the record for Charlie is deleted:
Connected to the database!
1 row(s) deleted!