You are on page 1of 79

J2EE Programming

Page 1 of 79

EX. No.: 1 HTML AND SERVLET
12.07.2012

Aim:
To create a web application that could display the player name and his country in
a servlet, to be passed by an html page.
Steps:
1. Create a html file and add the necessary tags to receive the inputs
2. Create a servlet file and customize
3. Map the submit button to the servlet
4. Display the values returned by the html in the servlet page
Source Code:
The source codes are enclosed in 1.1, 1.2 of this context
Result:
The run time screen shots of the web application have been enclosed













J2EE Programming

Page 2 of 79

Activity Diagram



1. Receive Inputs
(name,country)
2. getParameters(), Customize and
Display (name,country)



















PlayerHtml PlayerServlet
J2EE Programming

Page 3 of 79

1.1.Html:PlayerHtml.html
<html>
<head>
<title>Number Display</title>
</head>
<body>
<center><h1><font color="red" size=18><u>
Enter Name and choose Country
</u> </font>
<form action="http://localhost:8084/WebApplication5/PlayerServlet" method="get">
<table border="5">
<tr>
<td>
<center> Name:<Input type="text" size="5" name="name"></center><br><br><br>
Country:
<select name="country">
<option value="India"> India
<option value="China">China
<option value="Srilanka">Srilanka
<option value="WestIndies">WestIndies
</select><br><br><br>
<center> <input type="submit" value="GO!!!!!!!!!!!" ></center>
</td>
</tr>
</frame>
</form>
</center>
</body>
</html>










J2EE Programming

Page 4 of 79

Output: html












J2EE Programming

Page 5 of 79

1.2. Servlet: PlayerServlet.java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PlayerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException
{
String name=request.getParameter("name");
String[] country=request.getParameterValues("country");
int i;
String countree="";
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<h1>");

for(i=0;i<country.length;i++)
{
countree=country[i];
}

out.println(name+" belongs to "+countree+"<br>");
out.println("<a href='PlayerHtml.html'>previous</a>");

J2EE Programming

Page 6 of 79

out.println("</html>");
}
}
























J2EE Programming

Page 7 of 79

Output: using both Html and Servlet











J2EE Programming

Page 8 of 79

EX. No.: 2 XML AND SERVLET
16.07.2012

Aim:
To create a web application that could display the rate of interest during the
respective year in a servlet, to be mapped from a xml page.
Steps:
1. Use web.xml to initialize the parameter name and its value
2. Create a servlet file and customize
3. Use the parameter defined by web.xml in servlet
4. Display values in the servlet page.
Source Code:
The source codes are enclosed in 2.1, 2.2 of this context
Result:
The run time screen shots of the web application have been enclosed











J2EE Programming

Page 9 of 79

Activity Diagram



1. Initialize Parameter
(tax-rate,year)
2. getInitParameter(), Customize and
Display (tax-rate,year)


















Web.xml TaxTextServlet
J2EE Programming

Page 10 of 79

