AJ Practical File
AJ Practical File
AJ Practical File
Advanced Java
Practical File
Submitted by - Submitted to –
Ms. Urvashi
Asst. Prof. (CSE)
Sr. Experiment Name Date Signature
No
1 Write a program for JDBC connection.
2 Write a program to implement JSP.
3 Write a program to insert data into table using JSP
4 Write a Program to Show Validation of User Using
Servlet
5 Write a Program of calling one servlet by another
servlet.
6 Write a Program to design simple Login page using
Struts.
7 Write a Program to implement Struts.
8 Write a Program to implement Hibernation
9 Develop a Hibernate application to store Feedback of
Website Visitor in mysql Database.
PROGRAM NO: 1
Aim : JDBC Connection Demo.java
Import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import
java.sql.SQLException;
import java.sql.Statement;
// INSERT A RECORD
stmt.executeUpdate("INSERT INTO EXPTABLE (NAME) VALUES (\"TINU K\")");
System.out.println("ID \tNAME");
System.out.println("============"); while(rs.next())
{
System.out.print(rs.getInt("id") + ".\t" + rs.getString("name"));
System.out.println();
}
if (conn != null)
{
conn.close();
conn = null;
}
}
catch (SQLException sqle)
{
System.out.println("SQL Exception thrown: " + sqle);
}
}
} //JDBC_Connection_Demo ends here
OUTPUT:
------
ID NAME
============
1. ANUSHKA K
2. GARVITA K
3. TINU
PROGRAM NO: 2
Step 3) Start the Tomcat Service Runner in “C:\Program Files\Apache Software Foundation\Tomcat
6.0\bin”
Step 4) Open the web browser and type the following URL to run the servlet from any client system:
http://localhost:8080/examples/time.jsp
or
} catch(SQLExceptione)
{
e.printStackTrace();
} catch(Exceptioni)
{
System.out.println(i);
}
}
}
OUTPUT:
PROGRAM NO: 4
RequestServlet.java
import
javax.servlet.http.*;
import javax.servlet.*;
import java.io.*; import
java.sql.*;
public class RequestServlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse
res)throws ServletException,lOException {
res.setContentType("textƒhtml"); PrintWriter
out=res.getWriter();
out.println("<html><body>");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(name.equals("ABCDE")&& pass.equals("123456"))
out.println("user is valid through service method");
else out.println("user is invalid through service method");
out.println("<ƒbody><ƒhtml>"); }
Login.html
<html>
<body>
<form action="run3" method="get"> enter your name:
<input type="text" name="name"ƒ>
<br> enter your password:
<input type="password" name="pass"ƒ>
<br>
<input type="submit"ƒ>
<ƒbody>
<ƒhtml>
Web.xml
<web−app>
<servlet>
<servlet−name>RequestServlet<ƒservlet−name>
<servlet−class>RequestServlet<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>RequestServlet<ƒservlet−name>
<url−pattern>ƒrun3<ƒurl−pattern>
<ƒservlet−mapping>
<ƒweb−app>
OUTPUT :
PROGRAM NO: 5
RequestDispatcher2.java
import javax.servlet.http.*;
import javax.servlet.*; import
java.io.*;
public class RequestDispatcher2 extends HttpServlet
{ public void doGet(HttpServletRequest req, HttpServletResponse res)throws
ServletException,lOException {
res.setContentType("textƒhtml");
PrintWriter out=res.getWriter();
out.println("<html><body>"); String
s="before dispatcher";
out.println(s);
try
{
RequestDispatcher rd=req.getRequestDispatcher("ƒrun2");
rd.include(req,res);
}
catch(Exception e)
{ out.println("exception");
}
out.println("after dispatcher");
out.println("<ƒbody><ƒhtml>"); }
}
DemoServlet2.java
import javax.servlet.http.*;
import javax.servlet.*; import
java.io.*;
public class DemoServlet2 extends GenericServlet
{ public void service(ServletRequest req, ServletResponse res)throws
ServletException,lOException {
res.setContentType("textƒhtml"); PrintWriter
out=res.getWriter();
out.println("<html><body>");
out.println("hello servlet with GenericServlet");
out.println("<ƒbody><ƒhtml>"); }
}
Web.xml
<web−app>
<servlet>
<servlet−name>RequestDispatcher2<ƒservlet−name>
<servlet−class>RequestDispatcher2<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>RequestDispatcher2<ƒservlet−name>
<url−pattern>ƒrun7<ƒurl−pattern>
<ƒservlet−mapping>
<servlet>
<servlet−name>DemoServlet2<ƒservlet−name>
<servlet−class>DemoServlet2<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>DemoServlet2<ƒservlet−name>
<url−pattern>ƒrun2<ƒurl−pattern>
<ƒservlet−mapping>
<ƒweb−app>
OUTPUT:
PROGRAM NO: 6
AIM: Program to design simple Login page using Struts.
Login.jsp
User.java
package net.codejava.struts;
LoginAction.java
package net.codejava.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(
value = "login", results = {
@Result(name = "success", location = "/WEB-INF/jsp/Login.jsp")
}
)
public class LoginAction extends ActionSupport {
// empty
}
DoLoginAction.java
package net.codejava.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(
value = "doLogin", results = {
@Result(name = "input", location = "/WEB-INF/jsp/Login.jsp"),
@Result(name = "success", location = "/WEB-INF/jsp/Welcome.jsp")
}
)
public class DoLoginAction extends ActionSupport {
private User user;
Welcome.jsp
OUTPUT:
PROGRAM NO: 7
AIM: Write a Program to implement Struts.
input.jsp
<%@taglibprefix="s"uri="/struts-tags"%>
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<met ahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Struts2 beginner example application</title>
</head>
<body>
<center>
<h2>Calculate sum of two numbers</h2>
<s:form action="calculateSumAction"method="post">
<s:textfield name="x"size="10"label="Enter X"/>
<s:textfield name="y"size="10"label="Enter Y"/>
<s:submit value="Calculate"/>
</s:form>
</center>
</body>
</html>
Package net.codejava.struts;
Import com.opensymphony.xwork2.ActionSupport;
Result.jsp
<%@taglibprefix="s"uri="/struts-tags"%>
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Sum Result</title>
</head>
<body>
Sum of <s:property value="x"/>
and <s:property value="y"/> is:
<s:property value="sum"/>
</body>
</html>
PROGRAM NO: 8
package com.mycompany;
// deletes an object
Contact contact7 = new Contact();
contact7.setId(7); session.delete(contact7);
// deletes a loaded instance of an object
Contact contact8 = (Contact) session.load(Contact.class, new Integer(8));
session.delete(contact8);
}
}
OUTPUT:
PROGRAM NO: 9
AIM : Develop a Hibernate application to store Feedback of Website Visitor in MySQL
Database.
create
database
feedback
db; create
table
GuestBook(
vno int PRIMARY KEY AUTO_INCREMENT,
vname varchar(5 0), msg varchar(1 00), mdate
varchar(5 0) )
GuestBookBean.java package
mypack; import
javax.persistence.*;
@Entity
@Table(name="gues tbook") public class
GuestBookBean implements
java.io.Serializable { @Id
@GeneratedValue
@Column(name="
vno") private Integer
visitorNo;
@Column(name="
vname") private String
visitorName;
@Column(name="
msg") private
String msg;
@Column(name="
mdate") private String
msgDate; public
GuestBookBean() {
}
public Integer getVisitorNo() { return
visitorNo; } public String getVisitorName()
{ return
visitorName; } public String getMsg() { return msg; }
public String getMsgDate() { return msgDate; }
public void setVisitorNo(Integer vn) { visitorNo = vn ; }
public void setVisitorName(String vn) {
visitorName =
vn; } public void setMsg(String m) { msg = m;
}
public void setMsgDate(String md) { msgDate=md; }
}
Source packages →new → others→select category Hibernate →Hibernate
Configuration
Wizard
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/feedbackdb?zeroDateTimeBeha
vior=co nvertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
--------------------------------
index.html---------------------------------------------------
<h1>Website Feedback Form for google.con </h1>
<form action="fb.jsp" >
package MyJdbc;
import java.sql.*;
import java.util.ArrayList; import java.util.Scanner;
u=s.nextLine();
UserDetails user=new UserDetails(n,a,u,cn); db.insertIntoTable(user);
break; case 2:
System.out.println("Name " +
" Contact no " + "Address " + "University Name");
ArrayList<UserDetails>arr=db.read();
if(arr.isEmpty()) System.out.println("Nothing to show."); else for
(UserDetailsur:arr
){
System.out.println(ur.name+" " + ur.contactno+" " + ur.address+" " +
ur.universityName+" ");
} break; case
3 : db.update();
break; case 4 :
System.out.print("Enter contact no. : "); s.nextLine();
String no=s.nextLine(); db.delete(no); break; default:
System.out.println("Thank you");
} }}
INSERTION
READ
UPDATING
BEFORE UDATING:
AFTER UPDATING
DELETING
AFTER DELETING
PROGRAM NO. 11
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form action="Calculator"
method="GET">
Enter number1:
<input type="text"
name="n1"/><br><br>
Enter number2:
<input type="text" name="n2"/><br><br>
Operator
<select name="op">
<option value="Addition">Addition</option>
<option value="Subtraction">Subtraction</option>
<option value="multiplication">multiplication</option>
<option value="division">division</option>
</select>
<input type="submit" value="calculate" />
</form>
</body>
</html>
Calculator.java file
import java.io.*; import javax.servlet.*; import
javax.servlet.http.*; public class Calculator extends
HttpServlet { public void doGet(HttpServletRequest
request, HttpServletResponse response) throws
ServletException, IOException
{ response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n1 = request.getParameter("n1");
String n2 = request.getParameter("n2");
String op =
request.getParameter("op");
switch (op) { case
"Addition":
out.println("Answer = "+(Integer.parseInt(n1)
+ Integer.parseInt(n2))); break; case
"Subtraction":
out.println("Answer = "+(Integer.parseInt(n1)
- Integer.parseInt(n2))); break; case
"multiplication":
out.println("Answer = "+(Integer.parseInt(n1)
* Integer.parseInt(n2))); break;
default:
out.println("Answer = "+(Integer.parseInt(n1)
/ Integer.parseInt(n2))); break;
}
}
}
OUTPUT:
PROGRAM NO. 12
Aim : Create a registration servlet in Java using JDBC. Accept the details such
as
Username, Password, Email, and Country from the user using HTML Form
and store the registration details in the database.
Index . Html
<!DOCTYPE html>
<html>
<head>
<title>Practical 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
</head>
<body>
<h1>Practical 3.A</h1> <br>
<form action="addData" method="post">
Username : <input type='text' name='username'> <br><br>
Password: <input type='password' name='psd'> <br><br>
Email : <input type='email' name='email'> <br><br>
Country : <input type='text' name='country'> <br><br>
</form>
</body> </html>
addData.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","")
;
out.print("<h1>Connected</h1>");
String query = "insert into userdetails values(?,?,?,?)";
PreparedStatement ps = con.prepareStatement(query);
}
catch(Exception e){ out.println(e);
} }
OUTPUT:
PROGRAM NO. 13
Aim: Design a JDBC application using Servlet/JSP to demonstrate the concept of
ResultSetMetaData.
Resultset.java
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "");
String query = "select * from userdetails";
Statement st = con.createStatement();
rs = st.executeQuery(query);
} catch (Exception e) {
}
}
}
OUPTPUT:
PROGRAM NO. 14
Aim : Develop a simple JSP application to display values obtained from the use of
intrinsic objects[Implicit Objects] of various types.
<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="result.jsp" >
Output :
// response
Response.jsp
<%
response.sendRedirect("https://www.google.com/");
%>
</body>
</html>
PROGRAM NO. 15
Aim : Develop a simple JSP application to pass values from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, genderradio button)
formresult.html
<!DOCTYPE html>
<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="result.jsp" >
OUTPUT: