· web viewpractical 1 simple server-side programming using servlet 1.1. write a servlet that...

65
Practical 1 Simple Server-Side programming using Servlet 1.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. 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> <form method="post" action="abc"> <table border=1> <tr> <td>Name</td> <td><input type="text" name="Name"></td> </tr> <tr> <td>Tel.No</td>

Upload: truongthu

Post on 07-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 1

Simple Server-Side programming using Servlet1.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.

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>

<form method="post" action="abc">

<table border=1>

<tr>

<td>Name</td>

<td><input type="text" name="Name"></td>

</tr>

<tr>

<td>Tel.No</td>

<td><input type="text" name="Telno"></td>

</tr>

<tr>

<td>Language</td>

<td><input type=checkbox name=language value=eng>Eng</td>

Page 2:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

<td><input type=checkbox name=language value=mar>Marathi</td>

<td><input type=checkbox name=language value=hin>Hindi</td>

</tr>

<tr>

<td>Software</td>

<td>

<select multiple size=5 name="software">

<option>Excel

<option>VB

<option>Java

<option>C++

</select></td></tr>

<tr>

<td><input type=submit value=submit></td>

<td><input type=reset></td>

</tr>

</table>

</form>

</body>

</html>

abc.java

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;

Page 3:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

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(urlPatterns = {"/abc"})

public class abc extends HttpServlet

{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter())

{

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet abc</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Servlet abc at " + request.getContextPath() + "</h1>");

out.println("</body>");

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

}

}

@Override

Page 4:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

processRequest(request, response);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

try

{

response.setContentType("text/html");

Enumeration e=request.getParameterNames();

PrintWriter out = response.getWriter();

while(e.hasMoreElements())

{

String name=(String)e.nextElement();

out.println(name);

String[] value=request.getParameterValues(name);

for(int i=0;i<value.length;i++)

{

out.print(value[i]+"<tr>");

}

}

}

catch(Exception e)

Page 5:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

{

System.out.println("ERROR ="+e.getMessage());

}

}

}

}

@Override

public String getServletInfo()

{

return "Short description";

}

}

Page 6:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 7:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

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

index.html

<html>

<head>

<title>Factorial</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="Factorial" method="get">

Enter Number :<input type="text" name="t1">

<br> <br>

<input type="submit" value="submit">

</form>

</body>

</html>

Factorial.java

import java.io.IOException;

import java.io.PrintWriter;

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(urlPatterns = {"/Factorial"})

public class Factorial extends HttpServlet

Page 8:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet Factorial</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Servlet Factorial at " + request.getContextPath() + "</h1>");

out.println("</body>");

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

}

}

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter pw= response.getWriter())

{

int x=Integer.parseInt(request.getParameter("t1"));

Page 9:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

int f=1;

for(int i=1;i<=x;i++)

{

f=f*i;

}

pw.println(f);

}

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

processRequest(request, response);

}

@Override

public String getServletInfo() {

return "Short description";

}

}

Page 10:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 11:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 2

Advance Server-side programming using Servlets.2.1. Write two servlets in which one servlet will display a form in which data entry can be done for the field’s 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 recordentered in the database.

NewServlet.javaimport java.io.IOException;import java.io.PrintWriter;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(urlPatterns = {"/NewServlet1"})public class NewServlet1 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewServlet1</title>"); out.println("</head>"); out.println("<body>"); out.println("<Form name=frm method="+"POST"+" action=NewServlet>"); out.println("DepartmentNo: <input type=text name=txtNo><br>"); out.println("DepartmentName: <input type=text name=txtName><br>"); out.println("Location: <input type=text name=txtLoc><br>"); out.println("<input type=submit name=Submit>"); out.println("<input type=reset name=Reset>"); out.println("</form>");

Page 12:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

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

@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"; }}

NewServlet1.javaimport java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

Page 13:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

@WebServlet(urlPatterns = {"/NewServlet"})public class NewServlet extends HttpServlet{ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } }

