Java 22
Java 22
numbers given as input by the user. import javax.servlet.http.*; Make your own assumption about the database.
import java.awt.*; import java.io.*; import java.sql.*;
import javax.swing.*; public class Ques5_2016 extends HttpServlet{ public class T2016_6 {
import javax.swing.event.*; public void doGet(HttpServletRequest static Connection con;
import java.awt.event.*; request,HttpServletResponse response) public static void main(String[] args) throws
public class T2016_2 extends JFrame implements { response.setContentType("text/html"); Exception{
ActionListener{ try{ connect();
JTextField t1,t2; PrintWriter pw = response.getWriter(); }
JLabel sum; Cookie cookie1 = new Cookie("FirstName", "abc"); public static void connect() throws Exception{
JButton b; Cookie cookie2 = new Cookie("LastName", "bcd"); Class.forName("com.mysql.jdbc.Driver");
public T2016_2(){ cookie1.setMaxAge(24*60*60); String url="jdbc:mysql://localhost:3306/fomdb";
t1=new JTextField(); cookie2.setMaxAge(24*60*60); con=DriverManager.getConnection(url,"root","");
t2=new JTextField(); response.addCookie(cookie1); if(con!=null){
sum=new JLabel(""); response.addCookie(cookie2); System.out.println("Successfully connected to
b=new JButton("Add"); pw.println("Hello user cookie has been set to your database");
setLayout(new GridLayout(4,1)); computer"); }
add(t1); add(t2); add(b); add(sum); pw.close(); else{
setSize(200,400); }catch(Exception ex){ System.out.println("Error connecting to
setVisible(true); System.out.println(ex); } } } database!");
b.addActionListener(this); }}}
}
public void actionPerformed(ActionEvent e){ Write a servlet to display “Hello Tomcat”
int n1=Integer.parseInt(t1.getText()); import java.io.*;
import javax.servlet.*; Make a user form with three radio buttons.
int n2=Integer.parseInt(t2.getText());
import javax.servlet.http.*; import java.awt.*;
int s=n1+n2;
public class HelloWorld extends HttpServlet { public class T2016_7 extends Frame{
sum.setText("Sum is: "+s);
private String message; Label l1,l2;
}
public void init() throws ServletException Panel p1,p2;
public static void main(String[] args) {
{ TextField t1;
new T2016_2();
message = "Hello Tomcat"; CheckboxGroup cb;
}}
} Checkbox a,b,c;
public void doGet(HttpServletRequest request, public T2016_7() {
Write an applet to check whether given
HttpServletResponse response) l2=new Label("Gender: ");
word by user is of length 5 or not.
throws ServletException, IOException cb=new CheckboxGroup();
import java.awt.*;
{ a=new Checkbox("Male",true,cb);
import java.applet.*;
response.setContentType("text/html"); b=new Checkbox("Female",false,cb);
public class WordLength extends Applet {
PrintWriter out = response.getWriter(); c=new Checkbox("Other",false,cb);
String word, temp;
out.println("<h1>" + message + "</h1>"); add(l2); add(a); add(b); add(c);
int n;
} setLayout(new FlowLayout());
public void start () {
public void destroy() setSize(400,200);
word = getParameter("input");
{}} setVisible(true);
n = word.length ();
}
if(n==5)
public static void main(String[] args) {
temp = "The word is of length 5";
new T2016_7();
else
Create a HTML documents that contains }}
temp = "The word is not of length 5";
header information of a page
}
and include this HTML as a header file in JSP.
public void paint (Graphics g){ Create JSP page to display all odd numbers from
Step:1 Create an HTML header file (e.g.,
g.drawString (temp,20, 20); 10 to 50.
header .html)
}} <html>
<!-- header.html -->
The HTML file for above applet is: <head>
<header>
<html> <title></title>
<h1>Welcome to My Website</h1>
<body> </head>
<nav> <body>
<applet code ="WordLength.class"
<ul> <%
width = "400" height = "500">
<li><a href="home.jsp">Home</a></li> for(int i=10; i<=50; i++){
<param name = "input" value =" This is Applet">
<li><a href="about.jsp">About</a></li> %>
</applet> if(i%2!=0){
<li><a href="contact.jsp">Contact</a></li>
</html> (%i = %)
</ul>
}
</nav>
Write a JSP program to display <%
</header> }
“Apache Tomcat” eight times.
<html> Step:2 Include this header in your jsp (eg.,index.jsp) %>
<head> <!-- index.jsp --> </body>
<%@ include file="header.html" %> </html>
</head>
<body> <p>This is the main content of the page.</p>
<%
for(int i = 1; i <= 8; i++) {
out.println("Apache Tomcat <br />");
}
%>
Make an applet to display the name of your college.
</body>
import java.applet.Applet;
</html>
import java.awt.Graphics;
public class College extends Applet{
public void paint (Graphics g){
g.drawString ("IMS College",20,150);
}
}
/*<applet code = "College.class" width="200"
height="200"></applet>*/
Write a servlet to find the reverse of a string Create a servlet to find out factorial of a positive Write a program to demonstrate the key event
import java.io.*; integer value for moving an image left, right, up and down.
import javax.servlet.*; import java.io.IOException; import java.awt.*;
import javax.servlet.http.*; import java.io.PrintWriter;
import java.awt.event.*;
import javax.swing.*;
import javax.servlet.ServletException;
public class ReverseStringServlet extends HttpServlet { import javax.servlet.http.HttpServlet; public class KeyEventDemo extends JFrame implements
protected void doGet(HttpServletRequest request, import javax.servlet.http.HttpServletRequest; KeyListener {
HttpServletResponse response) import javax.servlet.http.HttpServletResponse; JLabel l;
throws ServletException, IOException { int x = 0, y = 0;
String inputString = request.getParameter("input"); public class FactorialServlet extends HttpServlet { public KeyEventDemo() {
String reversedString = new StringBuilder(inputString) protected void doGet(HttpServletRequest request,
setTitle("KeyEventDemo");
.reverse().toString( setSize(300, 300);
HttpServletResponse response) throws setLayout(new FlowLayout());
response.setContentType("text/html"); ServletException, IOException { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PrintWriter out = response.getWriter(); String numStr = l = new JLabel("Press any key on the keyboard");
out.println("Reversed String: " + reversedString); request.getParameter("number"); add(l);
}} int number = Integer.parseInt(numStr); addKeyListener(this);
int factorial = 1; setVisible(true);
Create an applet that contains 3 text fill and for (int i = 1; i <= number; i++) {
}
two buttons (Add and subtract). public void keyPressed(KeyEvent ke) {
factorial *= i; l.setText("You pressed: " + ke.getKeyChar());
Perform addition and subtraction } if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
when add and subtract buttons are clicked. response.setContentType("text/html"); x += 10;
public class Ap extends Applet{ PrintWriter out = response.getWriter(); l.setLocation(x, y);
int a, b; out.println("<html><body>"); } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
public void start () { out.println("<h1>Factorial of " + number + " is: x -= 10;
string b= getparameter("b"); " + factorial + "</h1>");
l.setLocation(x, y);
string a= getparameter("a"); } else if (ke.getKeyCode() == KeyEvent.VK_UP) {
out.println("</body></html>"); y -= 10;
try{ }} l.setLocation(x, y);
if(a!= null && b!=null){ } else if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
a= Integer.parseInt("a"); y += 10;
b= Integer.parseInt("b"); Create an applet that reads the l.setLocation(x, y);
int sub=a-b; parameter supplied from the <param> tag } }
int sum=a+b; and display the parameter value.
public void keyReleased(KeyEvent ke) {
}catch(Exception e){ l.setText("You released: " + ke.getKeyChar());
Also create a suitable HTML file. }
System.out.println(e); import java.applet.*; public void keyTyped(KeyEvent ke) {
System.out.println("sum" +sum); import java.awt.*; l.setText("You typed: " + ke.getKeyChar());
System.out.println("sub" +sub); import java.applet.*; }
} /* public static void main(String[] args) {
} <applet code="T2018_5.class" new KeyEventDemo();
<applet code="add.Applet"> height="300" width="500">
}}
<para name="a" value="50"> <param name="name" value="Laxmi"
<para name="b" value="60"> /> Assume a database named "college_db", which contains
</applet> <param name="age" value="20" /> a table named "employees" with attributes
</applet> (employee_id, employee_name, contact_number, salary).
*/ Write a JDBC program to update the salary of employee
public class T2018_5 extends Applet to 20,000 whose employee_id = 6
{ import java.sql.*;
String n; class Update
String a; {
public void init(){ public static void main(String args[])
n=getParameter("name"); {
a=getParameter("age"); try
} {
public void paint(Graphics g){ Class.forName("com.mysql.jdbc.Driver");
g.drawString("Name is: " + n, 20, 20); Connection
g.drawString("Age is: " + a, 20, 40); con=DriverManager.getConnection("jdbc:mysql://localhost:
} 3306/College_db","root","");
Create a Swing GUI that contains a combo box } Statement stmt=con.createStatement();
and a text field. When the users select any item String query="update employees set salary=20000 where
in employee_id=6";
the combo box, it will be displayed in the text field. int i=stmt.executeUpdate(query);
import javax.swing.*; System.out.println(i+" records updated");
import java.awt.event.*; con.close();
public class ComboBoxExample { }
public static void main(String[] args) { catch(Exception e)
JFrame frame = new JFrame("ComboBox Example"); {
frame.setSize(300, 150); System.out.println(e);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}}
frame.setLayout(null);
String[] items = {"Item 1", "Item 2", "Item 3"};
Write a JSP to display”Hello form
JComboBox<String> comboBox = new JComboBox<>(items);
jsp” 10 times.
comboBox.setBounds(50, 30, 100, 20); <html>
JTextField textField = new JTextField(); <head>
textField.setBounds(50, 60, 100, 20); <title> </title>
comboBox.addActionListener(new ActionListener() { </head>
public void actionPerformed(ActionEvent e) <body>
String selectedItem = (String) comboBox.getSelectedItem(); <%
String i=" Hello from jsp";
textField.setText(selectedItem);
For( i=1; i<=10; i++){
} }); %>
frame.add(comboBox); <%i = %>
frame.add(textField); <% }
frame.setVisible(true); %>
}} </body>
</html>
Write a servlet to read the values of principal, time and rate
from a HTML form and display the simple interest.
Also write the contents of HTML page and web.xml file
*/
import java.io.*;
import javax.servlet.*;
WAP to insert 5 records into the table “Book”
that contains (id, title, author, price) WAP of Adapter class.
inside the database “Library”. import java.awt.*;
import java.sql.*; import java.awt.event.*;
public class T2018_4 {
private static Connection con; public class AdapterClassExample extends
private static Statement s; Frame {
public static void main(String[] args) throws Exception{ public AdapterClassExample() {
insertRecords(); addMouseListener(new
} MouseAdapter() {
public static void insertRecords() throws Exception{ public void
Class.forName("com.mysql.jdbc.Driver"); mouseClicked(MouseEvent e) {
String url="jdbc:mysql://localhost:3306/Library"; System.out.println("Mouse
con=DriverManager.getConnection(url,"root",""); Clicked");
if(con!=null){ }
s=con.createStatement(); });
String sql="INSERT INTO book (id,title,author.price) VALUES " setSize(300, 200);
+ "(25,'AbcBook','AbcAuthor',2500)," setVisible(true);
+ "(47,'MnoBook','MnoAuthor',800)," }
+ "(33,'PqrBook','PqrAuthor',625)," public static void main(String[] args) {
+ "(49,'XyzBook','XyzAuthor',1200)," new AdapterClassExample();
+ "(11,'StuBook','StuAuthor',1200) "; }}
int res=s.executeUpdate(sql);
if(res!=-1){
System.out.println("Successfully inserted in table BOOK");
}
else{
System.out.println("Could not insert into table");
}}
else{
System.out.println("Error in connectiong to database!");
} }}
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}