2.1. XML coding (to give inside web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<distributable/>
<servlet>
<servlet-name>TaxTextServlet</servlet-name>
<servlet-class>TaxTextServlet</servlet-class>
<init-param>
<param-name>Year</param-name>
<param-value>2012</param-value>
</init-param>
<init-param>
<param-name>Rate</param-name>
<param-value>12%</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TaxTextServlet</servlet-name>
<url-pattern>/TaxTextServlet</url-pattern>
</servlet-mapping>
</web-app>



J2EE Programming

Page 11 of 79

2.2. Servlet: TaxTextServlet
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TaxTextServlet extends HttpServlet
{
int year;
String rate;

public void init(ServletConfig config) throws ServletException
{
String param1=config.getInitParameter("Year");
String param2=config.getInitParameter("Rate");

year=Integer.parseInt(param1);
rate=param2;
}
protected void service(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException
{ PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<h1>");
out.println("The rate of interest during "+year+" is "+rate+"<br>");
out.println("</html>");
}
}

J2EE Programming

Page 12 of 79

Output:XML and Servlet

















J2EE Programming

Page 13 of 79

EX. No.: 3 HTML AND SERVLET (DATA PROCESSING)
23.07.2012

Aim:
To create a web application, where the source data (marks) are to be passed by an
html page and the same would be processed and displayed (total, grade and result)
in a servlet page.
Steps:
1. Create a html file and add the necessary tags to receive the inputs
2. Create a servlet file and customize
3. Map the submit button to the servlet
4. Process and display the values in the servlet page
Source Code:
The source codes are enclosed in 3.1, 3.2 of this context
Result:
The run time screen shots of the web application have been enclosed












J2EE Programming

Page 14 of 79

Activity Diagram



1. Input(name,
marks (m1,m,m3))
2.getParameters(), Process, Customize
and Display(total, grade, result)





















MarkHtml MarkServlet
J2EE Programming

Page 15 of 79

3.1. Html:MarkHtml.html
<html>
<head>
<title>To display the marklist</title>
</head>
<body>
<form action="http://localhost:8084/WebApplication5/MarkServlet" method="doPost">
<center><br><br><br><br><br>
<h1><u>Simple Marklist Processing</u></h1>
Name :<input type="text" name="text1"><br>
Mark1:<input type="text" name="num1" ><br>
Mark2:<input type="text" name="num2" ><br>
Mark3:<input type="text" name="num3" ><br>

<input type="submit" name="sub" value="Result"><br>
</center>
</form>

</body>
</html>





























J2EE Programming

Page 16 of 79

Output: Html
























J2EE Programming

Page 17 of 79

3.2. Servlet:MarkServlet
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;


public class MarkServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
String name=request.getParameter("text1");
int m1=Integer.parseInt(request.getParameter("num1"));
int m2=Integer.parseInt(request.getParameter("num2"));
int m3=Integer.parseInt(request.getParameter("num3"));
int tot=m1+m2+m3;
float avg=tot/3;
String resu="undeclared";

if(m1<=100 && m2<=100 && m3<=100 && m1>=0 && m2>=0 && m3>=0)
{
pw.println("<h1><u> MarkSheet Generation</u></h1><br><br>");
if(m1>50 && m2>50 && m3>50)
{

pw.println("<h2> Name:"+name);
pw.println("<br><br> <font color='purple'>Languages</font>");
pw.println("<br> Tamil : " +m1);
pw.println("<br> English : "+m2);
pw.println("<br><br> <font color='purple'> Major Course </font><br>"+m3);
pw.println("<br><br> Total :"+tot);
resu="Pass";
pw.println("<br><br> RESULT : "+"<font color='green'>"+resu+"</font></h2>");

}
else
{

pw.println("<h2> Name:"+name);
pw.println("<br><br> <font color='purple'>Languages</font>");
pw.println("<br> Tamil : \t" +m1);
pw.println("<br> English : \t"+m2);
pw.println("<br><br> <font color='purple'>Major Course </font> \t"+m3);
resu="fail";
pw.println("<br><br> RESULT : "+"<font color='red'>"+resu+"</font></h2>");
}
J2EE Programming

Page 18 of 79


}
else
{
pw.println("<table border=1>");
pw.println("<tr><td>");
pw.println("MARKS MUST BE BETWEEN 0 AND 100!!!!!!!!!!!");
pw.println("<tr><td>");
pw.println("<a
href='http://localhost:8084/WebApplication5/MarkHtml.html'>Back to enter</a>");
}

pw.close();
}
}



































J2EE Programming

Page 19 of 79


Output: Html and Servlet Processing























J2EE Programming

Page 20 of 79


EX. No.: 4 A SIMPLE JSP
30.07.2012


Aim:
To create a Java Server Page to display the current days date and a phrase of
increasing font size.
Steps:
1. Create a jsp file and add the necessary tags.
2. Utilize the javas utility to display date
3. Use a loop statement to increase the font size to display the phrase
Source Code:
The source code is enclosed in 4.1of this context
Result:
The run time screen shot of the page has been enclosed












J2EE Programming

Page 21 of 79

Activity Diagram



1. Initialize


2. Display Date and phrase



















MySimpleJsp
J2EE Programming

Page 22 of 79

4.1. JSP: MySimpleJsp.jsp
<%-- simple jsp to display a phrase with increasing font size --%>
<%@ page import="java.io.*"%>

<html>
<head>
<title>TRIAL JSP</title>
</head>
<body bgcolor=pink>
<% out.println(new java.util.Date());%>
<%
int i;

for(i=1;i<=7;i++) {
out.write("<br>");

out.print("<font size="+i+">GOD IS GREAT");
// out.println("</font>");
%>
<%
}
%>
</body>
</html>



J2EE Programming

Page 23 of 79

Output: Simple JSP showing increasing font size




























J2EE Programming

Page 24 of 79

EX. No.: 5 SERVLET, BEAN, JSP
(Data sharing program scope=request)
06.08.2012



Aim:
To create a web application that could display two randoms numbers been
generated in servlet, populated in bean and comparing and finding the smaller one
in JSP, printing the result in JSP
Steps:
1. Create servlet file, use javas utility to generate two random numbers
2. Create a bean (java file) to populate the random numbers
3. Create a jsp file and get the numbers from bean
4. Compare the numbers in jsp and display the smaller.
Source Code:
The source codes are enclosed in 5.1, 5.2, 5.3 of this context
Result:
The run time screen shots of the web application have been enclosed










J2EE Programming

Page 25 of 79

Activity Diagram



1. Generate
Random numbers
2. Populate Random
numbers

3. Display the
smaller among the
numbers





























RandServlet RandBean RandJsp
J2EE Programming

Page 26 of 79

5.1. Servlet:RandNumbServlet.java
package Rand;

import java.util.*;
import java.io.*;
import java.net.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class RandomNumbServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException
{
try{

double x=Math.random();
double y=Math.random();
RandomBean myb=new RandomBean(x,y);
request.setAttribute("rndno",myb);
String add="/WEB-INF/RandNumJsp.jsp";
RequestDispatcher rd=request.getRequestDispatcher(add);
rd.forward(request,response);
}
catch(Exception e)
{
}
}






















J2EE Programming

Page 27 of 79

5.2. Bean: RandBean.java
package Rand;
public class RandomBean
{
double number1,number2;
public RandomBean(double number1,double number2)
{
setNumber1(number1);
setNumber2(number2);

}
public void setNumber1(double number1)
{
this.number1=number1;
}
public double getNumber1()
{
return(this.number1);
}
public void setNumber2(double number2)
{
this.number2=number2;
}
public double getNumber2()
{
return(this.number2);
}
}






















J2EE Programming

Page 28 of 79

5.3 JSP:RandJsp.jsp
<html>
<body>
<h1>Random Number Generator</h1>
<jsp:useBean id="rndno" type="Rand.RandomBean" scope="request" />
These RANDOM numbers are generated and transferred from
Servlet(RandNumbServlet) via bean(RandBean):
<br><br>
Number1 (X) : <jsp:getProperty name="rndno" property="number1"/><br>
Number2 (Y): <jsp:getProperty name="rndno" property="number2"/>
<% try{
double x=rndno.getNumber1();
double y=rndno.getNumber2();
if(y<x)
{ %>
<br> <h1><u>y is smaller
<% } else { %>
<br> x is smaller
<% }
}
catch(Exception e)
{
}
%>

</body>
</html>























J2EE Programming

Page 29 of 79

Output: Servlet, Bean,JSP



















J2EE Programming

Page 30 of 79

EX. No.: 6 HTML, SERVLET, BEAN, JSP
(Data sharing program scope=session)
14.08.2012


Aim:
To create a web application that could receive two names; observe the working of
session scope when shuttling data between applications (servlet, bean, jsp).
Steps:
1. Create html file, add necessary input tags to receive input and submit data to
other application(s)
2. Create a servlet to receive names as input from html, and initialize session
object
3. Create a bean (java file) to populate the names
4. Create a jsp file and get the names from bean
5. Observe by 1. not giving any names as input and submit
2. giving either one of the names and submit
3. give two names and submit
4. delete both the names and submit
The concept of restoring the session would be observed
Source Code:
The source codes are enclosed in 6.1, 6.2, 6.3, 6.4 of this context
Result:
The run time screen shots of the web application have been enclosed








J2EE Programming

Page 31 of 79

Activity Diagram



1. Input(firstName,
secondName)
2. getParameters()
(names),
initialize session

3. Populate names

4.Display
names

5. Names will be restored until new values replace the session
values
















SessHtml SessServlet SessBean SessJsp
J2EE Programming

Page 32 of 79

6.1. Html: SessHtml

<html>
<head><center>
<title>Trial</title>
<font color="blue">
<h1><u>Session Data Sharing through MVC</u></h1></font>
</head>
<body>
<form action="http://localhost:8084/MyApps_pGM1_GOD/SessServlet">
<MARQUEE DIRECTION="UP"> GOD IS GREAT</MARQUEE>
<table border="5">
<tr>
<td>
First Name :<input title="user name" type="text" name="t1" >
</td>
</tr>
<tr>
<td>
Last Name :<input type="text" name="t2" title="Expand Initials" >
</td>
</tr>
<tr>
<td><center>
<input type="SUBMIT" name="t3" VALUE="Observe" title="TO SUBMIT
CLICK HERE" >
</center>
J2EE Programming

Page 33 of 79

</td>
</tr>
</table>
Note: 1. Run the application by simply clicking Observe <font
color="red">without giving names</font><br>
2. Run the application by clicking Observe giving <font color="red">First
name alone</font><br>
3. Run the application by clicking Observe giving <font
color="red">Second name and clear First name</font>
</form>
</body>
</html>















J2EE Programming

Page 34 of 79

Output: Html













J2EE Programming

Page 35 of 79

6.2. Servlet: SessServlet
package Sess;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name1=request.getParameter("t1");
String name2=request.getParameter("t2");
HttpSession hsess=request.getSession();
SessBean sbeen=(SessBean)hsess.getAttribute("NB");
if(sbeen ==null)
{
sbeen=new SessBean();
hsess.setAttribute("NB",sbeen);
}
if((name1!=null) && (!name1.trim().equals("")))
{
sbeen.setName1(name1);
}
if((name2!=null) && (!name2.trim().equals("")))
J2EE Programming

Page 36 of 79

{
sbeen.setName2(name2);
}
String myurl="/WEB-INF/SessJsp.jsp";
RequestDispatcher rd=request.getRequestDispatcher(myurl);
rd.forward(request,response);
}
}



