@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{ String a,b,c,d,e,f; int i; Connection con = null; try

Page 14:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

{ response.setContentType("text/html"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:First"); String Query="insert into dept Values(?,?,?)"; java.sql.Statement st=con.createStatement(); PreparedStatement ps; ps=con.prepareStatement(Query); a=(String)request.getParameter("txtNo"); b=(String)request.getParameter("txtName"); c=(String)request.getParameter("txtLoc"); ps.setString(1,a); ps.setString(2,b); ps.setString(3,c); ps.executeUpdate(); PrintWriter out=response.getWriter(); ResultSet rs=st.executeQuery("select * from dept"); ResultSetMetaData md=rs.getMetaData(); int num=md.getColumnCount(); out.println("<html><body><table border=1><tr>"); for(i=1;i<=num;i++) { out.print("<th>"+md.getColumnName(i)+"</th>"); } out.println("</tr>"); while(rs.next()) { d=rs.getString(1); e=rs.getString(2); f=rs.getString(3); out.println("<tr><td>"); out.println(d); out.println("</td><td>"); out.println(e); out.println("</td><td>"); out.println(f); out.println("</td></tr>"); } out.println("</table>"); con.commit(); out.println("<a href=Newservlet1.java>BACK</a>"); out.println("</body></html>"); } catch ( ClassNotFoundException | SQLException | IOException ae) { System.out.println(ae.getMessage());

Page 15:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

}}

Output :

Page 16:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

2.2. Write a Servlet that accepts a username 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.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>

<form action="Servlet1">

Name :

<input type="text" name="Username"/><br>

<input type="submit" value="go"/>

</form>

</body>

</html>

NewServlet.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

Page 17:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

@WebServlet(urlPatterns = {"/NewServlet"})

public class NewServlet extends HttpServlet

{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

String n= request.getParameter("Username");

out.print("Welcome" +n);

HttpSession session=request.getSession();

session.setAttribute("Uname",n);

out.print("<a href = 'NewServlet1'>Visit</a>");

}

}

NewServlet1.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

Page 18:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/NewServlet1"})

public class NewServlet1 extends HttpServlet

{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter())

{

HttpSession session=request.getSession(false);

String n=(String)session.getAttribute("Uname");

out.print("Hello " +n);

}

}

Page 19:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 20:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 3

Simple Server-side programming using JSP3.1. Write JSP files that accept a number from the HTML form and

Displays whether it is even or odd. It’s Factorial Multiplication table.

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>

<form action="NewJSP.jsp">

Enter Number :

<br><br>

<input type="text" name="n">

<input type="submit" value="Submit">

</form>

</body>

</html>

NewJSP.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

Page 21:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h4>

<%int n=0,i;

int fact=1;

String ns=request.getParameter("n");

n=Integer.parseInt(ns);

if(n>1)

{

fact=1;

for(i=1;i<=n;i++)

{

fact=fact*i;

}

}

out.println("Factorial of that number is= "+ fact);

%>

<br><br>

<%

if(n%2==0)

{

out.println("This number is even");

}

else

{

Page 22:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

out.println("This number is odd");

}

%>

<br><br>

<%

for(int k=1;k<=10;k++)

{

out.println(n*k);

}

%>

</h4>

</body>

</html>

Page 23:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 24:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

3.2. Write a JSP page to accept two numbers (m,n) from the user, on click of the submit button display on a new JSP page all prime numbers between m and n. If the number m is not less than n (m<n), it should redirect to a different error page having link to the JSP form.

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>

<form action="PrimeNumber.jsp">

Enter the lower bound :

<input type="text" name="n">

<br><br>

Enter the upper bound :

<input type="text" name="n1">

<br><br>

<input type="submit" value="submit">

</form>

</body>

</html>

PrimeNumber.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Page 25:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

int p,d=2;

int Primeflag;

String r1=request.getParameter("n");

int n=Integer.parseInt(r1);

String r2=request.getParameter("n1");

int n1=Integer.parseInt(r2);

for(p=n;p<=n1;p++)

{

Primeflag=1;

for(d=2;d<=p-1;d++)

{

if(p%d==0)

{

Primeflag=0;

}

}

if(Primeflag==1)

out.print("\t" +p);

}

Page 26:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

%>

</body>

</html>

Output :

Page 27:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 4

Advance Server-side programming using JSP.4.1. Write a JSP page, which displays three text boxes for Department Number, Department Name and Location. 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. (Register.jsp).

DeptForm.jsp<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form method=GET action="Register1.jsp"><table><tr><td>DepartmentNo: </td><td> <input type=text name=txtNo></td></tr><tr><td>DepartmentName: </td><td><input type=text name=txtName></td></tr><tr><td>Location:</td><td> <input type=text name=txtLoc></td></tr></table><input type=submit value=Submit><input type=reset name=Reset></Form>

</body></html>

