You are on page 1of 49

TYCS SEM-VI

Adv.Java
Practical No.1

Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known,
Hobbies) parameters like check boxes and multiple selection list boxes from an HTML
document and outputs them to the screen.
xxx.java
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="get" action="http://localhost:8084/java/zzz">
Name: <input type="text" name="txtname"><br/><br/>
Phone no: <input type="text" name="phone"/> <br/><br/>
Hobbies <select multiple="multiple" id="sel" name="sel" >
<option value="Dancing">Cricket</option>
<option value=">Shooting">Shooting</option>
<option value="Surfing">Surfing</option>
<option value="Eating">Eating</option>
</select><br/><br/>
Languages:<input type="checkbox" name="chk" value="Marathi"> Marathi
<input type="checkbox" name="chk" value="Russy">Russy
<input type="checkbox" name="chk" value="English">English
<input type="checkbox" name="chk" value="French">French<br/><br/>
<input type="Submit" name="submit" value="submit">
</form>
</body>
</html>
zzz.java:import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class zzz extends HttpServlet {
String name,phon,hobbies,lang;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
name=request.getParameter("txtname");
phon=request.getParameter("phone");
hobbies=request.getParameter("hobbies");

TYCS SEM-VI
lang=request.getParameter("languages");
out.println("Name : " +name +"<br/>");
out.println("Phone No : " +phon+"<br/>");
String []t=request.getParameterValues("sel");
for(int i=0;i<t.length;i++)
{
out.println("Hobbies is " + t[i]+ "<br/>" );
}
String []ty=request.getParameterValues("chk");
for(int i=0;i<ty.length;i++)
{
out.println("languages is " + ty[i]+" <br/>" );
}
}
finally {
out.close();
}
}
}

Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java
Practical No.2

Write a servlet program that accepts an integer n from html form calculates and displays the factorial of n.
If the number is negative, it should redirect to a different error page having link to the html form.
Factorial.HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Factorial</title>
</head>
<body>
<h1> Factorial...</h1>
<form method="Get" action="http://localhost:8084/Factorial/fact">
<input type="text" name="txtnum"><br/><br/>
<input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
error.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class error extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("The number is Negative");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

TYCS SEM-VI
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
faccct.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int fact;
int n=Integer.parseInt(request.getParameter("txtnum"));
fact=1;
try {
if(n>0)
{
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
out.println("Factorial of " + n +" is " +fact);
}
else
{
response.sendRedirect("http://localhost:8084/Factorial/error");
}
}
finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);

Adv.Java

TYCS SEM-VI
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java
Practical No.3

