0% found this document useful (0 votes)
9 views36 pages

Ad Java

Uploaded by

mamalal346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

Ad Java

Uploaded by

mamalal346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Table of Contents

[Link]. Title of Experiment Sign

1 Print current date & time - Servlet application

Write a servlet application to establish communication between html and


2 servlet page using hyperlink.

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

Create the Persistent class


5

A simple servlet that just generates plain text


6

A Program which displays cookie id


7

A program to generates plain text using Java Beans


8

A Program on Stock Market


9

10 Print current date & time - Servlet application


Experiment
1
Number
Experiment Title Print current date & time - Servlet application
Course Outcome
CO1. Learn the Internet Programming, using Java Applets.
(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
The most important advantage of using Servlet is that we can
use all the methods available in core java. The Date class is
Experiment available in [Link] package. Below program shows how to
Description print the current date and time. We can use simple Date object
with to String() to print current date and time.

[Link]
import [Link].*;
import [Link].*;

public class DateSrv extends GenericServlet


{
//implement service()
public void service(ServletRequest req, ServletResponse res) throws
IOException, ServletException
{
//set response content type
Experiment [Link]("text/html");
Code/Procedure //get stream obj
PrintWriter pw = [Link]();
//write req processing logic
[Link] date = new [Link]();
[Link]("<h2>"+"Current Date & Time: "
+[Link]()+"</h2>");
//close stream object
[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.

First create the userlist table

Create table userlist(name varchar2(20), pass varchar2(20));

Experiment There are five files in the below program.


Description
i. [Link]
ii. [Link]
iii. [Link]
iv. [Link]
v. [Link]

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];

public class FirstServlet extends HttpServlet


{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html");
PrintWriter out = [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];

public class WelcomeServlet extends HttpServlet


{
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
[Link]("text/html");
PrintWriter out = [Link]();
String n=[Link]("username");
[Link]("Welcome "+n);
[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:

i. Enter username or password

ii. Correct username and password


1)What are local variables? Local variables are those which are declared within a
block of code like methods. Local variables should be initialized 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
4
Number
Write a servlet program that accepts the Mobile phone
details from user and displays the details on the next
Experiment Title page.

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].*;

public class MobileDetails extends HttpServlet


{
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
[Link]("text/html");
PrintWriter out = [Link]();
String model = [Link]("model");
int price =
[Link]([Link]("price"));
String company = [Link]("company");
String color = [Link]("color");

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>");

//create the statement object


Statement stmt = [Link]();
String sql;
sql = "SELECT * FROM MobileDetails";
ResultSet rs = [Link](sql);
[Link]("<table border=1 >");
[Link]("<caption><h2>Mobile
Details</h2></caption>");

[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

ii. Display the record


1)What are local variables? Local variables are those which are declared within a
block of code like methods. Local variables should be initialized 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
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 .

2) Add jar files for hibernate


Experiment To add the jar files Right click on your project - Build path - Add external
Description archives. Now select all the jar files as shown in the image given below then click
open

Experiment 3) Create the Persistent class


Code/Procedure
[Link]
1. package [Link];
2.
3. public class Employee {
4. private int id;
5. private String firstName,lastName;
6.
7. public int getId() {
8. return id;
9. }
[Link] void setId(int id) {
11. [Link] = id;
12.}
13. public String getFirstName() {
14. return firstName;
15. }
[Link] void setFirstName(String firstName) {
17. [Link] = firstName;
18.}
19. public String getLastName() {
20. return lastName;
21. }
[Link] void setLastName(String lastName) {
23. [Link] = lastName;
24.}
25. }

4) Create the mapping file for Persistent class


Here, we are creating the same mapping file as created in
the previous topic. To create the mapping file, Right click
on src - new - file - specify the file name (e.g.
[Link]) - ok. It must be outside the package.

[Link]

1. <?xml version='1.0' encoding='UTF-8'?>


2. <!DOCTYPE hibernate-mapping PUBLIC
3. "-//Hibernate/Hibernate Mapping DTD 5.3//EN"
4. "[Link]
[Link]">
5.
6. <hibernate-mapping>
7. <class name="[Link]
e" table="emp1000">
8. <id name="id">
9. <generator class="assigned"></generator>
10. </id>
11.
12. <property name="firstName"></property>
13. <property name="lastName"></property>
14.
15. </class>
16.
17. </hibernate-mapping>

5) Create the Configuration file


The configuration file contains all the informations for the
database such as connection_url, driver_class, username,
password etc. The [Link] property is used to create
the table in the database automatically. We will have in-
depth learning about Dialect class in next topics. To create
the configuration file, right click on src - new - file. Now
specify the configuration file name e.g. [Link].

[Link]

1. <?xml version='1.0' encoding='UTF-8'?>


2. <!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 5.3//
EN"
4. "[Link]
[Link]">
5.
6. <hibernate-configuration>
7.
8. <session-factory>
9. <property name="[Link]">update</
property>
10. <property name="dialect">[Link]
cle9Dialect</property>
11. <property name="[Link]">jdbc:or
acle:thin:@localhost:1521:xe</property>
12. <property name="[Link]">system</
property>
13. <property name="[Link]">
oracle</property>
14. <property name="connection.driver_class">[Link]
[Link]</property>
15. <mapping resource="[Link]"/>
16. </session-factory>
17.
18.</hibernate-configuration>

6) Create the class that retrieves or stores the


persistent object
In this class, we are simply storing the employee object to
the database.

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. }

