Ad Java
Ad Java
Write a Servlet application for login page, which is check the username
3 and password. If username and password are matched, display welcome
message.
Write a servlet program that accepts the Mobile phone details from user
and displays the details on the next page.
4
[Link]
import [Link].*;
import [Link].*;
Input
Output/Result
Sample Viva 1. What is the most important feature of Java?
Java is a platform independent language.
Questions with 2 What do you mean by platform independence?
Answers (at least Platform independence means that we can write and compile the java code in
one platform (eg Windows) and can execute the class in any other supported
platform eg (Linux, Solaris, etc).
3. What is a JVM? JVM is Java Virtual Machine which is a run time environment
for the compiled java class files.
4. Are JVM's platform independent? JVM's are not platform independent. JVM's
5) are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM? JDK is Java Development Kit
which is for development purpose and it includes execution environment also.
But JVM is purely a run time environment and hence you will not be able to
compile your source files using a JVM.
Experiment
2
Number
Experiment Title Write a servlet application to establish communication between html and
servlet page using hyperlink.
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
Experiment [Link]
Code/Procedure
1. <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wishing message</title>
</head>
<body>
<a href =
"[Link]
wishing</a>
</body>
</html>
2. [Link]
3. import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class WishApp extends HttpServlet
{
public void service(HttpServletRequest req,
HttpServletResponse res) throws IOException,
ServletException
{
//set response content type
[Link]("text/html");
//get printWrite obj
PrintWriter pw = [Link]();
//write request processing logic to generate wish
message
Calendar cal = [Link]();
//get current hours of the day
int hour = [Link](Calendar.HOUR_OF_DAY);//24
hrs format
//generate wish message
if(hour<12)
[Link]("Good Morning!!");
else if (hour < 16)
[Link]("Good afternoon");
else if(hour<20)
[Link]("Good evening");
else
[Link]("Good night");
[Link]("<br><br><a href=
'../WishSrvApp/[Link]'>Home</a>");
//close stream object
[Link]();
}
}
4. [Link]
5. <web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>WishApp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Input
Good Morning !!
Output/Result
1)What are local variables? Local varaiables are those which are declared within
a block of code like methods. Local variables should be initialised before
accessing them
2)Can a class declared as private be accessed outside it's package? Not possible.
26. Can a class be declared as protected? A class can't be declared as protected.
only methods can be declared as protected.
Sample Viva 3)What is the access scope of a protected method? A protected method can be
accessed by the classes within the same package or by the subclasses of the class
Questions with
in any package.
Answers (at least 3). What is the purpose of declaring a variable as final? A final variable's value
5) can't be changed. final variables should be initialized before using them.
4) What is the impact of declaring a method as final? A method declared as final
can't be overridden. A sub-class can't have the same method signature with a
different implementation.
5)I don't want my class to be inherited by any other class. What should i do? You
should declared your class as final. But you can't define your class as final, if it is
an abstract class. A class declared as final can't be extended by any other class.
Experiment
3
Number
Experiment Title Write a Servlet application for login page, which is check the
username and password. If username and password are matched,
display welcome message
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
In this example, we match the username and password from the database.
Experiment [Link]
Code/Procedure
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="servlet1" method="post">
<fieldset style="width:20%; background-
color:#ccffcc">
<h2 align="center">Login Page</h2>
<hr>
<table>
<tr><td>Name</td><td><input type="text"
name="username"/></td>
<tr><td>Password</td><td><input
type="password" name="userpass"/></td></tr>
<tr><td></td><td><input type="submit"
value="login"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
String n=[Link]("username");
String p=[Link]("userpass");
if([Link](n, p))
{
RequestDispatcher
rd=[Link]("servlet2");
[Link](request,response);
}
else
{
[Link]("Sorry username or password error");
RequestDispatcher
rd=[Link]("[Link]");
[Link](request,response);
}
[Link]();
}
}
[Link]
import [Link].*;
public class LoginDao
{
public static boolean validate(String name,String pass)
{
boolean status=false;
try
{
[Link]("[Link]");
Connection
con=[Link]("jdbc:oracle:thin:@localho
st:1521:xe","local","test");
PreparedStatement ps=[Link](
"select * from userreg where name=? and
pass=?");
[Link](1,name);
[Link](2,pass);
ResultSet rs=[Link]();
status=[Link]();
}
catch(Exception e)
{
[Link](e);
}
return status;
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]
<web-app>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>
Input
Output/Result Output:
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Answer:
The [Link] page accepts the mobile details and then
Experiment
communicates to the java page. All the records are stored in
Description the database and details displayed on the next page.
Experiment
Code/Procedure [Link]
<!doctype html>
<body>
<form action="servlet/MobileDetails"
method="post">
<fieldset style="width:25%; background-
color:#b3ffff">
<h2 align="center">Mobile Details
form</h2><hr>
<table>
<tr><td>Model Number</td>
<td><input type="text" name="model"
required /></td></tr>
<tr><td>Mobile Price</td>
<td><input type="text" name="price"
required /></td></tr>
<tr><td>Mobile Company</td>
<td><input type="text" name="company"
required /></td></tr>
<tr><td>Mobile Color</td>
<td><input type="text" name="color"
required/></td></tr>
<tr><td><input type="reset"
value="Reset"/></td>
<td><input type="submit"
value="Register"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
try
{
//load the driver
[Link]("[Link]");
//create connection object
Connection
con=[Link]( "jdbc:oracle:thin:@localh
ost:1521:xe","local","test");
//create the prepared statement object
PreparedStatement
ps=[Link]("insert into MobileDetails
values(?,?,?,?)");
[Link](1,model);
[Link](2,price);
[Link](3,company);
[Link](4, color);
int i = [Link]();
if(i>0)
[Link]("<font color='green' size='4'>Record
inserted successfully...</font>");
[Link]("<tr style='background-color:#ffffb3;
color:red'>");
[Link]("<th>Model Id</th>");
[Link]("<th>Price(Rs.)</th>");
[Link]("<th>Company</th>");
[Link]("<th>Color</th>");
[Link]("</tr>");
// Extract data from result set
while([Link]())
{
//Retrieve by column name
String mModel = [Link]("model");
int mPrice = [Link]("price");
String mCompany = [Link]("company");
String mColor = [Link]("color");
//Display values
[Link]("<tr style='background-
color:#b3ffd9;'>");
[Link]("<td>" + mModel + "</td>");
[Link]("<td>" + mPrice + "</td>");
[Link]("<td>" + mCompany + "</td>");
[Link]("<td>" + mColor + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
[Link]("<a href='[Link]'>Home</a>");
// Clean-up environment
[Link]();
[Link]();
[Link]();
}
catch (Exception ex)
{
[Link]();
}
[Link]();
}
}
[Link]
<web-app>
<servlet>
<servlet-name>MobileDetails</servlet-name>
<servlet-class>MobileDetails</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MobileDetails</servlet-name>
<url-pattern>/servlet/MobileDetails</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>
Input
Output/Result i. Insert record
Experiment
5
Number
Experiment Title Create the Persistent class
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
For creating the first hibernate application in Eclipse IDE, we need to follow the
following steps:
1. Create the java project
2. Add jar files for hibernate
3. Create the Persistent class
4. Create the mapping file for Persistent class
5. Create the Configuration file
6. Create the class that retrieves or stores the persistent object
7. Run the application Here, we are creating the same persistent class
which we have created in the previous topic. To create the persistent class, Right
click on src - New - Class - specify the class with package name (e.g.
[Link]) - finish .
1) Create the java project
Create the java project by File Menu - New - project - java project . Now specify
the project name e.g. firsthb then next - finish .
[Link]
[Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link]
ry;
9. import [Link]
egistryBuilder;
10.
11. public class StoreData {
12.
13. public static void main( String[] args )
14. {
15. StandardServiceRegistry ssr = new Stand
ardServiceRegistryBuilder().configure("[Link].
xml").build();
16. Metadata meta = new MetadataSources(ssr).getM
etadataBuilder().build();
17.
18. SessionFactory factory = [Link]
der().build();
19. Session session = [Link]();
20. Transaction t = [Link]();
21.
22. Employee e1=new Employee();
23. [Link](1);
24. [Link]("Gaurav");
25. [Link]("Chawla");
26.
27. [Link](e1);
28. [Link]();
29. [Link]("successfully saved");
30. [Link]();
31. [Link]();
32. }
33. }
Input
Output/Result 1. What is Hibernate in Java and Why do we need it?
Hibernate is an open source Object-Relational Persistence and
Query service for any Java Application. Hibernate maps Java
classes to database tables and from Java data types to SQL
data types and relieves the developer from most common
data persistence related programming tasks
The application uses the Hibernate framework as the
persistence layer to retrieve POJOs (plain old Java objects)
from a relational database. Hibernate is framework that
provides tools for object relational mapping (ORM).
Experiment
6
Number
Experiment Title Performing CRUD Operations with Hibernate
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
Performing CREATE operation:
Call the [Link](Object)method to persist a mapped object to the
database. Update the create() method of the BookManager class as
following:
1
2
3
4
protected void create() {
5 Book book = new Book();
6 [Link]("Effective Java");
7 [Link]("Joshua Bloch");
8 [Link](32.59f);
9
Experiment 1 Session session = [Link]();
Code/Procedure 0
[Link]();
1
1
1 [Link](book);
2
1 [Link]().commit();
3
[Link]();
1
4
}
3 [Link]();
4
5 [Link]();
6
7 [Link]();
8
}
Now run the program again. You should see Hibernate prints a SQL
statement in the Console view like this:
Similarly, repeat the same steps for other operations describe below.
1
protected void read() {
3
4 long bookId = 20;
6
7 [Link]("Title: " + [Link]());
1
0
[Link]();
1
}
1
1
2
3 [Link](20);
5 [Link]("Nam Ha Minh");
6 [Link](19.99f);
7
8 Session session = [Link]();
9 [Link]();
1
0
[Link](book);
1
1
1 [Link]().commit();
2 [Link]();
1
}
3
Performing DELETE operation:
1
Call the [Link](Object)method to remove a mapped object from the data
4
the delete() method of the BookManager class:
1
1
protected void delete() {
5
2 Book book = new Book();
3 [Link](20);
4
5 Session session = [Link]();
6 [Link]();
7
8 [Link](book);
9
1 [Link]().commit();
0
1
1
[Link]();
1
2
}
That’s an awesome Hibernate hello world tutorial using Eclipse IDE and MySQL datab
project under the Attachments section. Happy learning Hibernate!
Input
Output/Result
Experiment
7
Number
Experiment Title A simple servlet that just generates plain text
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
im port j ava. io.* ;
im port j avax. servlet.* ;
im port j avax. servlet. http.* ;
Input
Output/Result
Experiment
8
Number
Experiment Title A Program which displays cookie id
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
/ / set a cookie
String nam e = request. get Param eter(" cookieNam e"); if ( nam e ! = null
&& nam e. length() > 0) {
String value = request. get Param eter(" cookieValue"); Cookie c = new
Cookie( nam e, value);
response. addCookie( c) ;
}
}
}
Input
Output/Result
Experiment
9
Number
Experiment Title A program to generates plain text using Java Beans
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
Input
Output/Result
Experiment
10
Number
Experiment Title A Program on Stock Market
Course Outcome
(CO)
P- I V/ I I I
Hardware PROCESSOR HDD 4 0 GB
Tools/Apparatus RAM 1 2 8 MB or above
Required W indow 9 8 / 2 0 0 0 / ME/ XP, Java Entrprise Edition ,5 SQL
Software Server / MySql ,W eb Server( Apache Tom cat/ JSW DK) ,Net
Beans I DE
Experiment
Description
Experiment 1 . Develop your Rem ote I nterface package Sim pleStocks;
Code/Procedure im port j ava. util.* ; im port j ava. rm i.* ;
public static void m ain( String[ ] args) throws Exception { if( System . get
Security Manager() = = null) {
System . set Security Manager( new RMI Security Manager() ) ;
}
Stock Market I m pl m y Object = new Stock Market I m pl( " NASDAQ" )
;
Grant
{
perm ission j ava. security. AllPerm ission "", "";
};
Input
Output/Result
Viva Question:
Dynamic Web Pages
Dynamic web pages are server-side web pages, each time it is viewed, we see different [Link] is
controlled by Application server processing server-side scripts. The dynamic web pages can also
change their contents on the request of the client. They have the capability to generate new content
according to time and need. Which simply means that dynamic web pages are never the same for all
users.
We all are well aware of the need for dynamic web pages in day to day life.
Servlets
In Java, a servlet is a way to create those dynamic web pages. Servlets are nothing but the java
programs. In Java, a servlet is a type of java class which runs on JVM(java virtual machine) on the
server side. Java servlets works on server [Link] servlets are able to handle large and complex
problems and requests by users.
A web server is used to transfer data in the form of the HTTP protocol. The client just has to type the
URL in a browser and the web server provides her/him the required web page to read.
The web server converts the client typed URL into the HTTP protocol in order to respond to the
request and with the help of Servlets, it serves the client’s request.
Properties of servlets
What is CGI?
CGI (common gateway interface), is an application which is used to produce dynamic contents of
web pages. Common gateway interface can be created by using any programming language like c,c+
+, etc.
Servlets are cheaper than CGI and are capable to handle [Link] java servlet follows a simple
process
Steps
[Link](Basic)
[Link](Advance)
Advantages of Servlets
Users did not have the facility to request and access the static pages but dynamic also, where
dynamic web pages can work differently each time for different inputs and according to the time.
A servlet container is nothing but a concept or idea to use them. Servlet container is a part of
web server that can easily communicate with the java servlets.
There are three essential methods that can be invoked by the client according to the need:-
Init()
Service()
Destroy()
What is JDBC
Call- level interfaces such as JDBC are program m ing interfaces allowing external
access to SQL database m anipulation and update com m ands. They allow the
integration of SQL calls into a general program m ing environm ent by providing
library routines which interface with the database. I n particular, Java based JDBC has
a r ich collection of routines which m ake such an interface extrem ely sim ple and
intuitive.
what happens in a call level interface: You are writing a norm al Java program .
Som ewhere in the program , you need to interact with a database. Using
standard library routines, you open a connection to the database. You then use JDBC to
send your SQL code to the database, and process the results that are
returned. When you are done, you close the connection.
Remote Method Invocation ( RMI ) is the object equivalent of Remote Procedure Calls (
RPC). While RPC allows you to call procedures over a network, RMI
invokes an object's methods over a network.
I n the RMI model, the server defines objects that the client can use remotely. The
clients can now invoke methods of this rem ote object as if it were a local object
running in the same virtual machine as the client. RMI hides the
underlying mechanism of transporting method arguments and return values across
the network. I n Java- RMI , an argument or return value can be of any prim it ive Java
type or any other Serializable Java object.