J2EE Programming

Page 37 of 79

6.3. Bean: SessBean
package Sess;
public class SessBean
{
public String name1="firstname",name2="lastname";
public SessBean()
{
}
public SessBean(String name1, String name2)
{
setName1(name1);
setName2(name2);

}
public void setName1(String name1)
{
this.name1=name1;
}
public void setName2(String name2)
{
this.name2=name2;
}
public String getName1()
{
return(name1);
}
public String getName2()
J2EE Programming

Page 38 of 79

{
return(name2);
}

}






















J2EE Programming

Page 39 of 79

6.4. JSP: SessJsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<jsp:useBean id="NB" type="NXG.SessBean" scope="session" />
<jsp:getProperty name="NB" property="name1" />
<jsp:getProperty name="NB" property="name2" />
</body>
</html>













J2EE Programming

Page 40 of 79

Output: Visuals of session data restore



J2EE Programming

Page 41 of 79








J2EE Programming

Page 42 of 79

EX. No.: 7 HTML, JSP, BEAN
04.09.2012


Aim:
To create a web application that depicts the shopping cart properties, viz., add
item and remove item using html, bean and jsp
Steps:
1. Create html file with the necessary input tags (type= select, tupe=submit)
2. Create a jsp file, include the html file of step 1 and setProperty to bean
3. Create a bean (java file) to populate properties from jsp of step2
4. Observe the jsp of step 2 showing the dynamic update of adding item and
removing item; also total items selected
Source Code:
The source codes are enclosed in 7.1, 7.2, and 7.3 of this context
Result:
The run time screen shots of the web application have been enclosed











