Aaa
Aaa
X. PROGRAM CODE:
import java.applet.*;
Ans.
import java.awt.*;
import java.applet.*;
add(11);add(b1);add(t1);add(ta1);
add(11);
2. Develop a program to select multiple languages known to user(e.g. Marathi, Hindi, English, Sanskrit).
Ans.
import java.awt . * ;
{
public void init()
add(11) ;
3. Write a program to create three buttons with Captions OK, RESET and
CANCEL.
Ans.
import java.awt.*;
import java.applet.*;
{
Button b1 =new Button ("OK");
add(b1);add(b2);add(b3);
X. PROGRAM CODE:
Ans.
c1.add("Winter");
c1.add("Rainy");
add(c1);
}
Ans.
import java.awt.*;
import java.applet.*;
l.add("Jaipur");
l.add("Thane");
l.add("Mumbai");
l.add("Andheri");
l.add("Vile Parle");
l.add("Santracuz");
l.add("Bandra");
l.add("Nallasopara");
l.add("Panvel");
l.add("Dahisar");
add(l);
Ans.
java.applet.*;
l.add("Mint");
l.add("Gujarat Samachar");
l.add("Divya Bhaskar");
l.add("Hindustan Times");
l.add("Punjab Kesari");
l.add("Times of India");
l.add("Mid-Day");
l.add("Maharashtra Times");
add(l);
X. PROGRAM CODE:
Ans.
javax.swing.*; import
java.awt.*;
setLayout(new GridLayout(5,5));
int a=1;
for(int i=0;i<5;i++)
for(int m=0;m<5;m++)
add(new JButton("BUTTON"+a));
A++;
Ans.
javax.swing.*; import
java.awt.*;
for(int i=0;i<9;i++)
add(new JButton("BUTTON"+a));
A++;
Ans.
import java.awt.*;
import java.applet.*;
add(b1);add(b2);add(b3);add(b4);add(b5);
Ans.
import java.awt.*;
import java.applet.*;
setLayout(new BorderLayout());
add(b1,BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH);
add(b3,BorderLayout.EAST);
add(b4,BorderLayout.WEST);
add(b5,BorderLayout.CENTER);
}}
/*<applet code="borderlayout.class" width=200 height=200></applet>*/
PRACTICAL NO.4
X. PROGRAM CODE:
1. Execute the following Program and write the output. import java.awt.*;
javax.swing.*;
CardLayoutExample()
c=getContentPane(); card=new
CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver space c.setLayout(card);
JButton("Boy"); b3=new
JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
card.next(c);
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
XIII. EXERCISE:
Ans.
import java.awt.*;
import javax.swing.*;
jp.setLayout(new GridBagLayout());
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=0;
cst.gridy=0;
jp.add(b1,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=1;
cst.gridy=0;
jp.add(b2,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=0;
cst.gridy=1;
jp.add(b3,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=1;
cst.gridy=1;
jp.add(b4,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridwidth=2;
cst.gridx=0;
cst.gridy=2;
jp.add(b5,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridwidth=2;
cst.gridx=0;
cst.gridy=3;
jp.add(b6,cst);
co.add(jp);
f.setSize(200,200);
f.setVisible(true);
}
}
2. Write Java Program to display following output.
Ans.
import java.awt.*;
import javax.swing.*;
jp.setLayout(new GridBagLayout());
GridBagConstraints cst=new GridBagConstraints();
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=0;
cst.gridy=0;
cst.weighty=0.1;
jp.add(L1,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=1;
cst.gridy=0;
jp.add(T1,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=0;
cst.gridy=1;
jp.add(L2,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=1;
cst.gridy=1;
jp.add(T2,cst);
cst.fill=GridBagConstraints.HORIZONTAL;
cst.gridx=1;
cst.gridy=2;
jp.add(B1,cst);
f.setSize(200,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
PRACTICAL NO. 5
X. PROGRAM CODE:
1. Write a program which creates Menu of different colors and disable menu item for Black color.
Ans.
import java.awt.*;
import java.awt.event.*;
menucolor(String s)
super(s);
setMenuBar(mb);
mb.add(m1);
m1.add(i1);
m1.add(i2);
m1.add(i3);
i3.setEnabled(false);
obj.setSize(200,200);
obj.setVisible(true)
}}
XIII. EXERCISE:
Ans.
MenuBar mb;
setTitle("MenuBar demo");
setSize(500,500); setLayout(null);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
md.setVisible(true);
}
PRACTICAL NO. 6
X. PROGRAM CODE:
Ans.
import javax.swing.*;
import java.awt.*;
import java.util.*;
Container co=getContentPane();
co.setLayout(new FlowLayout());
c1.addItem("Mumbai");
c1.addItem("Solapur");
c1.addItem("Pune");
c1.addItem("Banglore");
co.add(c1);
co.add(l1);
l1.setText("You are in "+c1.getSelectedItem());
1. Write a program to develop a frame to select the different states of India using JComboBox.
Ans.
import javax.swing.*;
import java.awt.*;
import java.util.*;
Container co=getContentPane();
co.setLayout(new FlowLayout());
co.setSize(200,200);
co.setVisible(true);
c.addItem("Maharashtra");
c.addItem("Gujrat");
c.addItem("Punjab");
c.addItem("Karnataka");
co.add(c);
Ans.
jp.add(cb1);
jp.add(cb2);
jp.add(cb3);
jp.add(cb4);
jp.add(cb5);
jp.add(cb6);
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPanejsp=new
JScrollPane(jp,v,h); co.add(jsp);
X. PROGRAM CODE:
Ans.
Container c=getContentPane();
DefaultMutableTreeNode t3=new
DefaultMutableTreeNode("Mango");
DefaultMutableTreeNode t4=new
DefaultMutableTreeNode("Banana");
t1.add(t2);t1.add(t3);t1.add(t4);
add(t);
Ans.
Container c=getContentPane();
DefaultMutableTreeNode top=new
DefaultMutableTreeNode("INDIA");
DefaultMutableTreeNode m=new
DefaultMutableTreeNode("MAHARASHTRA");
top.add(m);
DefaultMutableTreeNode mi=new
DefaultMutableTreeNode("MUMBAI");
DefaultMutableTreeNode n=new
DefaultMutableTreeNode("NASHIK");
DefaultMutableTreeNode nag=new
DefaultMutableTreeNode("NAGPUR");
m.add(mi);m.add(p);m.add(n);m.add(nag);
DefaultMutableTreeNode g=new
DefaultMutableTreeNode("GUJRAT");
top.add(g);
1. Write a JTree program to show root directory and its subFolders of your system.
Ans.
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
{ Container c=getContentPane();
DefaultMutableTreeNode top=new
DefaultMutableTreeNode("OS(C:)");
top.add(f);
DefaultMutableTreeNode a=new
DefaultMutableTreeNode("Android");
DefaultMutableTreeNode m=new
DefaultMutableTreeNode("Notepad++");
DefaultMutableTreeNode g=new
DefaultMutableTreeNode("Microsoft Office");
f.add(a);f.add(m);f.add(g);
DefaultMutableTreeNode nvg=new
DefaultMutableTreeNode("Drivers");
top.add(nvg);
DefaultMutableTreeNode cm=new
DefaultMutableTreeNode("network");
X. PROGRAM CODE:
Ans.
{"3","Aditya","pune"},
{"4","Adeen","bhopal"}};
Ans.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
Container co=getContentPane();
co.setLayout(new BorderLayout());
String co1[]={"ID","NAME","SALARY"};
String rows[][]={{"101","Isha","670000"},
{"102","Tanu","780000"},
{"101","Aditya","700000"}};
1. Write a Java program to create a table of Name of Student, Percentage and Grade of 10 students using
JTable.
Ans.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
Container co=getContentPane();
co.setLayout(new BorderLayout());
X. PROGRAM CODE:
Ans.
import java.awt.*;
import javax.swing.*;
JProgressBarjb;
jpbar123()
jb=new JProgressBar();
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
jp.setVisible(true);
jp.iterate();
jp.setTitle("JPROGRESSBAR");}}
XIII. EXERCISE:
Ans.
JProgressBar jb;
jpbar12()
{
jb=new JProgressBar(0,2000);
setLayout(null); jb.setValue(0);
jb.setBounds(40,40,160,30);
jb.setStringPainted(true); add(jb);
setSize(200,200); setVisible(true);
}
int i=0;
while(i<=1000)
{
jb.setValue(i+10);
i=i+20;
try
{
Thread.sleep(1000);
catch(Exception e){}
jp.loop();
}}
2. Write a Program using JProgressBar to show the progress of ProgressBar when user clicks on
JButton.
Ans.
import java.awt.event.*;
import javax.swing.*;
JProgressBar pb;
jpbar1()
pb=new JProgressBar(0,2000);
setLayout(null);
pb.setValue(0);
pb.setBounds(110,20,200,25);
b1.setBounds(20,20,80,25);
pb.setStringPainted(true);
add(pb);
add(b1);
b1.addActionListener(this);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);}
public void actionPerformed(ActionEvent ae)
int i=0;
if(ae.getSource()==b1)
pb.setVisible(true);
try
while(i<=100)
Thread.sleep(50);
pb.paintImmediately(0,0,200,25);
pb.setValue(i);
i++;
}
}
catch(Exception e1)
System.out.println("exception is="+e1);
m.setSize(300,100);
m.setVisible(true);
}
PRACTICAL NO. 10
X. PROGRAM CODE:
1. Write a program to generate KeyEvent when a ke is pressed and display “Key Pressed” message.
import java.applet.*;
import java.awt.event.*;
addMouseListener(new mymouselistener());
showStatus("pressed");
repaint();
Ans.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
String msg="";
addKeyListener(this);
showStatus("UP");
{
showStatus("DOWN");
msg+=a.getKeyChar();
repaint();
g.drawString(msg,30,70);
3. Develop a program to accept two numbers and display product of two numbers when user pressed “Multiply” button.
Ans.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Button b1;
setLayout(new GridLayout(2,2));
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
b1=new Button("MULTIPLICATION");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(b1);
b1.addActionListener(this);
if(ae.getSource()==b1)
int n=Integer.parseInt(t1.getText());
int m=Integer.parseInt(t2.getText());
int s=n*m;
t3.setText(Integer.toString(s));
X. PROGRAM CODE
Ans.
/*<applet code= "mousedemo.java" width=300 height=200></applet>*/ public class mousedemo extends Applet implements
MouseListener
Label l;
setLayout(null);
l.setBounds(0,100,100,100);
add(l);
l.setText("Mouse Pressed no. of clicks:" + e.getClickCount() + "at position" + e.getX() + "," +e.getY());
{l.setText("Mouse Entered");
l.setText("Mouse exited");
}
XIII. EXERCISE:
1. Write a program to change the background color of Applet when user performs events using Mouse.
Ans.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Label l;
setLayout(null);
addMouseListener(this);
setBackground(Color.red);
setBackground(Color.green);
setBackground(Color.pink);
setBackground(Color.red);
setBackground(Color.yellow);
Ans.
Ans.
import java.awt.event.*;
import java.applet.Applet;
addMouseMotionListener(new demo());
int x=me.getX();
int y=me.getY();
showStatus("Moved at"+x+","+y);
int x=me.getX();
int y=me.getY();
showStatus("Dragged at"+x+","+y);
}
}
X. PROGRAM CODE:
1. Write a program using JPasswordField to set the password character as ‘#’ instead of ‘*’.
Ans.
import java.awt.*;
import java.applet.*;
import javax.swing.*;
add(l1);add(t);add(l2);add(p);add(b1);
1. Write a program using JPasswordField and JTextField to demonstrate the use of user authentication.
Ans.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Label l1,l2,l3;
JTextField t1;
JPasswordField jp1;
JButton b1;
setLayout(new FlowLayout());
t1=new JTextField(15);
l3=new Label("");
b1=new JButton("LOGIN");
add(l1);add(t1);add(l2);add(jp1);add(b1);add(l3);
b1.addActionListener(this);
}
if(t1.getText().equals("Isha")&&(jp1.getText().equals("tpoly")))
else
l3.setText("Invalid User");
Ans.
b1=new JButton("Addition");
b1.addActionListener(this);
add(l1);add(t1);add(l2);add(t2);add(b1);add(l3);
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int a,b,c;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
c=a+b;
l3.setText("Addition:-"+Integer.toString(c));
}
3. Write a program using JPasswordField to accept password from user and if the length is less than 6 characters then error message
should be displayed “Password length must be >6 characters”.
Ans.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
JLabel l1,l2,l3;
JTextField t1;
JPasswordField p1;
JButton b1;
setLayout(new FlowLayout(FlowLayout.LEFT));
l3=new JLabel("");
t1=new JTextField(15);
p1=new JPasswordField(15);
b1=new JButton("LOGIN");
b1.addActionListener(this);
add(l1);add(t1);add(l2);add(p1);add(b1);add(l3);
b1.addActionListener(this);
{
if((p1.getPassword().length<6))
else
l3.setText("OK");
X. PROGRAM CODE:
import java.io.*;
import java.net.*;
try
InetAddress ip=InetAddress.getByName("localhost");
catch(Exception e){System.out.println(e);}
}
XIII. EXERCISE:
Ans.
import java.net.*;
try
InetAddress i=InetAddress.getByName("www.msbte.com");
System.out.println(i);
catch(Exception e){}
}
PRACTICAL NO. 15
X. PROGRAM CODE:
import java.net.*;
class URLDemo
}
XIII. EXERCISE:
1. Write a program using URL class to retrieve the host, protocol, port and file of URL http://www.msbte.org.in
Ans.
import java.net.*;
System.out.println("Protocol: "+hp.getProtocol());
System.out.println("Port: "+hp.getPort());
System.out.println("Host: "+hp.getHost());
System.out.println("File: "+hp.getFile());
2. Write a program using URL and URLConnection class to retrieve the date, content type, content length information
of any entered URL
Ans.
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
{
public static void main(String args[])throws Exception
}
PRACTICAL NO. 18
XIII. EXERCISE:
1. Develop a program to create employee table in database having two columns “emp_id” and “emp_name”.
Ans.
import java.sql.*;
class create {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("driver
loaded"); String uname = "root"; String pass = "Vscode@1";
2. Develop a program to display the name and roll_no of students from “student table” having
percentage >70.
Ans.
import java.sql.*;
class develop {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("driver
loaded"); String uname = "root"; String pass = "Vscode@1";
System.out.println(
"Roll no. " + rs.getInt(1) + "\nName: " + rs.getString(2) +
"\nPercentage: " + rs.getFloat(3));
}
System.out.println("data Displayed");
st.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 19
X. PROGRAM CODE:
1. Write a Program to update row of student table from MSBTE database using Mysql 5.5 a database server.
Ans:
import java.sql.*;
class Exp19_1 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";
2. Write the output of following JDBC code. Use Mysql server 5.5 as database Server.
Ans:
XIII. EXERCISE:
class Exp19_3 {
public static void main(String ar[]) throws Exception { String url =
"jdbc:mysql://localhost:3306/MSBTE"; String uname = "root";
String pass = "Vscode@1";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, uname, pass); String query = "select *
from student";
PreparedStatementst = con.prepareStatement(query); ResultSet rs =
st.executeQuery(); while (rs.next()) {
System.out.println("Roll no. " + rs.getInt(1) + "\t");
System.out.println("Name: " + rs.getString(2) + "\t");
System.out.print("Location: " + rs.getString(3));
}
st.close();
con.close();
}
}
import java.sql.*;
class Exp19_4 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded");
String uname = "root";
String pass = "Vscode@1";
String url = "jdbc:mysql://localhost:3306/MSBTE";
Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Update student set name='Levi', Location='Titan' Where roll=2";
PreparedStatementps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data Updated");
ps.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 20
X. PROGRAM CODE:
class Exp20_1 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";
class Exp20_3 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";
class Exp20_4 {
public static void main(String ar[]) throws Exception {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded");
String uname = "root";
String pass = "Vscode@1";
String url = "jdbc:mysql://localhost:3306/MSBTE";
Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Delete from product where price>500 and id='P1234'";
PreparedStatementps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data deleted");
ps.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 21
X. PROGRAM CODE:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ser1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.close();
}
}
2. Write the output of following code considering below HTML is front end and servlet as back end
Ans:
XIII. EXERCISE:
1. Develop servlet program to print Hello MSBTE in browser window.
Ans:
<!DOCTYPE
html><html>
<head>
<title>Welcome</title><meta
charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"></head>
<body>
<b>Hello
MSBTE</b></body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ser1
extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.close();
}
}
2. Develop a program to receive the parameter through HTML forms and send back
received parameter to browser.
Ans:
<html>
<head>
<title>Servlet 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="pass">
Enter Username: <input type = "text" name = "n">
<!--Enter Password: <input type = "password" name = "p">--><input type="submit"
name="SUBMIT">
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Servlet</title>
<meta charset="UTF-8">
<body><form action="length1">
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
String len=request.getParameter("n");
pw.println("Length= "+len.length());
pw.close();
}
2. Write the output of following code considering below HTML is front end and servlet as back end.
Ans:
XIII. EXERCISE:
1. Develop servlet program to retrieve data from List and Radio Button using HTML Forms.
Ans:
<html>
<head>
<title>Servlet 4</title>
<meta charset="UTF-8">
</head>
<body>
<form action="last">
<option value="AJP">AJP</option>
<option value="OSY">OSY</option>
<option value="ACN">ACN</option>
<option value="EST">EST</option>
</select><br>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
}
2. Develop a program to receive student subject marks through HTML forms TextField and send the response as passed or Failed in
Examination...
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Experiment 22</title>
<meta charset="UTF-8">
</head>
<body>
<form action="StudentMarks">
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
String marks=request.getParameter("m");
if(Integer.parseInt(marks)>=40)
pw.println("Pass");
else
pw.println("Fail");
}
PRACTICAL NO. 23
X. PROGRAM CODE:
Ans:
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession hs=request.getSession();
String id=hs.getId();
pw.close();
2. Write the output of following code considering below HTML is front end and servlet as back end.
Ans.
XIII. EXERCISE:
1. Develop servlet program to display various details about session using HttpSession methods.
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Exp23_3</title>
<meta charset="UTF-8">
</head>
<body>
<form action="Servlet1">
</form>
</body>
</html>
import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
throws ServletException,
IOException{ response.setContentType("text/html"); PrintWriter
pw=response.getWriter(); String name=request.getParameter("n");
pw.println("Welcome "+name); HttpSession
session=request.getSession();
session.setAttribute("uname",name); pw.println("<a href='Servlet2'> visit
<a/>"); pw.close();
import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
HttpSession session=request.getSession(false);
String n=(String)session.getAttribute("uname");
pw.print("Hello "+n);
pw.close();}}
PRACTICAL NO. 24
X. EXERCISE:
1. Develop a program to collect user information using cookie.
Ans.
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="cookie1">
Enter Cookie data: <input type="text" name="data"><input type="submit" value="submit">
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class cookie1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String d = request.getParameter("data");
Cookie co = new Cookie("MyCookie",d);
response.addCookie(co);
pw.println("your cookie has been set to"+d);
pw.println("<a href='cooval1'>view details</a>");
pw.close();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class cooval1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Cookie co[]=request.getCookies();
for(int i=0; i<co.length; i++){ pw.println("Name-> "+co[i].getName());
pw.println("Value-> "+co[i].getValue());
}}