Professional Documents
Culture Documents
Practical No:01: AIM: Develop Simple Servlet Question Answer Application To Demonstrate Use of Index - HTML
Practical No:01: AIM: Develop Simple Servlet Question Answer Application To Demonstrate Use of Index - HTML
index.html:-
<html>
<head>
<title>Question Answer</title>
<meta charset="UTF-8">
</head> <body>
</form>
</body> </html>
Aques.java:-
package Aques;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Aques extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
PrintWriter out=resp.getWriter();
int r=0;
int a=Integer.parseInt(req.getParameter("f1"));
int b=Integer.parseInt(req.getParameter("f2"));
if(a==2) r++;
if(b==1) r++;
out.println("total score is:"+r);
}
}
Html form:
J pages
J S Pages
J Server page
2.What is full form of JVM?
J Machine
JV Machine
JV
Done
Output:
Practical No:02
Source Code:-
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Calulator1.java:-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/Calculator"})
@Override
PrintWriter out=respone.getWriter();
try
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
int c=0;
String opt=request.getParameter("btn");
if(opt.equals("+"))
c=n1+n2;
if(opt.equals("-"))
c=n1-n2;
if(opt.equals("*"))
c=n1*n2;
if(opt.equals("/"))
c=n1/n2;
if(opt.equals("%"))
c=n1%n2;
out.println("result"+c);
catch(Exception e)
out.println("error"+e.getMessage());
Output:-
Enter no 1:
Enter no 2:
+ - * / %
Practical No:03
AIM:Develop a JSP Application to accept RegistrationDetails form user and store it into the database
table.
html.jsp file:
<html>
<head>
<body>
</form>
</body>
</html>
reg.jsp file:
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","tiger");
ps.setInt(1,regnumber);
ps.setString(2,emailid);
ps.setString(3,name);
ps.executeUpdate();
out.println("data added");
ps.close();
con.close();
catch(Exception e)
%>
</body>
</html>
Output:
Practical no:04
AIM: Develop JSP Application to Authenticate User Login as per the Registration
details .If login success the forward user to index page otherwise show login failure
message.
CODE:index.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","tiger");
String uname1=request.getParameter("uname");
String pass1=request.getParameter("pass");
ps.setString(1, uname1);
ps.setString(2, pass1);
ResultSet rs=ps.executeQuery();
if(!rs.next())
%>
<jsp:include page="index.html;"/>
<%}
else
%>
<jsp:forward page="AuthenticatedUser.jsp"/>
<%
}
%>
</body>
</html>
AuthenticatedUser.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%=request.getParameter("uname")%>
</body>
</html>
HTML Output:
LOGIN
Username: Password:
BROWSER OUTPUT:
Practical No:05(A)
AIM:Write a java program to present a set of choices for a user to select Stationary
products and display the price of Product after Selection from the list.
CODE:
package product9;
import java.awt.Container;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
JList list;
String prod[]={"pen","notebook","pencil","Graph","colorbox","files"};
int price[]={5,40,20,10,140,50};
JScrollPane jsp;
JLabel lbl1;
Container c;
Product9()
super("Product Details:");
c=getContentPane();
list=new JList(prod);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
jsp=new JScrollPane(list);
c.add(jsp);
lbl1=new JLabel("");
c.add(lbl1);
setSize(400,400);
setDefaultCloseOperation(3);
setVisible(true);
@Override
int val=list.getSelectedIndex();
lbl1.setText("Price="+ price[val]);
new Product9();
}
OUTPUT:
Practical No:05(B)
CODE:
package emptable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
JTable jt;
DefaultTableModel dtm;
JScrollPane jsp;
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1;
EmpTable()
super("Employee Table:");
Object[][] data=null;
dtm=new DefaultTableModel(0,3);
dtm.setDataVector(data,head);
jt=new JTable(dtm);
jt.setGridColor(Color.RED);
jt.setRowSelectionAllowed(true);
jsp=new JScrollPane(jt);
p1.add(jsp);
l2=new JLabel("Name");
l3=new JLabel("Age");
t1=new JTextField(5);
t2=new JTextField(15);
t3=new JTextField(3);
b1.addActionListener(this);
p2.add(l1);
p2.add(t1);
p2.add(l2);
p2.add(t2);
p2.add(l3);
p2.add(t3);
p2.add(b1);
Container c=getContentPane();
c.add(p1,BorderLayout.NORTH);
c.add(p2,BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
@Override
String no=t1.getText();
String name=t2.getText();
String age=t3.getText();
Object[] data={no,name,age};
dtm.addRow(data);
t1.setText("");
t2.setText("");
t3.setText("");
new EmpTable();
}
OUTPUT:
Practical No:06(A)
AIM:Write a java program using split pane to demonstrate a screen divided in two
parts,one part contains the names of planets and another Displays the image of
planets.When user selects the planets name from Left Screen,appropriate image of
planets displayed in right screen.
CODE:
package split;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
JList list1;
JScrollPane jsp1;
JLabel lbl;
JSplitPane pane;
ImageIcon img1,img2,img3,img4,img5,img6,img7,img8,img9;
Container c;
Split()
super("Planet display");
c=getContentPane();
String[] st={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
list1=new JList(st);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.addListSelectionListener(this);
jsp1=new JScrollPane(list1);
img1=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\mercury.jpg");
img2=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\venus.jpg");
img3=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\earth.jpg");
img4=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\mars.jpg");
img5=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\jupiter.jpg");
img6=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\saturn.jpg");
img7=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\uranus.jpg");
img8=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\neptune.jpg");
img9=new ImageIcon("C:\\Users\\exam.VSIT-X119-11\\Desktop\\pluto.jpg");
pane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp1,lbl);
pane.setDividerLocation(80);
c.add(pane);
setSize(400,400);
setDefaultCloseOperation(3);
setVisible(true);
{
int ch=list1.getSelectedIndex() +1;
switch(ch)
new Split();
}
OUTPUT:
Practical No:06(B)
CODE:
package jfilechooserdemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
JMenuBar mb;
JMenu f;
JMenuItem o,x;
JFileChooser fc;
JTextArea ta;
JDialog d;
Jfilechooserdemo(String s)
super(s);
mb=new JMenuBar();
setJMenuBar(mb);
f=new JMenu("File");
mb.add(f);
o=new JMenuItem("Open...");
x=new JMenuItem("Exit");
f.add(o);
f.add(x);
f.setMnemonic(KeyEvent.VK_F);
o.setMnemonic(KeyEvent.VK_O);
x.setMnemonic(KeyEvent.VK_X);
fc=new JFileChooser("C:\\Users\\dell\\Desktop");
o.addActionListener(this);
x.addActionListener(this);
ta=new JTextArea();
ta.setEditable(false);
Container c=getContentPane();
c.add(new JScrollPane(ta));
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
if(ae.getSource()==o) loadFl();
else if(ae.getSource()==x)
dispose();
System.exit(0);
void loadFl()
int result=fc.showOpenDialog(this);
File f=fc.getSelectedFile();
try
ta.setText("");
int count=fr.read(buffer,0,buffer.length);
ta.append(new String(buffer,0,count));
catch(IOException ie)
System.out.println(ie);
OUTPUT:
Practical no:06(C)
AIM:Write a java program for JColorChooserDemo.(split pane)
CODE:
package jcolorfiledemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
JMenuBar mb;
JMenu e;
JMenuItem c;
JColorChooser cc;
JTextArea ta;
JDialog d;
JColorFileDemo(String s)
super(s);
mb=new JMenuBar();
setJMenuBar(mb);
e=new JMenu("Edit");
e.setMnemonic('E');
mb.add(e);
c.setMnemonic(KeyEvent.VK_C);
cc=new JColorChooser();
c.addActionListener(this);
ta=new JTextArea();
Container c=getContentPane();
c.add(new JScrollPane(ta));
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
if(ae.getSource()==c)
@Override
},null);
cc.setColor(ta.getForeground());
d.setVisible(true);
Color c=cc.getColor();
ta.setForeground(c);
ta.repaint();
OUTPUT:
Practical No:-08
AIM:Develop a web application to add items in the inventory using JSF.
CODE:
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Product table</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
</h:panelGrid>
</h:form>
</h:body>
</html>
Success.xhtml
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
</h:body>
</html>
Error.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
</h:body>
</html>
Faces-config-xml
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>yes</from-outcome>
<to-view-id>/Succes.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>no</from-outcome>
<to-view-id>/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>b1</managed-bean-name>
<managed-bean-class>been.Productdet</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Productdet.java
package been;
import java.sql.*;
return pcode;
this.pcode = pcode;
return pname;
this.pname = pname;
{ try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/producdb","root","tiger");
ps.setInt(1,pcode);
ps.setString(2,pname);
ps.executeUpdate();
return "yes";
}
catch(Exception e)
return "no";
Output:
Practical No:09
AIM:Develop a Room Reservation System Application Using Enterprise Java Beans.
index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Interface-RoomLocal.java
package ejb;
import javax.ejb.Local;
@Local
}
Room.java
package ejb;
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
@Override
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ejbdb","root","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql1);
rs.next();
int total=rs.getInt(1);
int occ=rs.getInt(2);
int free=total-occ;
if (free>=no)
ps.setInt(1,occ+no);
int res=ps.executeUpdate();
return 1;
else
return 0;
catch (Exception e)
return 0;
@Override
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ejbdb","root","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql1);
rs.next();
int total=rs.getInt(1);
int occ=rs.getInt(2);
if (occ>=no)
PreparedStatement ps=con.prepareStatement(sql2);
ps.setInt(1,occ-no);
int res=ps.executeUpdate();
return 1;
else
return 0;
catch (Exception e)
return 0;
Servlet-Roomclient
package servlet;
import ejb.RoomLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
PrintWriter out=resp.getWriter();
try
int no=Integer.parseInt(req.getParameter("tl"));
String b =req.getParameter("btn");
int res=0;
if(b.equals("checkin"))
res=obj.checkin(no);
if(res==1)
out.println(no+"room checkin");
else if(b.equals("checkout"))
{
res=obj.checkout(no);
if(res==1)
out.println(no+"room checkout");
if(res==0)
finally
out.close();
OUTPUT:
practical 10
Aim:hibernate program
Guestbook.jsp
<!DOCTYPE html>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="hibernate.Guestbook"%>
<%!
SessionFactory sf;
org.hibernate.Session ss;
List<hibernate.Guestbook> gbook;
%>
<%
sf=new Configuration().configure().buildSessionFactory();
ss=sf.openSession();
Transaction tx=null;
try
tx=ss.beginTransaction();
String name=request.getParameter("name");
String msg=request.getParameter("msg");
gb.setName(name);
gb.setMsg(msg);
gb.setDt(dt);
ss.save(gb);
tx.commit();
}
catch(Exception e)
{out.println("Error"+e.getMessage());
try
ss.beginTransaction();
gbook=ss.createQuery("from Guestbook").list();
catch(Exception e)
{}
%>
<html>
<head>
<title>Guest view</title>
</head>
<body>
Guest View
<br><br>
<%
Iterator it=gbook.iterator();
while(it.hasNext())
Guestbook each=(Guestbook)it.next();
out.print(each.getDt()+" ");
out.print(each.getName()+"<br>");
out.print(each.getMsg()+"<br><hr>");
%>
</body>
</html>
Index.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</body>
</html>
Guestbook.java
package hibernate;
public Guestbook() {
this.name = name;
this.msg = msg;
this.dt = dt;
return this.no;
}
public void setNo(Integer no) {
this.no = no;
return this.name;
this.name = name;
return this.msg;
this.msg = msg;
return this.dt;
this.dt = dt;
}}
}
Steps for EJB Application
1) File -> New project -> java web -> web application -> get New web
Application Window -> provide the information in that window as: project
name -> EJBRoomReservetionApp -> browse the path for saving EJB -> next ->
select server - glassfish server -> next -> finish.
2) Click on services -> database -> create database ->db -> right click on db
-> create table -> room -> columns-> total - int primary key -> add
column->occ - int - deselect NULL -> ok.
3) Click on project -> right click on library -> add mysql connector jar
file.
4) Right click on EJBRoomReservetionApp -> new -> other -> Enterprise java
bean -> session bean -> next ->you will get new session bean window.
Provide the information to that window: EJBName -> RoomBean -> package ->
ejb, session type -> stateless, Create interface -> Local -> Finish. You
will get two files i.e. RoomBeanLocal.java and RoomBean.java.
5) Right click on EJBRoomReservetionApp -> new -> servlet. You will get
new servlet window. Give class name -> roomclient, package -> servlet-
> next-> finish.