J2EE Programming

Page 43 of 79

Activity Diagram




1.choose item
to add/remove

2.include html,
setProperty(item,
add/remove) to bean,
call beans methods
to populate values

3.Populate the
values

4.update the values to Jsp
and display the sme






















TryyyHtml TryJsp TryingBean
J2EE Programming

Page 44 of 79

7.1. Html:TryyyHtml.html


<html>

<head>
<title>carts</title>
</head>

<body bgcolor="white">
<font size = 5 color="#CC0000">

<form type=POST action=TryJsp.jsp>
<BR>
Please Choose item to add or remove:
<br>
Add Item:

<SELECT NAME="item">
<OPTION value="Thirukkural">Thirukkural
<OPTION value="BHAGAVAT GITA">BHAGAVAT GITA
<OPTION value="QURAN">QURAN
<OPTION value="BIBLE">BIBLE
<OPTION value="THIRUVASAGAM">THIRUVASAGAM
<OPTION value="Ponnyin Selvan">Ponnyin Selvan
<OPTION value="Mid Summer's Night Dream">Mid Summer's Night Dream
<OPTION value="Horrid Henry">Horrid Henry
<OPTION value="FAIRY TALES">FAIRY TALES
</SELECT>


<br> <br>
<INPUT TYPE=submit name="submit1" value="add">
<INPUT TYPE=submit name="submit1" value="remove">


