Professional Documents
Culture Documents
1. Write a JAVA Servlet Program to implement a dynamic HTML using Servlet (user
name and Password should be accepted using HTML and displayed using a Servlet).
<html><body>
<center>
<form action="LoginServlet" method="POST">
<b> Login form </b><br><br>
Username: <input type="text" name="uname" value="" /><br><br>
Password: <input type="password" name="pass" value="" /><br><br>
<input type="submit" value="submit" />
</form>
</center>
</body></html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
1
Advanced Java Programming Laboratory (13MCA47)
2. Write a JAVA Servlet Program to Auto Web Page Refresh (Consider a webpage which
is displaying Date and time or stock market status. For all such type of pages, you would
need to refresh your web page regularly; Java Servlet makes this job easy by providing
refresh automatically after a given interval).
index.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>
<p>JAVA Servlet Program to Auto Web Page Refresh</p>
<p>Click on the following link for Auto Web Page Refresh:</p>
<!-- this will redirect to ServletApplication as URL-pattern for ServletApplication was
mentioned as /home -->
<a href="home"><b>More...</b></a> <hr />
</body>
</html>
TestServlet.java
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
2
Advanced Java Programming Laboratory (13MCA47)
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
3
Advanced Java Programming Laboratory (13MCA47)
3. Write a JAVA Servlet Program to implement and demonstrate get() and Post
methods(Using HTTP Servlet Class).
index.html
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
index.html
<html>
<head>
<title>Cookies</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form id="form1" action="SetCookieServlet" method="post">
<table>
<tr>
<td>Name</td><td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Email</td><td><input type="email" name="uemail"/></td>
</tr>
<tr>
<td>Password</td><td><input type="password" name="upass"/></td>
</tr>
</table>
<input type="submit" value="Set Cookie"/>
</form>
<br/>
<form id="form2" action="FetchCookieServlet" method="post">
<input type="submit" value="Fetch Cookie"/>
</form>
</center>
</body>
</html>
SetCookieServlet.java
package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
5
Advanced Java Programming Laboratory (13MCA47)
cookie_name.setMaxAge(60*60*24);
cookie_email.setMaxAge(60*60*24);
cookie_pass.setMaxAge(60*60*24);
response.addCookie(cookie_name);
response.addCookie(cookie_email);
response.addCookie(cookie_pass);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML><html><head><title>Set Cookie</title></head>"+
"<body><center>"+
"<h1>Cookie has been set successfully!</h1><br/>"+
"<a href='index.html'>Click here to go back to previous page</a>"+
"</body></html>");
}
}
FetchCookieServlet
package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
cookies = request.getCookies();
6
Advanced Java Programming Laboratory (13MCA47)
response.setContentType("text/html");
out = response.getWriter();
out.println("<!DOCTYPE HTML><html><head><title>Fetch Cookie</title> </head> <body>
<center>");
if( cookies != null )
{
out.println("<h1>Welcome "+searchCookie("uname")+"</h1><br/>"+
"Your Email ID: "+searchCookie("uemail")+"<br/>"+
"Your Password: "+searchCookie("upass"));
}
else
{
out.println("<h1>No Cookies found!</h1>");
}
out.println("<br/><a href='index.html'>Click here to go back to previous
page</a></center></body></html>");
}
}
7
Advanced Java Programming Laboratory (13MCA47)
5. a. Write a JAVA JSP Program to implement verification of a particular user login and
display a Welcome page.
login.jsp
<html>
<head> <title> Login page </title>
</head>
<body>
<form action="validation.jsp">
<table border="0">
<tr>
<td> USER ID: </td>
<td>
<input type="text" name="uname" /> <br>
</td>
</tr>
<tr>
<td> PASSWORD: </td>
<td>
<input type="password" name="password" /> <br>
</td>
</tr>
<tr> <td align ="center">
<input type="submit" value="submit" >
<input type="reset" value="reset">
</td>
</tr>
</form>
</body>
</html>
validation.jsp
<html>
<body>
<%! String uid="student"; %>
<%! String pass="aitmca"; %>
<%! String id, password; %>
<% id=request.getParameter("uname"); %>
<% password=request.getParameter("password"); %>
<% if(uid.equals(id)&&pass.equals(password))
{
%>
<jsp:forward page="welcome.jsp"/>
<%
}
else
{
8
Advanced Java Programming Laboratory (13MCA47)
%>
<jsp:forward page="error.jsp" />
<%
}
%>
</body>
</html>
welcome.jsp
<html>
<body bgcolor="pink">
<center>
<%! String id; %>
<% id=request.getParameter("uname"); %>
<h1> welcome <%=id%> to the home page </h1>
</center>
</body>
</html>
error.jsp
<html>
<head> <title>JSP Page</title> </head>
<body bgcolor="pink">
<h1>INVALID ENTRY</h1>
</body>
</html>
lab5b.jsp
9
Advanced Java Programming Laboratory (13MCA47)
6. Write a JAVA JSP Program which uses jsp:include and jsp:forward action to display a
Webpage.
header.jsp
<%@ page import="java.util.Date" %>
<html>
<head>
<title> </title>
</head>
<body>
<h1>
My website. Time:
<% Date d=new Date();%>
<%=d%>
</h1>
</body>
</html>
index.jsp
<html>
<head>
<title>Index Page</title>
</head>
<body>
<center>
<jsp:include page="header.jsp"/>
<br />
<h1>
Enter Data
</h1>
<br />
<form method="post" action="val.jsp">
Username:
<input type="text" name="uname"/>
<br/> password:
<input type="password" name="upass"/>
<br/>
<input type="submit" value="login">
</form>
</center>
</body>
</html>
10
Advanced Java Programming Laboratory (13MCA47)
val.jsp
welcome.jsp
<!DOCTYPE html>
<html>
<head>
<title>welcome page</title>
</head>
<body bgcolor="pink">
<center>
<jsp:include page="header.jsp"/><br/>
<h1>Welcome Mr./Ms. <%=request.getParameter("uname") %></h1>
</center>
</body>
</html>
11
Advanced Java Programming Laboratory (13MCA47)
7. Write a JAVA JSP Program which uses <jsp:plugin> tag to run a applet.
index.jsp
p7.java
import java.awt.*;
import java.applet.*;
public class Myapplet extends Applet
{
public void init()
{
setBackground(Color.blue);
}
public void paint(Graphics g)
{
int y=50,x=50;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if((i+j)%2!=0)
{
g.setColor(Color.black);
}
else
{
g.setColor(Color.white);
}
g.fillRect(x,y,50,50);
x=x+50;
}
y=y+50;
x=50;
}
}
}
12
Advanced Java Programming Laboratory (13MCA47)
8. Write a JAVA JSP Program to get student information through a HTML and create a
JAVA Bean class, populate Bean and display the same information through another JSP.
index.jsp
<html>
<head>
</head>
<body bgcolor="pink">
<pre>
</pre>
<form>
</body>
</html>
StudInfo.jsp
<html>
<head>
<title>
Register User
</title>
</head>
<body>
<jsp:useBean id="stud" scope="session" class="com.Student">
<jsp:setProperty name="stud" property="*" />
</jsp:useBean>
13
Advanced Java Programming Laboratory (13MCA47)
</body>
</html>
Student.java
package com;
return firstName;
14
Advanced Java Programming Laboratory (13MCA47)
return surname ;
return course;
return age;
return sem;
return address;
this.firstName =newFirstName;
this.surname =newSurname;
this.usn =newUsn;
15
Advanced Java Programming Laboratory (13MCA47)
this.course =newCourse;
this.sem = newSem;
this.age =newAge;
}
public void setAddress(String newAddress )
{
this.address =newAddress;
}
}
16
Advanced Java Programming Laboratory (13MCA47)
9. Write a JAVA Program to insert data into Student DATA BASE and retrieve info based
on particular queries(For example update, delete, search etc…).
Student.java
package student;
import java.io.*;
import java.sql.*;
17
Advanced Java Programming Laboratory (13MCA47)
catch(SQLException sqle){System.err.println(sqle);}
break;
case 2:
try {
PreparedStatement pst ;
System.out.println("Enter your query");
String q2 = br.readLine();
pst=Database.prepareStatement(q2);
pst.execute();
System.out.println("Record inserted Successfully.....");
pst.close();
}
catch(SQLException e){System.out.println(e);}
break;
case 3:
try
{
System.out.println("\nEnter the query to be executed\n");
String str=br.readLine();
DataRequest=Database.createStatement();
Results2=DataRequest.executeQuery(str);
DisplayResults(Results2);
DataRequest.close();
}
catch(Exception e){System.err.println(e);}
break;
case 4: System.exit(0);
}
}
}
catch(IOException ioe1){System.err.println(ioe1);}
}
private void DisplayResults (ResultSet Results2) throws SQLException
{
ResultSetMetaData rmd=Results2.getMetaData();
int col=rmd.getColumnCount();
int count=1;
boolean b=Results2.next();
if(!b)
{
System.out.println("No Data Found");
}
else
{
do
{ System.out.print("RECORD " +(count++)+" => ");
for(int i=0;i<col;i++)
System.out.print(Results2.getString(i+1)+"\t");
System.out.println();
}while(Results2.next());
18
Advanced Java Programming Laboratory (13MCA47)
}
}
}
Output:
1. Queries like:: create table/view,alter,drop,update and delete
2. Quries like:: Insert
3. Queries like:: Selection/Calculation/Group By/Order By/Join/Conditional Testing Query
4. Exit
19
Advanced Java Programming Laboratory (13MCA47)
20
Advanced Java Programming Laboratory (13MCA47)
10. Write a JSP program to implement all the attributes of page directive tag.
index.jsp
Directive.jsp
21
Advanced Java Programming Laboratory (13MCA47)
error.jsp
22
Advanced Java Programming Laboratory (13MCA47)
11. An EJB application that demonstrates Session Bean (with appropriate business logic).
1. Select file -> new project -> java EE -> Enterprise Application -> next -> give project name
and project location -> next -> select glassFishServer -> Select all EJBModule,
WebApplicationModule and finish.
3. Right click on project-ejb and select new-> session bean -> give EJBName, location,package
and
session type (stateless)& select local interface. It will create java files like projectname.java,
local.java
4. Right click inside projectname.java page -> click insert code -> add business method -> and
define
business methods.
method : add
returnType:int
param1:int -a
6. Create a servlet by right clicking the war file and select -> new servlet, give name and location
-> next -> accept the servletname and URL pattern(s) fields and click finish.
7. Right click on the content of servlet session and select insert code then select callEnterprise
Bean -
9. Set the relative URL of the project -> right click the project folder -> select properties -> run -
>
23
Advanced Java Programming Laboratory (13MCA47)
12. An EJB application that demonstrates MDB (with appropriate business logic).
JNDI Name: jms/dest -> Resource Type: javax.jms.Queue minimize the window
File -> NewProject -> Java EE -> EnterpriseApplication -> next -> Java EE Version: select Java
EE 5 ->
Check Create EJB module and Web Application module -> finish
In EJB Module
Right click EJB module -> select new message driven bean -> give EJB Name as MyBean and
package name as com -> Select server destinations as jms/dest
TextMessage tmsg=null;
tmsg=(TextMessage)message;
In war Module
Select index.jsp file and type the following code inside body tag
<form action="NewServlet">
<table border="1">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
24
Advanced Java Programming Laboratory (13MCA47)
<tr>
<td>Enter message</td>
</tr>
</tbody>
</table>
</form>
Right Click on war module -> New -> Servlet -> write servlet name and package name -> finish
In NewServlet before try block of processRequest() method ->right click select insert code -
>Select send JMS message ->Connection factory : jms/queue
String str=request.getParameter("msg");
sendJMSMessageToDest(str); /*Left click on error indicator and select surround statement with
try catch*/
25
Advanced Java Programming Laboratory (13MCA47)
13. An EJB application that demonstrates persistence (with appropriate business logic).
1. File -> New Project -> Java EE -> Enterprise Application -> click next -> Give name for
project (ex: employee -> set Java EE version as Java EE 5 -> Finish.
2. At the end of creation you can see three modules generated.
1. Select services-> Databases ->right click JavaDB ->create database ->right click
jdbc:derby://….. and connect
1. Right click on projects ejb module -> new -> Entity class -> Give class name and
package -> click next -> Select data source with your database -> Finish.
2. Change AUTO to IDENTITY and add the following code
String name;
int salary;
3. Select the variables -> right click over the selection -> insert code -> Getter and Setter…
-> select all -> Generate. You can see getter and setter method generated.
1. Right click on projects ejb module -> new -> Other -> Enterprise Java beans -> Session
bean for entity classes -> click Add all -> click next -> select local.
1. Right click on project war module -> new -> servlet -> Give name for servlet and
package -> next -> finish.
2. Inside the class definition -> Right click -> Insert code -> call enterprise bean -> select
your entity class( ex: EmployeeFacade) -> click ok.
3. Inside processRequest method add the following code,
employee dan=new employee();
dan.setName(request.getParameter("name"));
dan.setSalary(Integer.parseInt(request.getParameter("salary")));
empFacade.create(dan);
4. Left click on the error of first line code -> Add import (for your entity class).
26
Advanced Java Programming Laboratory (13MCA47)
(<form action="EmployeeServlet">
Name: <input type="text" name="name" value="">
<br>
Salary: <input type="text" name="salary" value="">
<br><br>
<input type="submit" value="submit">
</form>)
1. Right click on Application module -> Clean and bulid -> Right click on Application
module -> Deploy.
2. Under services window -> right click on glassfish server -> start the server -> right click
on glassfish server -> view admin console -> Applications -> click on entity bean class
name -> Launch -> click on first link -> Enter data and submit->
3. You can see the data on database by -> right click on your database -> refresh -> expand
your table -> view data.
Note: In the examination each student should do one question out of the above 13 questions
27