Resigter1.jsp<%-- Document : Register1 Created on : Feb 20, 2017, 10:18:03 PM Author : USER--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><%@ page import="java.sql.*" %><%! String a,b,c,d,e,f,Query; %>

Page 28:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

<%! Connection con;Statement st;PreparedStatement ps; %><%! int i,num; %><%!public void jspInit(){try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");con=DriverManager.getConnection("jdbc:odbc:connect");st=con.createStatement();Query="insert into Dept values(?,?,?)";ps=con.prepareStatement(Query);}catch(Exception e){System.out.println("Error: "+e.getMessage());}}%><%a=(String)request.getParameter("txtNo");b=(String)request.getParameter("txtName");c=(String)request.getParameter("txtLoc");ps.setInt(1,Integer.parseInt(a));ps.setString(2,b);ps.setString(3,c);ps.executeUpdate();con.commit();ResultSet rs=st.executeQuery("select * from Dept");%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <table border=1><tr><th>Dept No </th><th>Dept Name</th><th>Location</th></tr><%while(rs.next()){%><tr><% for(int j=0;j<=2;j++)

Page 29:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

{Object obj=rs.getObject(j+1);%><td><%=obj.toString()%></td><%}}%></tr></table><%!public void jspDestroy(){ try{ps.close();

st.close();}catch(Exception e){System.out.println("Error:"+e.getMessage());}}%></body></html>

Page 30:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 31:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

4.2. Create a java bean that gives information about the current time. The bean has getter properties for time, hour, minute, second. Write a JSP page that uses the bean and display all the information.

CalenderB.java

package myclass;

import java.util.Calendar;

import java.util.Date;

public class CalenderB

{

private final Calendar calendar;

public CalenderB()

{

calendar = Calendar.getInstance();

}

public Date Time;

public Date getTime()

{

return calendar.getTime();

}

public int Hour;

public int getHour()

{

return calendar.get(Calendar.HOUR_OF_DAY);

}

Page 32:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

public int Minute;

public int getMinute()

{

return calendar.get(Calendar.MINUTE);

}

public int Second;

public int getSecond()

{

return calendar.get(Calendar.SECOND);

}

}

BeanTime.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<jsp:useBean class="myclass.CalenderB" id="cal"/>

<pre>

Page 33:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Time : <jsp:getProperty name="cal" property="time"/> <br>

Hour : <jsp:getProperty name="cal" property="hour"/> <br>

Minute : <jsp:getProperty name="cal" property="minute"/> <br>

Second : <jsp:getProperty name="cal" property="second"/> <br>

</pre>

</body>

</html>

Output :

Page 34:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 5

Developing Simple Enterprise Java Beans5.1. 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.

Jsp file :

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<form method = "get" action="ConverterServlet">

<table border="2">

<tr>

<td> Enter Amount </td>

<td> <input type="text" name="txtnum"> </td>

</tr>

<tr>

<td> <input type="Submit" name="Submit"> </td>

</tr>

</table>

</form>

Page 35:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

</body>

</html>

ConverterBeanoneRemote.java

package Server;

import java.math.BigDecimal;

import javax.ejb.Remote;

@Remote

public interface ConverterBeanoneRemote

{

BigDecimal dollarToYen(BigDecimal dollars);

BigDecimal yenToEuro(BigDecimal yen);

}

ConverterBeanone.java

package Server;

import java.math.BigDecimal;

import javax.ejb.Stateless;

@Stateless

public class ConverterBeanone implements ConverterBeanoneRemote

{

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

Page 36:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

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

}

}

ConverterServlet.java

package server;

import Server.ConverterBeanoneRemote;

import java.io.IOException;

import java.io.PrintWriter;

import java.math.BigDecimal;

import javax.ejb.EJB;

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 = "ConverterServlet", urlPatterns = {"/ConverterServlet"})

public class ConverterServlet extends HttpServlet

{

Page 37:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

@EJB(name = "Conv")

private ConverterBeanoneRemote conv;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter())

{

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet ConverterServlet</title>");

out.println("</head>");

out.println("<body>");

String str=request.getParameter("txtnum");

int txtnum=Integer.parseInt(str);

BigDecimal num=new BigDecimal(txtnum);

out.println("<h2> Dollar To Yen"+conv.dollarToYen(num)+ "</h2>");

out.println("<h2> yen to euro" +conv.yenToEuro(num)+" </h2>");

out.println("</body>");

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

}

}

Page 38:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 39:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