</form>

</FONT>
</body>
</html>









J2EE Programming

Page 45 of 79

Output:html
































J2EE Programming

Page 46 of 79

7.2 JSP:TryJsp.jsp

<html>


<jsp:useBean id="tb" scope="session" class="TRYYY.TryingBean" />
<jsp:setProperty name="tb" property="item" />
<jsp:setProperty name="tb" property="submit1" />


<%@page import="java.util.*" %>
<%
tb.processRequest(request);

%>


<FONT size = 5 COLOR="#CC0000">
<br> Items added to the shopping cart:
<OL>
<%

String s;
Vector v=new Vector();
v= tb.getItems();


int si=v.size();

Enumeration e;
e=v.elements();

while(e.hasMoreElements())
{%>

<LI>
<%
out.println(e.nextElement());
}

%>
<hr>
<br>
<%
out.println("total items ="+si);
%>
</OL>

</FONT>

J2EE Programming

Page 47 of 79

<hr>

<%@include file="TryyyHtml.html" %>
</html>














































J2EE Programming

Page 48 of 79

7.3. Bean: TryingBean


package TRYYY;
import javax.servlet.http.*;
import java.util.Vector;
import java.util.Enumeration;

public class TryingBean {

Vector v = new Vector();
String submit1 = null;
String item = null;

String s,rstr;
// int i=1;
private void addItem(String name) {
v.addElement(name);
}
private void removeItem(String name) {

v.removeElement(name);

}

public void setItem(String name) {
item = name;
}
public Vector getItems() {
return v;

}

public void setSubmit1(String s) {
submit1 = s;
}
public String getSubmit1()
{
return submit1;
}


public void processRequest(HttpServletRequest request)
{
if (submit1.equals("add"))
addItem(item);
else if (submit1.equals("remove"))
removeItem(item);
}
}
J2EE Programming

Page 49 of 79

Output: add item




Output: remove item







J2EE Programming

Page 50 of 79

EX. No.: 8 HTML, SERVLET, BEAN, JSP
11.09.2012



Aim:
To create a web application that checks user credentials
Steps:
1. Create html file, add necessary input tags to receive user name and password
2. Create a servlet to receive user name and password from html and check for
validity
3. Create a bean (java file) to populate the user name and password, populate the
values in the created bean only if user name and password are found to be
valid in step 2
4. Display the html defined in step 1 if user name and password do not match
5. Create jsp file to display the authenticated user name by getting the values
from bean of step 3
Source Code:
The source codes are enclosed in 8.1, 8.2, 8.3, and 8.4 of this context
Result:
The run time screen shots of the web application have been enclosed









J2EE Programming

Page 51 of 79

Activity Diagram



1.Input(username,
password)
2. getParameters()
(username,password),
Check for validity

3. Populate values
(only if valid)

3. back to
enter
credentials
(if invalid)

4.Display
user
name
and
greet



















LogHtml LogSer LogBean LogJsp
J2EE Programming

Page 52 of 79

8.1 HTML:LogHtml.html

<html>
<head><center>
<title>Trial</title>
<BLINK>ACTION</BLINK>
<font color="blue">
<h1>Log in form</h1></font>
<script type="text/javascript">
function method1(ob1)
{
if(ob1.value="User Name Here");
ob1.value=" ";
}
function method2(ob2)
{
if(ob2.value="Password Here");
ob2.value="";
}
</script>
</head>
<body>
<form action="http://localhost:8084/WebApplication8/LogServ">
<MARQUEE DIRECTION="UP"> GOD IS GREAT</MARQUEE>
<h3> USER NAME :<input color="red" title="user name" type="text"
name="t1" value="User Name Here" onfocus="method1(this)">
<br>
PASSWORD :<input type="Password" name="t2" title="Password Here"
ONFOCUS="method2(this)">
<br>
<input type="SUBMIT" name="t3" VALUE="LOGIN" title="TO
SUBMIT CLICK HERE" >
<br>
</form>
</body>
</html>














J2EE Programming

Page 53 of 79

Output: Html
























