Year 2016
Group B
Q.no 2 Write a swing program to add two numbers given as input by user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class T2016_2 extends JFrame implements ActionListener{ JTextField t1,t2; JLabel sum; JButton b; public T2016_2(){ t1=new JTextField(); t2=new JTextField(); sum=new JLabel(""); b=new JButton("Add"); setLayout(new GridLayout(4,1)); add(t1); add(t2); add(b); add(sum); setSize(200,400); setVisible(true); b.addActionListener(this); } public void actionPerformed(ActionEvent e){ int n1=Integer.parseInt(t1.getText()); int n2=Integer.parseInt(t2.getText()); int s=n1+n2; sum.setText("Sum is: "+s); } public static void main(String[] args) { new T2016_2(); } } |
Q.no 3 Write an applet to check whether given word by user is of length 5 or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.applet.*; import java.awt.*; import java.awt.event.*; public class T2016_3 extends Applet implements KeyListener{ TextField t1; Label lab; public void init(){ t1=new TextField(); add(t1); lab=new Label(); add(lab); setSize(300,300); setLayout(new GridLayout(2,1)); } public void start(){ t1.addKeyListener(this); } public void keyTyped(KeyEvent e){} public void keyPressed(KeyEvent e){} public void keyReleased(KeyEvent e){ String a=t1.getText(); if(a.length()==5){ lab.setText(a+" is of length 5"); } else{ lab.setText(a+" is not of length 5"); } } } |
4. Write a JSP program to display ” Apache Tomcat” eight times.
1 2 3 4 5 6 7 8 9 10 11 |
<html> <head> </head> <body> <% for(int i = 1; i <= 8; i++) { out.println("Apache Tomcat <br />"); } %> </body> </html> |
5. Write a program to set cookie.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import javax.servlet.http.*; import java.io.*; public class Ques5_2016 extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) { response.setContentType("text/html"); try{ PrintWriter pw = response.getWriter(); Cookie cookie1 = new Cookie("FirstName", "abc"); Cookie cookie2 = new Cookie("LastName", "bcd"); cookie1.setMaxAge(24*60*60); cookie2.setMaxAge(24*60*60); response.addCookie(cookie1); response.addCookie(cookie2); pw.println("Hello user cookie has been set to your computer"); pw.close(); }catch(Exception ex){ System.out.println(ex); } } } |
Q.no 6 Write a program to make connection with database. Make your own assumptions about the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.sql.*; public class T2016_6 { static Connection con; public static void main(String[] args) throws Exception{ connect(); } public static void connect() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/fomdb"; con=DriverManager.getConnection(url,"root",""); if(con!=null){ System.out.println("Successfully connected to database"); } else{ System.out.println("Error connecting to database!"); } } } |
Group C
Q.no 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.awt.*; public class T2016_7 extends Frame{ Label l1,l2; Panel p1,p2; TextField t1; CheckboxGroup cb; Checkbox a,b,c; public T2016_7() { l2=new Label("Gender: "); cb=new CheckboxGroup(); a=new Checkbox("Male",true,cb); b=new Checkbox("Female",false,cb); c=new Checkbox("Other",false,cb); add(l2); add(a); add(b); add(c); setLayout(new FlowLayout()); setSize(400,200); setVisible(true); } public static void main(String[] args) { new T2016_7(); } } |
Q.no 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloWorld extends HttpServlet { private String message; public void init() throws ServletException { // Do required initialization message = "Hello Tomcat"; } 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("<h1>" + message + "</h1>"); } public void destroy() { } } |