7) Run the application


Before running the application, determine that directory structure is like

To run the hibernate application, right click on the StoreData class


Application.

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
}

Then update the main() method:


1
public static void main(String[] args) {

2 BookManager manager = new BookManager();

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:

Using MySQL Workbench or MySQL Command Line Client to check data,


you will see a new record is inserted to the book table:

Similarly, repeat the same steps for other operations describe below.

Performing READ operation:


The [Link](Class, long)returns an object of the specified class
that maps a row in the database table. Update the read() method of
the BookManager class with the following code:

1
protected void read() {

2 Session session = [Link]();

3
4 long bookId = 20;

5 Book book = [Link]([Link], bookId);

6
7 [Link]("Title: " + [Link]());

8 [Link]("Author: " + [Link]());

9 [Link]("Price: " + [Link]());

1
0
[Link]();
1
}
1
1
2

Performing UPDATE operation:


Call the [Link](Object)method to update a mapped object to the
database. Add the following code to the update() method of
the BookManager class:
1
protected void update() {

2 Book book = new Book();

3 [Link](20);

4 [Link]("Ultimate Java Programming");

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.* ;

public class HelloWorld extends HttpServlet {


public void doGet(
HttpServlet
Request
request,
HttpServlet
Response
Experiment
response)
Code/Procedure throws Servlet
Exception, I
OException { Print
Writer out =
response. get
Writer(); out.
println(" Hello
World");
}
}

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

im port j ava. io.* ;


im port j avax. servlet.* ;
im port j avax. servlet. http.* ;

public class CookieExam ple extends HttpServlet {

public void doGet( HttpServlet Request request, HttpServlet Response


response)
throws I OException, Servlet Exception
{
response. set Content Type(" text/ htm l" ) ; Print Writer out = response.
get Writer();

/ / print out cookies


Experiment
Cookie[ ] cookies = request. get Cookies(); for ( int i = 0; i < cookies.
Code/Procedure length; i+ + ) {
Cookie c = cookies[ i] ;
String nam e = c. get Nam e(); String value = c. get Value();
out. println( nam e + " = " + value);
}

/ / 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

im port j ava. awt. Color;


im port j ava. beans. XMLDecoder; im port j avax. swing. JLabel;
im port j ava. io. Serializable;
public class Sim pleBean extends JLabel im plem ents Serializable
{
public Sim pleBean()
Experiment {
set Text( " Hello world!" ) ; set Opaque( t rue ) ;
Code/Procedure
set Background( Color. RED ) ;
set Foreground( Color. YELLOW ) ; set VerticalAlignm ent( CENTER ) ;
set HorizontalAlignm ent( CENTER ) ;
}

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 interface Stock Market extends j ava. rm i. Rem ote {


float get_price( String sym bol ) throws Rem oteException;
}

2 . I m plem ent your Java/ RMI Server

package Sim pleStocks; im port j ava. rm i.* ;


im port j ava. rm i. server. Unicast Rem oteObject;

public class Stock Market I m pl


extends Unicast Rem oteObject im plem ents Stock Market {

public Stock Market I m pl( String nam e ) throws Rem oteException { t ry


{
Nam ing. rebind( nam e, this ) ;
}
catch( Exception e ) {
System . out. println( e ) ;
}
}

public float get_price( String sym bol ) { float price = 0;


for( int i = 0; i < sym bol. length(); i+ + ) { price + = ( int) sym bol. char
At( i ) ;
}
price / = 5;
return price;
}

3 I m plem ent an Application that creates your Server


im port j ava. rm i.* ;
im port j ava. rm i. server. Unicast Rem oteObject; im port Sim pleStocks.*
;

public class Stock Market Server {

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" )
;

System . out. println( " RMI Stock Market Server ready..." ) ;


}
}

4 . Develop your security policy file.

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.

The best example of a dynamic web page we always see is captcha.


The main difference between static and dynamic web pages is that static web page remains the
same for all clients or users while dynamic web page changes itself according to the time and as per
the user’s request.

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.

What is a web server?

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

Servlets work on server side extensions to handle the complex problems.

Servlets covers all the limitations of CGI.

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

 A client sends the request to a web server.


 The web server receives the request from client.
 Servlets receives the request.
 Servlets process the request and produce the output.
 Servlet sends the output to the web server.
 A web server sends it to the client’s browser and browser display it on the client’s screen.

There are two packages by which servlets can build

[Link](Basic)

[Link](Advance)

Advantages of Servlets

 They are platform independent.


 They are cheaper than CGI.
 They are capable to handle cookies.
 They overcome the limitations of CGI.
 NO need to create new process for any request.
 As it is server-side application, it can inherit the security from a web server.

What is servlet container

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.

Rem ote Method I nvocation ( RMI )

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.

You might also like