5.2. . Develop Calculator Stateless Session Bean. Write enterprise application for adding, subtracting, multiplying and dividing two numbers. Calculator consists of an enterprise bean, which performs the calculations, and a web client(JSP/Servlet) or Application Client.

CalculatorBeanOne.java

@Stateless

public class CalculatorBeaneOne implements CalculatorBeaneOneRemote

{

@Override

public int add(int x, int y)

{

return x+y;

}

public int sub(int x, int y)

{

return x-y;

}

public int mul(int x, int y)

{

return x*y;

}

public int div(int x, int y)

{

return x/y;

}

}

Page 40:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

CalculatorBeanOneRemote.java

package server1;

import javax.ejb.Remote;

@Remote

public interface CalculatorBeaneOneRemote

{

int add(int y, int x);

int sub(int x, int y);

int mul(int x, int y);

int div(int x, int y);

}

Calc.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<form action="fetch" method="get">

<h2>Enter First Number : </h2>

<input type="text" name="t1"><br>

<h2>Enter Second Number : </h2>

<input type="text" name="t2"><br>

Page 41:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

<h3>Select Operation : </h3>

<input type="radio" name="operation" value="add">Addition<br>

<input type="radio" name="operation" value="sub">Subtraction<br>

<input type="radio" name="operation" value="mul">Multiplication<br>

<input type="radio" name="operation" value="div">Division<br>

<input type="submit" value="Submit">

</form>

</body>

</html>

Fetch.java

package server1;

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;

import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "fetch", urlPatterns = {"/fetch"})

public class fetch extends HttpServlet

{

@EJB(name = "cbo")

private CalculatorBeaneOneRemote cbo;

Page 42:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter())

{

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet fetch</title>");

out.println("</head>");

out.println("<body>");

int a=Integer.parseInt(request.getParameter("t1"));

int b=Integer.parseInt(request.getParameter("t2"));

String o=request.getParameter("operation");

if(o.equals("add"))

{

out.println("<h1>Addition is : " +cbo.add(a,b)+"</h1>");

}

if(o.equals("sub"))

{

out.println("<h1>Subtraction is : " +cbo.sub(a,b)+"</h1>");

}

if(o.equals("mul"))

{

Page 43:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

out.println("<h1>Multiplication is : " +cbo.mul(a,b)+"</h1>");

}

if(o.equals("div"))

{

out.println("<h1>Division is : " +cbo.div(a,b)+"</h1>");

}

out.println("</body>");

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

}

}

Page 44:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Output :

Page 45:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 6

6.Developing Simple Web Services in Java6.1. 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.

NseBseGoldWSIndexWebservice.javapackage server;

import javax.jws.WebService;import javax.jws.WebMethod;import javax.jws.WebParam;import java.io.*;import java.sql.*;

@WebService(serviceName = "IndexWebService")public class IndexWebService{ Connection con; Statement st; String DriverName="sun.jdbc.odbc.JdbcOdbcDriver"; String dbURL="jdbc:odbc:MYDSN";

ResultSetrs;floatnse, bse, gold;

@WebMethod(operationName = "getNSE")public Float getNSE() {try {Class.forName(DriverName);con=DriverManager.getConnection(dbURL);st=con.createStatement();rs=st.executeQuery("select * from Index");rs.next();nse=Float.parseFloat(rs.getString(1));

Page 46:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

}catch(ClassNotFoundException | SQLException e) {System.out.println(e.getMessage()); }

returnnse; }

@WebMethod(operationName = "getBSE")public Float getBSE() {try {Class.forName(DriverName);con=DriverManager.getConnection(dbURL);st=con.createStatement();rs=st.executeQuery("select * from Index");rs.next();bse=Float.parseFloat(rs.getString(2));

}catch(ClassNotFoundException | SQLException e) {System.out.println(e.getMessage()); }

returnbse; }

@WebMethod(operationName = "getGold")public Float getGold() {try {Class.forName(DriverName);con=DriverManager.getConnection(dbURL);st=con.createStatement();rs=st.executeQuery("select * from Index");rs.next();gold=Float.parseFloat(rs.getString(3)); }

Page 47:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

catch(ClassNotFoundException | SQLException e) {System.out.println(e.getMessage()); }return gold; }}