Write two servlets in which one servlet will display a form in which data entry can be done for the fields
dept-no, dept-name and location. In the same form place a button called as submit and on click of that
button this record should be posted to the table called as DEPT in the database. This inserting of record
should be done in another servlet. The second servlet should also display all the previous record entered
in the database.
DeptUi.java:import
import
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class DeptUi extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw= response.getWriter();
response.setContentType("text/html");
pw.println("<html>");
pw.println("<head>");
pw.println("<title>Dept UI </title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1> Department Info.</h1>");
pw.println("<form action='http://localhost:8084/hhh/DeptSave'
method='post'>");
pw.println("<br>Dept ID <input type='text' name='deptId'
size='10'></input>");
pw.println("<br>Dept Name <input type='text' name='deptName'
size='20'></input>");
pw.println("<br>Location <input type='text' name='location'
size='20'></input>");
pw.println("<br><input type='submit' name='submit' value='Save
Department' ></input>");
pw.println("</form>");
pw.println("</body></html>");
}
}
DeptSave.java:import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.*;
javax.servlet.http.*;

TYCS SEM-VI
import java.sql.*;
public class DeptSave extends HttpServlet
{
Connection con;
ResultSet rs;
Statement stmt;
public void init(ServletConfig config)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:hhh");
stmt = con.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
try
{
String deptId = request.getParameter("deptId");
String deptName = request.getParameter("deptName");
String location = request.getParameter("location");
int dept = Integer.parseInt(deptId);
String sql = "insert into dept values("+ dept
+",'"+deptName+"','"+location+"')";
stmt.executeUpdate(sql);
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.println("<h2> Data was saved successfully");
rs = stmt.executeQuery("select * from dept");
pw.println("<table border='1'><tr><th>DeptID </th><th>Dept
Name</th><th>Location</th><tr> ");
while(rs.next())
{
pw.println("<tr><td>");
pw.println(rs.getInt(1));
pw.println("</td><td>");
pw.println(rs.getString(2));
pw.println("</td><td>");
pw.println(rs.getString(3));
pw.println("</td></tr>");
}
pw.println("</table>");

Adv.Java

TYCS SEM-VI
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

Practical No.4
Write a Servlet that accepts a User Name from a HTML form and stores it as a session variable. Write
another Servlet that returns the value of this session variable and displays it.
Session.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class k extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("ID " + session.getId());
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("txtn");
session.setAttribute(dataName, dataValue);
out.println("dataName=" + dataName);

TYCS SEM-VI
}
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println(name + " = " + value);
} }
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Html file
<!-To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="get"
action="http://localhost:8084/shiby/session">
Name: <input type="text"
name="Name"><br/><br/>
<input type="Submit"
name="submit" value="submit">
</form>
</body>
</html>

Adv.Java

TYCS SEM-VI

Adv.Java

OUTPUT:

Practical No.5
Write JSP files that accept a number from the HTML form and
Displays whether its even or odd
Its factorial.
Multiplication Table.
Evenfactmul.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="http://localhost:8084/factjsp/evenfactmul.jsp" method="GET">
<h3>Calculate Factorial ,Even/Odd and Table of given Number.</h3><br>
Enter Number:<input type="text" name="num">
<input type="submit" value="submit">
</form>
</body>
</html>
Evenfactmul.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

TYCS SEM-VI
<title>JSP Page</title>
</head>
<body>
<% String str;
int n;
str=request.getParameter("num");
n=Integer.parseInt(str);%>
<h3>Display Factorial </h3>
<% try {
int fact=1,j;
for(j=1;j<=n;j++)
{
fact=fact*j;
}
out.print("Factorial of "+ n+" is "+fact);
}
catch(Exception e)
{
}
%>
<%! int m; %>
<h3>Display EVEN/ODD </h3>
<%
if(n%2==0)
{
out.print("Even Number");
}
else
{
out.print("Odd Number");
}
%>
<%
int t;
%>
<h3>Display Table </h3>
<%
for(t=1;t<=n;t++)
{
out.print(t*n);
out.print("<br>");
}
%>
</body></html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java

Practical No.7
Write a JSP page, which displays three text boxes for user name, password, and email. On click of the
submit button call another JSP page which will enter the values in the database with the help of
PreparedStatement class. Also use jspInit() and jspDestroy() to open and close the connection.
DesignPage.jsp:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee</title>
</head>
<body>
<h1>Student Details</h1>
<form method="get" action="http://localhost:8084/Database/Database.jsp">
UserName :<input type="text" name="user"><br>
Password :<input type="password" name="pswd"><br>
EmailId :<input type="text" name="email"><br>
<input type="submit" value="Save"><br>
</form>
</body>
</html>
DbPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Insert title here</title>
</head>

TYCS SEM-VI
<body>
<%!
Connection con;
ResultSet rs;
PreparedStatement ptst;
public void jspInit()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Student");
ptst=con.prepareStatement("insert into std values(?,?,?)");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void jspDestroy()
{
try
{
ptst.close();
rs.close();
con.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
%>
<%
String name = request.getParameter("text");
String pass = request.getParameter("password");
String email = request.getParameter("text");
try
{
ptst.setString(1,name);
ptst.setString(2,pass);
ptst.setString(3,email);
ptst.executeUpdate();
out.println("<h3>Record Saved Sucessfully</h3>");
}
catch(Exception e)
{
e.printStackTrace();
}

Adv.Java

TYCS SEM-VI
%>
</body>
</html>

Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

Practical No.8
Create a java bean that gives information about the current time. The bean has getter properties for time,
hour, minute, and second. Write a JSP page that uses the bean and display all the information.
CODE:TimeBean.java:
package test;
import java.util.*;
public class TimeBean {
int hr,min,sec;
Calendar cal;
Date date= new Date();
public TimeBean()
{
hr=1;
min=30;
sec=45;
cal=Calendar.getInstance();

TYCS SEM-VI
}
public int gethr()
{
hr=cal.get(Calendar.HOUR_OF_DAY);
return hr;
}
public int getmin()
{
min=cal.get(Calendar.MINUTE);
return min;
}
public int getsec()
{
sec=cal.get(Calendar.SECOND);
return sec;
}
}
Current.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Current Time</title>
</head>
<body>
<h1>To get the current time </h1>
<form method="get" action="http://localhost:8084/CurrentTime/time.jsp">
<input type="submit" value="CLICK ME">
</form>
</body>
</html>
Time.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="time" class="test.TimeBean" scope="page"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> Current time is</h1>
Hour:<jsp:getProperty name="time" property="hr"/><br>

Adv.Java

TYCS SEM-VI
Minute:<jsp:getProperty name="time" property="min"/><br>
Second:<jsp:getProperty name="time" property="sec"/>
</body>
</html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

Practical No.9
Write a HTML page to accept a database table name from the user on click of the submit button display a
JSP to connect to a database and display the total number of records followed by the contents of the
database table using the HTML TABLE tag, The column names should be fetched from the database.
FirstJSP.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Student Info</title>
</head>
<body>
<%!
Connection con;
ResultSet rs;
Statement stmt;
%>
<%
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

TYCS SEM-VI
con=DriverManager.getConnection("jdbc:odbc:Student","","");
}
catch(Exception e)
{
e.printStackTrace();
}
stmt=con.createStatement();
rs=stmt.executeQuery("select * from StuInfo");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
out.println("<table><tr><td>");
//out.println("<tr><td>");
for (int i = 1; i <= numberOfColumns; i++)
{
out.println("<tr>&nbsp");
out.println(rsMetaData.getColumnName(i));
out.println("&nbsp </tr>");
}
out.println("</td></tr>");
while(rs.next())
{
out.println("<tr><td>");
out.println(rs.getString("sroll").toString());
out.println("</td>");
out.println("<td>");
out.println(rs.getString("sname"));
out.println("</td>");
out.println("<td>");
out.println(rs.getString("sclass"));
out.println("</td></tr>");
}
out.println("</table>");
%>
</body>
</html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

PRACTICAL NO.10
Aim: Develop Converter Stateless Session Bean. Write Enterprise application for converting Japanese
yen currency to Eurodollars currency. Converter consists of an enterprise bean, which performs the
calculations, and a web client (JSP/Servlet) or Application client.
Source code:
converter.java

TYCS SEM-VI
package converter.ejb;
import java.math.BigDecimal;
import javax.ejb.Remote;
@Remote
public interface Converter
{
public BigDecimal dollarToYen(BigDecimal dollars);
public BigDecimal YenToEuro(BigDecimal yen);
}
Converterbean.java
package converter.ejb;
import java.math.BigDecimal;
import javax.ejb.Stateless;
@Stateless
public class ConverterBean implements converter.ejb.Converter{
private BigDecimal eurorate=new BigDecimal("0.0070");
private BigDecimal yenrate=new BigDecimal("112.58");
@Override
public BigDecimal dollarToYen(BigDecimal dollars)
{
BigDecimal result=dollars.multiply(yenrate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
@Override
public BigDecimal YenToEuro(BigDecimal yen)
{
BigDecimal result=yen.multiply(eurorate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
}
converterclient.java
package converter.client;
import converter.ejb.Converter;
import java.math.BigDecimal;
import javax.ejb.EJB;
public class ConverterClient
{
@EJB
private static Converter converter;
public ConverterClient(String[] args)
{
}
public static void main(String args[])
{
ConverterClient client =new ConverterClient(args);
client.doConversion();

Adv.Java

TYCS SEM-VI
}
public void doConversion()
{
try{
BigDecimal param =new BigDecimal("100.00");
BigDecimal yenAmount=converter.dollarToYen(param);
System.out.println("$"+param+ "is" +yenAmount+"Yen");
BigDecimal euroAmount=converter.YenToEuro(yenAmount);
System.out.println(yenAmount +"yen is"+euroAmount+"Euro");
System.exit(0);
}
catch(Exception e)
{
System.err.println("Caught an unexpected exception");
}
}
}
index.jsp
<%@page import="converter.ejb.Converter,java.math.*,javax.naming.*"%>
<%!
private Converter converter=null;
public void jspInit()
{
try
{
InitialContext ic=new InitialContext();
converter=(Converter)ic.lookup(Converter.class.getName());
}
catch(Exception e)
{
System.out.println("Couldnt create converter bean" +e.getMessage());
}
}
public void jspDestroy()
{
converter=null;
}
%>
<html>
<head>
<title>converter</title></head>
<body>
<p>Enter an amount to convert</p>
<form method="get">
<input type=""text" name="amount">
<p><input type="submit" value="submit">
<p><input type="reset" value="reset">
</form>

Adv.Java

TYCS SEM-VI
<%
String amount=request.getParameter("amount");
if(amount!=null && amount.length()>0)
{
BigDecimal d=new BigDecimal(amount);
BigDecimal yenAmount=converter.dollarToYen(d);
%>
<p><%=amount%>dollars are <%=yenAmount%>Yen</p>
<%
BigDecimal euroAmount=converter.YenToEuro(yenAmount);
%>
<%=yenAmount%>Yen are <%=euroAmount%>Euro
<%}
%>
</body>
</html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

PRACTICAL NO.11

TYCS SEM-VI

Adv.Java

Aim: Develop a calculator satateless session bean .Write enterprise application for addind,multiplying,substracting
& dividing two numbersv.Calculator consist of enterprise bean ,which perform the calculation & web
client(JSP/Servlet) or Application servlet
Source code:
package calculatorws_client_application;
public class Main {
public Main() {
}
public static void main(String[] args)
{
try
{
org.me.calculator.client.CalculatorWSService service = new
org.me.calculator.client.CalculatorWSService();
org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();
int i = 3;
int j = 5;
int result = port.add(i, j);
System.out.println("Result = "+result);
}
catch (Exception ex)
{
}
}
}
Output:-

TYCS SEM-VI

Practical #6: CalculatorWSJSPClient

Adv.Java

TYCS SEM-VI

Practical #7: CalculatorWSServletClient

package org.me.calculator.client;
import java.io.*;
import java.net.*;
import javax.servlet.*;

Adv.Java

TYCS SEM-VI

Adv.Java

import javax.servlet.http.*;
import javax.xml.ws.WebServiceRef;
public class ClientServlet extends HttpServlet {
@WebServiceRef(wsdlLocation =
"http://localhost:8080/CalculatorWSApplication/CalculatorWSService?wsdl")
private org.me.calculator.client.CalculatorWSService service;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ClientServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ClientServlet at " + request.getContextPath () + "</h1>");
try { // Call Web Service Operation
org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();
// TODO initialize WS operation arguments here
int i = 3;
int j = 5;
int result = port.add(i, j);
out.println("Result = "+result);
} catch (Exception ex) {
out.println("<p>Exception: " + ex);
}
out.println("</body>");
out.println("</html>");
out.close();
}

Output:-

TYCS SEM-VI

Adv.Java
PRACTICAL NO.12

Aim: Develop CommonDivMul Stateless Session Bean. Write Enterprise application for calculating the
GCD and LCM of two numbers. CommonDivMul consists of an enterprise bean, which performs the
calculations, and a web client (JSP/Servlet) or Application client.
Source Code:
Bean1.java
package rt;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
@Stateless
@LocalBean
public class Bean1 {
int r=-1;
public int gcd(int c,int d)
{
while(r!=0)
{
r=c%d;
c=d;
d=r;
}
return c;
}
public int lcm(int c,int d)
{
int r=-1;
int a1=c;
int a2=d;
while(r!=0)
{
r=a1%a2;
a1=a2;
a2=r;
}
return((c*d)/a1);
}
}
NewServlet
package rt;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

TYCS SEM-VI
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="NewServlet", urlPatterns={"/NewServlet"})
public class NewServlet extends HttpServlet {
@EJB
private Bean1 obj;;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
out.println("GCD of two numbers is "+obj.gcd(a, b));
out.println("<br>");
out.println("LCM of two numbers is"+obj.lcm(a, b));
} finally {
out.close();
}
}
Newjsp.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>To Find GCD and LCM of two Numbers</h1>
<form method="get" action="NewServlet">
Enter First Number:<input type="text" name="t1">
<br>Enter Second Number<input type="text" name="t2"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java
PRACTICAL NO.13

Aim: Design a Mobile Bean as a Stateful EJB containing a variable representing modelname and three
functions setModelName, getPrice, getFeatures. setModelName accepts a string parameter, getPrice &
getFeatures dont have any parameter but return types. Design a servlet that accepts mobile name from
HTML page and shows mobile price and features to user by accessing mobile bean.
Source Code:
First.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="f" action="NewServlet">
Enter mobile model name:<input type="text" name="t">
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
MobileBeanRemote.java
package a;
import javax.ejb.Remote;
@Remote
public interface MobileBeanRemote
{
public void setModelName(String s);
public int getPrice();
public String getFeature()
MobileBean.java
package a;
import javax.ejb.Stateful;
@Stateful
public class MobileBean implements MobileBeanRemote {
int price;
String model,feature;
@Override

TYCS SEM-VI
public void setModelName(String s) {
model=s;
}
@Override
public int getPrice() {
if(model.equals("samsungduos"))
{
price= 3500;
}
if(model.equals("experia"))
{
price=15000;
}
return price;
}
@Override
public String getFeature() {
if(model.equals("samsungduos"))
{
feature="dual sim";
}
if(model.equals("experia")){
feature="super camera and soung";
}
return feature;
}
}
NewServlet.java
package a;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="NewServlet", urlPatterns={"/NewServlet"})
public class NewServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

Adv.Java

TYCS SEM-VI

Adv.Java

String str=request.getParameter("t");
InitialContext ct=new InitialContext();
MobileBeanRemote mb=(MobileBeanRemote)ct.lookup("java:global/ass7b/MobileBean");
mb.setModelName(str);
out.println("price="+mb.getPrice());
out.println("<br>feature="+mb.getFeature());
} catch(Exception e) {
out.print(e) } }
Output:-

TYCS SEM-VI

Adv.Java

PRACTICAL NO.14
Aim: Create a web service that gives (i) NSE Index, (ii) BSE Index, (iii) Gold Rate. The values are
stored in database. Also create a web client for a share trading firm that displays these values on its home
page.
Source Code:
TradeWS
packageorg.me.trade;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.jws.WebParam;
importjava.sql.*;
@WebService()
public class TradeWS
{
@WebMethod
public double giveRate(@WebParam(name = "item") String item)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:webservice");
}
catch(Exception e)
{
System.err.println(e);
}
try
{
rate=rs.getInt(2);
}
}
catch(SQLException e)
{
System.err.println(e);
}
return rate;
}}
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

TYCS SEM-VI
<form action="trade.jsp" method="POST">
<h1>Share trading company</h1>
<hr>
Select item to know the rate
<select name="selitem" >
<option value="NSE">NSE Index
<option value="BSE">BSE Index
<option value="GOLD">Gold Rate
</select>
<input type="Submit" value="submit">
</body>
</html>
trade.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<%
String str=request.getParameter("selitem");
try
{
org.me.trade.client.TradeWS pordouble result=port.giveRate(str);
out.println("Rate is = "+result);
}
catch(Exception e) { }
%>
</body>
</html>
Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java

PRACTICAL NO.15
Aim: Design a web service for college containing 2 functions- 1 st function getCutoff accepts coursename
as parameter and returns last years cutoff for that course as floating point number, 2 nd function getFees
accepts coursename as parameter and returns this years fee for that course as floating point number.
Design a client to test the above web service.
Source Code:
CollegeWS
packageorg.me.college;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.jws.WebParam;
@WebService()
public class CollegeWS
{
@WebMethod
public float getCutOff(@WebParam(name = "CourseName") String CourseName)
{
float cutoff;
if (CourseName.equalsIgnoreCase("CS"))
{
cutoff=40.0f;
}
else if (CourseName.equalsIgnoreCase("IT"))
{
cutoff=45.0f;
}
else
cutoff=35.0f;
return cutoff;

TYCS SEM-VI

Adv.Java

}
@WebMethod
public float getFees(@WebParam(name = "Coursename") String Coursename)
{
float fees;
if (Coursename.equalsIgnoreCase("CS"))
{
fees=18000.0f;
}
else if (Coursename.equalsIgnoreCase("IT"))
{
fees=25000.0f;
}
else
fees=10000.0f;
return fees;
}
}
Code for Main.java
packagecollegeclient;
public class Main
{
public Main() {
}
public static void main(String[] args)
{
try
{
org.me.college.client.CollegeWSService service =new org.me.college.client.CollegeWSService();
org.me.college.client.CollegeWS port=service.getCollegeWSPort();
float cutoff=port.getCutOff("CS");
System.out.println("Cutoff for CS course are "+cutoff);
float fees=port.getFees("CS");
System.out.println("Fees for CS course is "+ fees);
}
catch(Exception e){}
}
}
Output:-

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java

TYCS SEM-VI

Adv.Java

PRACTICAL NO.16
Aim: Create a web service for UGC that contains a method which accepts college name as parameter and
returns the NAAC rating. The college names and their ratings are stored in database. Design a web client
to test the above web service
Source Code:
UGCWS
packageorg.me.ugc;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.jws.WebParam;
importjava.sql.*;
@WebService()
public class UGCWS
{
public String gateRating(@WebParam(name = "cname") String cname)
{
String rating="A";
Connection con=null;
Try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:webservice");
}
catch(Exception e)
{

TYCS SEM-VI
System.err.println(e);
}
try {
Statement st=con.createStatement();
ResultSetrs=st.executeQuery("Select * from naac where college='" + cname + "'");
while(rs.next())
{
rating=rs.getString(2);
} }
catch(SQLException e)
{
System.err.println(e);
}
return rating } }
Code for index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="naac.jsp" method="POST">
<h1>UGC NAAC Rating</h1>
<hr>
Select College to know the rating
<select name="selitem" >
<option value="JSM College">JSM College
<option value="CKT College">CKT College
<option value="KC College">KC College
<option value="Pillai College">Pillai College
</select>
<input type="Submit" value="submit">
</body>
</html>
Code for naac.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>UGC NAAC Rating</h1>
<%
String str=request.getParameter("selitem");
try

Adv.Java

TYCS SEM-VI
{
org.me.ugc.client.UGCWSService service=new org.me.ugc.client.UGCWSService();
org.me.ugc.client.UGCWS port=service.getUGCWSPort();
String result=port.gateRating(str);
out.println("UGC NAAC Rating of "+str +" is "+result);
}
catch(Exception e)
{
}
%>
</body>
</html>

Output:-

Adv.Java

TYCS SEM-VI

Adv.Java

You might also like