J2EE Programming

Page 54 of 79

8.2. Servlet: LogSer.java

package LogFolder;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class LogServ extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter pw=response.getWriter();
String s1=request.getParameter("t1");
String s2=request.getParameter("t2");
String ans="";
if((s1.trim().equals("GOD") )&& (s2.trim().equals("GOD")))
{
ans="Valid User";
pw.println(ans);
}
else
{
ans="INvalid";
}
if(ans.equals("Valid User"))
{
LogBean tbeen=new LogBean(s1,s2,ans);
request.setAttribute("two",tbeen);
String myurl="/WEB-INF/LogJsf.jsp";
RequestDispatcher rd=request.getRequestDispatcher(myurl);
rd.forward(request,response);
}
else
{
pw.println("<html>");
pw.println("<title>"+"INVLAID USER"+"</title>");
pw.println("<font color=blue>"+"<h1>"+"INVALID USER"+"</h1>"+"</font>");
pw.println(" To try again, click the hypertext below!..."+"<br>");
pw.println("<a href=LogHtml.html>go to enter the user credentials</a>");
pw.println("</html>");
}
}
}



J2EE Programming

Page 55 of 79

8.3. Bean: LogBean.java

package LogFolder;

public class LogBean
{
public String namer,pwd,ans;
public LogBean(String namer, String pwd, String ans)
{
this.namer=namer;

this.pwd=pwd;
this.ans=ans;

}
public String getNamer()
{
return(namer);
}

public String getPwd()
{
return(pwd);
}
public String getAns()
{
return(ans);
}

}




















J2EE Programming

Page 56 of 79

8.4. JSP: LogJsf.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> LOG in Screen</title>

<h1>Greetings!.......</h1>
<jsp:useBean id="two" type="LogFolder.LogBean" scope="request" />
Welcome!.., <font size=4 color=purple><jsp:getProperty name="two" property="namer"
/></font>

<br> You are found to be the <font color=green><jsp:getProperty name="two"
property="ans" /></font></!...
</body>
</html>



































J2EE Programming

Page 57 of 79

Output: Valid User
























J2EE Programming

Page 58 of 79

Output: Invalid User
























J2EE Programming

Page 59 of 79