Homepage.jsp<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title></head><body><%-- start web service invocation --%><hr/><%try {

server.IndexWebService_Service service = new server.IndexWebService_Service();

server.IndexWebService port = service.getIndexWebServicePort();java.lang.Float result = port.getBSE();out.println("Result = "+result);

} catch (Exception ex) { } %><%-- start web service invocation --%><hr/><%try {

server.IndexWebService_Service service = new server.IndexWebService_Service();

server.IndexWebService port = service.getIndexWebServicePort();java.lang.Float result = port.getGold();out.println("Result = "+result);

} catch (Exception ex) {// TODO handle custom exceptions here

} %><%-- end web service invocation --%><hr/><%-- start web service invocation --%><hr/><%

Page 48:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

try {server.IndexWebService_Service service = new

server.IndexWebService_Service();server.IndexWebService port = service.getIndexWebServicePort();// TODO process result herejava.lang.Float result = port.getNSE();out.println("Result = "+result);

} catch (Exception ex) {// TODO handle custom exceptions here

} %><%-- end web service invocation --%><hr/><%-- end web service invocation --%><hr/><h1>Hello World!</h1></body></html>

Homepageservlet.javapackage server;

importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.xml.ws.WebServiceRef;

public class HomepageServlet extends HttpServlet {@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/NseBseGold/IndexWebService.wsdl")privateIndexWebService_Service service;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {response.setContentType("text/html;charset=UTF-8");try (PrintWriter out = response.getWriter()) {out.println("<!DOCTYPE html>");out.println("<html>");out.println("<head>");

Page 49:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

out.println("<title>Servlet HomepageServlet</title>"); out.println("</head>");out.println("<body>");out.println("<br> value of NSE is "+getNSE());out.println("<br> value of BSE is "+getBSE());out.println("<br> value of Gold is "+getGold());out.println("</body>");out.println("</html>"); } }

private Float getBSE() {server.IndexWebService port = service.getIndexWebServicePort();returnport.getBSE(); }

private Float getGold() {server.IndexWebService port = service.getIndexWebServicePort();returnport.getGold(); }

private Float getNSE() {server.IndexWebService port = service.getIndexWebServicePort();returnport.getNSE(); }}

Page 50:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

Practical 7

7.Developing Advance Web Services in Java7.1. 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

UGC1.UgcWebservice.javapackage server;

import javax.jws.WebService;import javax.jws.WebMethod;import javax.jws.WebParam;import java.io.*;import java.sql.*;

@WebService(serviceName = "UgcWebService")public class UgcWebService{ Connection con; Statement st; String DriverName="sun.jdbc.odbc.JdbcOdbcDriver"; String dbURL="jdbc:odbc:MYDSN";

ResultSetrs;floatnaac; String collegen ;

@WebMethod(operationName = "getCollege")public Float getCollege(@WebParam(name = "college") String college) {collegen=college;try {

Class.forName(DriverName);con=DriverManager.getConnection(dbURL);st=con.createStatement();

Page 51:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

rs=st.executeQuery("select * from UGC where CollegeName="+ "\'" + collegen + "\'");System.out.println(collegen);rs.next();naac=Float.parseFloat(rs.getString(2));

}catch(ClassNotFoundException | SQLException e) {System.out.println(e.getMessage()); }

returnnaac; }}

2.UGC.html<html><head><title>NAAC Rating</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width"></head><body><form method="get" action="http://localhost:8080/UGC/UGCServlet"><b>Know NAAC Rating For your College</b><br> Enter Your college name:<input type="text" name="txtn"><br><input type="submit" value="submit"></form></body></html>

3.UGCServlet.javapackage server;

importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.xml.ws.WebServiceRef;import server1.UgcWebService_Service;

Page 52:  · Web viewPractical 1 Simple Server-Side programming using Servlet 1.1. Write a servlet that accepts single-valued (Name, Mobile No.) as well as multi-valued (Languages known, Hobbies)

public class UGCServlet extends HttpServlet{@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/UGC/UgcWebService.wsdl")privateUgcWebService_Service service;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {response.setContentType("text/html;charset=UTF-8");try (PrintWriter out = response.getWriter()) { String collegen= request.getParameter("txtn"); out.println("<!DOCTYPE html>");out.println("<html>");out.println("<head>");out.println("<title>Servlet UGCServlet</title>"); out.println("</head>");out.println("<body>");out.println("<br>Your College NAAC Grade is "+getCollege(collegen));out.println("</body>");out.println("</html>"); } }

private Float getCollege(java.lang.String college) {are not thread safe.synchronization is required.server1.UgcWebService port = service.getUgcWebServicePort();returnport.getCollege(college); }

}