EX. No.: 9 JSF (FRAMEWORK) in JSPAPPLICATION
18.09.2012 (JSP (uses actionListener, Bean)



Aim:
To create a web application that does the basic mathematical operations and
validate for positive numbers only
Steps:
1. Create jsp file uses JSF framework, add necessary input tags to receive
numbers
2. Create a bean (java file) to populate the numbers, check for validity (both the
numbers should be positive), and to do operations on numbers. If validity is
obeyed step 3; step 4 otherwise
3. Create a jsp to display the processed value along with the operation carried
out in step 2.
4. Create jsp file to throw the exceptional string
5. Give specifications on backing bean and the navigation rule in the configuring
file (face-config.xml)
Source Code:
The source codes are enclosed in 9.1, 9.2, 9.3, and 9.4 of this context
Result:
The run time screen shots of the web application have been enclosed










J2EE Programming

Page 60 of 79

Activity Diagram



1.Input(numbers)
2. getParameters()
(numbers),
Check for validity,
Do operation

3.If Valid, Display
the result
and the operation
supported to arrive
at the result

4.If
invalid,
Display
the
exceptional
string





















TrialJsf Beaner ResultJsp NotSuitableJsp
J2EE Programming

Page 61 of 79

9.1. JSP: TrialJsf.jsp

<html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<body bgcolor="cyan">
<f:view>
<h:form>
<center>
<h1>
<font color="red">
<h:outputText id="op" value="GOD IS GREAT"/></font></h1>
<br>
<br><font color="purple" ><u><h:outputText id="op1" value="JSP of type JSF using
actionListener"/></u></font>
<br>First Number <h:inputText id="userNo1" value="#{Beaner.first}" /> <br>

Second Number <h:inputText id="userNo2" value="#{Beaner.second}" /><br><br>
<br>

<h:commandButton id="opn1" action="#{Beaner.checker}" value="ADD"
actionListener="#{Beaner.beanerAction}"/>
<h:commandButton id="opn2" action="#{Beaner.checker}" value="SUB"
actionListener="#{Beaner.beanerAction}"/>
<h:commandButton id="opn3" action="#{Beaner.checker}" value="MUL"
actionListener="#{Beaner.beanerAction}"/>
<h:commandButton id="opn4" action="#{Beaner.checker}" value="DIV"
actionListener="#{Beaner.beanerAction}"/>
<h:commandButton id="opn5" action="#{Beaner.canceler}" value="Clear"
actionListener="#{Beaner.beanerAction}"/>
</center>
</h:form>
</f:view>
</body>
</html>















J2EE Programming

Page 62 of 79

9.2. Bean: Beaner.java

import javax.faces.event.*;
public class Beaner
{
int first,second;
String oprval;
int value;
String str,cancel;

/*public Beaner()
{
}*/
public void setFirst(int userNo1)
{
this.first=userNo1;
}
public void setSecond(int userNo2)
{
this.second=userNo2;
}
public int getFirst()
{
return this.first;
}
public int getSecond()
{
return this.second;
}

public String checker( )
{
if (this.first>0 && this.second>0)
{

return "ok";

}

else
return "no";

}

public void beanerAction(ActionEvent ae)
{
String id=ae.getComponent().getId();
if(id.equals("opn1"))
{
this.value=this.first+this.second;
J2EE Programming

Page 63 of 79

oprval=" addition done";
}
else if(id.equals("opn2"))
{
this.value=this.first-this.second;
oprval=" subtraction done";
}
else if(id.equals("opn3"))
{
this.value=this.first*this.second;
oprval=" multiplication done";
}
else if(id.equals("opn4"))
{
this.value=this.first/this.second;
oprval=" division done";

}
this.str=new Integer(this.value).toString()+oprval;

}
public String getStr()
{
return this.str;
}
public String canceler()
{
this.first=0;
this.second=0;
return "null";
}
}


















J2EE Programming

Page 64 of 79


9.3. JSP: ResultJsp.jsp
<html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<h:form>

<h1> <font color="red"><h:outputText value="#{Beaner.str}"/></font></h1>
</h:form>
</f:view>
</html>






































J2EE Programming

Page 65 of 79



9.4. JSP: NotSuitableJsp.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Error boss! No Negative numbers please!-------------</h1>
</body>
</html>





































J2EE Programming

Page 66 of 79

9.5. Faces-config.xml (backing bean, navigation rule)
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>Beaner</managed-bean-name>
<managed-bean-class>Beaner</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
<from-view-id>/TrialJsf.jsp</from-view-id>

<navigation-case>
<from-action>#{Beaner.checker}</from-action>
<from-outcome>ok</from-outcome>
<to-view-id>/ResultJsp.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{Beaner.checker}</from-action>
<from-outcome>no</from-outcome>
<to-view-id>/NotSuitable.jsp</to-view-id>
</navigation-case>

<navigation-case>
<from-action>#{Beaner.canceler}</from-action>
<from-outcome>null</from-outcome>
<to-view-id>/TrialJsf.jsp</to-view-id>
</navigation-case>

</navigation-rule>

</faces-config>














J2EE Programming

Page 67 of 79


Output: Initially




Output: Add Two Positive Numbers


J2EE Programming

Page 68 of 79




Output:When negative input(s)


J2EE Programming

Page 69 of 79

































J2EE Programming

Page 70 of 79

EX. No.: 10 SESSION BEAN, REMOTE INTERFACE, CLIENT
APPLICATION
04.10.2012


Aim:
To create a JavaEE application that includes a stateless session bean and a remote
interface
Steps:
1. Creating Java class library: Choose File, go to New Project, select Java Class
Library in the Java category, type EJBRemoteInterface for the Project Name.
Click Finish.
2. Creating JavaEE module:Choose File, go to > New Project and select
Enterprise Application in the Java EE category. Click Next,Type EntAppEJB
for the Project Name. Click Next.
3. Create Session Bean: Right-click the EJB module project and choose New,
choose Session Bean, type MySession for the EJB Name, type ejb for the
Package, select Stateless for the Session Type, select the Remote option for
Create Interface, select the EJBRemoteInterface project from the dropdown
list. Click Finish.
4. Adding business method: Right-click in the editor of MySession and choose
Insert Code and select Add Business Method, type getResult for the Method
Name and String for the Return Type. Click OK. Make the following changes
to modify the getResult method to return a String.
5. Creating Application Client: Choose File, select New Project and select
Enterprise Application Client in the Java EE category. Click Next, type
EntAppClient for the Project Name. Click Next, select GlassFish Server 3.1 for
the Server. Click Finish. Now, expand the Source Packages node of the
EntAppClient project and open Main.java in the editor, right-click in the source
code and choose Insert Code and select Call Enterprise Bean to open the Call
Enterprise Bean dialog. Expand the EntAppEJB project node and select
MySession. Click OK.
6. Run: Right-click the EntAppClient project in the Projects window and choose
Run.

Source Code:
The source codes are enclosed in 10.1, 10.2, and 10.3 of this context
Result:
The run time screen shot of the J2EE application has been enclosed


J2EE Programming

Page 71 of 79

10.1. RemoteInterface: MySessionRemote
package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionnRemote {

String getMsg();

}

10.2. SessionBean: MySessionn
package ejb;

import javax.ejb.Stateless;

@Stateless
public class MySessionn implements MySessionnRemote {

@Override
public String getMsg() {
return "GOD IS GREAT";
}

}

10.3. Application Client: NewMain

public class NewMain {
@EJB
private static MySessionnRemote mySessionn;



public static void main(String[] args)
{

System.out.println( mySessionn.getMsg());
}
}









J2EE Programming

Page 72 of 79

Output: Result can be view in the output window of the picture shown below

























J2EE Programming

Page 73 of 79

EX. No.: 11 SERVLET, BEAN, JSP
(Data sharing program scope=application)
08.10.2012


Aim:
To create a web application that could display todays date in JSP
Steps:
1. Create java file, use javas utility to define date
2. Create a bean (java file) and initialize with java file defined in step 1
3. Create a servlet file and initialize the bean and populate
4. Create a jsp file to display the date.
Source Code:
The source codes are enclosed in 11.1, 11.2, 11.3 and 11.4 of this context
Result:
The run time screen shots of the web application have been enclosed












J2EE Programming

Page 74 of 79

Activity Diagram



1. returns date



2.Initialize Bean

3. Displays
Date





























CalendarJava CalendarServlet CalendarBean CalendarJsp
J2EE Programming

Page 75 of 79

11.1.Servlet: CalendarServlet.java
package NXG;
import java.io.IOException;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CalendarServlet extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
CalendarBean cb=new CalendarBean();
request.setAttribute("cal",cb);
String add="WEB-INF/CalendarJsp.jsp";
RequestDispatcher rd = request.getRequestDispatcher(add);
rd.forward(request, response);
}
}


























J2EE Programming

Page 76 of 79

11.2.Java class(context): CalendarJava.java
package NXG;


import java.util.*;
public class CalendarJava {
int day,month,year;
Calendar c;
public CalendarJava()
{
c=Calendar.getInstance();
CalculateDMY();
}
public void CalculateDMY()
{
day=(c.get(Calendar.DATE));
month=(c.get(Calendar.MONTH));
year=(c.get(Calendar.YEAR));
}
public int returnDay()
{
return(day);
}
public int returnmonth()
{
return(month);

}
public int returnyear()
{
return(year);

}

}















J2EE Programming

Page 77 of 79

11.3.Bean: CalendarBean.java
package NXG;

public class CalendarBean
{
public int bday,bmonth,byear;
CalendarJava cj;
public CalendarBean()
{
cj=new CalendarJava();
bday=cj.returnDay();
bmonth=cj.returnmonth();
byear=cj.returnyear();

setBday(bday);
setBmonth(bmonth);
setByear(byear);
}
public void setBday(int bday)
{
this.bday=bday;
}
public void setBmonth(int bmonth)
{
this.bmonth=bmonth;

}
public void setByear(int byaer)
{
this.byear=byear;

}
public int getBday()
{
return(bday);

}
public int getBmonth()
{
return(bmonth);

}
public int getByear()
{
return(byear);

}
}


J2EE Programming

Page 78 of 79

11.4.JSP: CalendarJsp.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Jsp page that displays date today!</h1>
<u>data sharing through context</u><br>
<font size=18 color=red>
<jsp:useBean id="c" scope="application" class="NXG.CalendarBean"/>
<jsp:getProperty name="c" property="bday"/>
- <jsp:getProperty name="c" property="bmonth"/>
- <jsp:getProperty name="c" property="byear"/>
</font>

</body>
</html>
































J2EE Programming

Page 79 of 79

You might also like