0% found this document useful (0 votes)
4 views54 pages

Advanced Java

Uploaded by

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

Advanced Java

Uploaded by

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

PRN No: Advanced Java Technologies

PRACTICAL NO: 1
Aim:: Implement the following Simple Servlet application.
A: Create a simple calculator application using servlet.
INPUT::
index.html:

<html>
<head>

<title>Simple Calculator</title>

</head>
<body>

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


Enter number 1:<input type="text" name="n1">

Enter number 2:<input type="text" name="n2">


Select Operator:<select name="operator">

<option value="+">+</option>

<option value="-">-</option>
<option value="*">*</option>

<option value="/">/</option>

</select>
<input type="submit" value="Calculate">
<input type="reset" value="Reset">
</form> </body> </html>

check.java:
import java.io.* import
javax.servlet.*;
public class check extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)

throws ServletException, IOException {

PrintWriter out = res.getWriter();


int a=Integer.parseInt(req.getParameter("n1"));

1
PRN No: Advanced Java Technologies
int b=Integer.parseInt(req.getParameter("n2"));
String op=req.getParameter("operator");
if(op.equals("+")){
out.println("Addition is: "+(a+b));

else if(op.equals("-")){ out.println("Subtraction


is: "+(a-b));

}
else
if(op.equals("*")){ out.println("Multiplicatio
n is: "+(a*b));

else{

out.println("division is: "+(a/b));


}

}
}

OUTPUT:

2
PRN No: Advanced Java Technologies
B: Create a servlet for a login page. If the username and password are correct
then it says message “Hello <username>” else a message “Login failed”.
INPUT::
index.html:
<html>

<head>
<title>Login Page</title>

</head>
<body>
<form action="Demo">

Enter UserName: <input type="text" name="n1"><br>


Enter Password: <input type="password" name="n2"><br><br>

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

<input type="reset" value="Reset">


</form>

</body>
</html>

Demo.java:
import java.io.*;
import javax.servlet.*;
public class Demo extends GenericServlet {

public void service(ServletRequest req, ServletResponse res) throws


ServletException, IOException {
PrintWriter out = res.getWriter();
String n1=req.getParameter("n1");
String n2=req.getParameter("n2");
if(n1.equals("SHUBHAM") && n2.equals("115"))

out.println("<h1>HELLO "+n1+"<br>"); out.println("You


are succesfully logged in...</h1>");

3
PRN No: Advanced Java Technologies
else
{

out.println("<h1>Login Failed</h1>");
}

OUTPUT:

4
PRN No: Advanced Java Technologies
C: Create a registration servlet in java using JDBC. Accept the details such as
Username, Password, Email and Country from the user using HTML Form and
store the registration details in the database.
INPUT::
index.html:
<html>
<head>

<title>Registration Servlet</title>
</head>

<body>
<form action="register" method="get">

Enter Username:<input type="text" name="n1"><br>


Enter Password:<input type="text" name="n2"><br>
Enter E-mail:<input type="text" name="n3"><br>
Enter Country Name:<input type="text" name="n4"><br><br>
<input type="submit" value="Register">

<input type="reset" value="Reset">


</form>
</body>

</html>

register.java:
import java.io.IOException;
import java.io.PrintWriter; import
java.sql.Connection; import
java.sql.DriverManager;

import java.sql.PreparedStatement; import


javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

5
PRN No: Advanced Java Technologies
@WebServlet(urlPatterns = {"/register"})
public class register extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

}}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


PrintWriter out = response.getWriter();

String s1 = request.getParameter("n1");
String s2 = request.getParameter("n2");

String s3 = request.getParameter("n3");
String s4 = request.getParameter("n4");

try{ Class.forName("com.mysql.jdbc.Dri
ver"); Connection con =

DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","system");
String sql = "Insert into register values(?,?,?,?)";

PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,s1);

ps.setString(2,s2);
ps.setString(3,s3);
ps.setString(4,s4);
ps.executeUpdate();
out.println("Data Inserted!");
con.close();

}
catch(Exception
e){ out.println(e);

}}}

6
PRN No: Advanced Java Technologies
OUTPUT:

Database:

7
PRN No: Advanced Java Technologies
PRACTICAL NO: 2
Aim:: Implement the following Servlet applications with Cookies and Session.
A: Using Request Dispatcher Interface create a Servlet which will validate the
password entered by the user, if the user has entered "Servlet" as password, then
he will be forwarded to “Welcome” Servlet else the user will stay on the
“index.html” page and an error message will be displayed.
INPUT::
index.html:
<html>
<head>

<title>Request Dispatcher</title>
</head>

<body>

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


Enter Username:<input type="text" name="n1"><br>
Enter Password:<input type="password" name="n2"><br><br>
<input type="submit" value="Submit">

<input type="reset" value="Reset">

</form>
</body>
</html>

Demo.java:
import java.io.*;
import javax.servlet.*;

public class Demo extends GenericServlet {

public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException


{

PrintWriter out=res.getWriter(); String


n1=req.getParameter("n1"); String
n2=req.getParameter("n2");
if(n2.equals("Servlet"))

8
PRN No: Advanced Java Technologies
{
req.setAttribute("user",n1);

RequestDispatcher rd=req.getRequestDispatcher("welcome");
rd.forward(req, res);

else
{

out.print("Password is invalid");
}

}}
welcome.java:

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

public class welcome extends GenericServlet {

public void service(ServletRequest req, ServletResponse res)throws IOException, ServletException

String s =(String)req.getAttribute("user");
PrintWriter out=res.getWriter();

out.print("<h1>Hello "+s+"</br>You are Welcome here."+"</h1>");

}}

OUTPUT:

9
PRN No: Advanced Java Technologies
B: Create a servlet that uses Cookies to store the number of times a user has visited
servlet.
INPUT::
index.html:
<html>

<head>
<title>Using Cookies</title>

</head>
<body>
<form action="cookie">

<input type="submit" value="Go to servlet for visiting counter"/>


</form>

</body>

</html>

cookie.java:
import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class cookie extends


HttpServlet{ static int i=1;
public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException,

ServletException{

res.setContentType("text/html");
PrintWriter out=res.getWriter();

String k=String.valueOf(i);
Cookie c=new Cookie("visit",k);
res.addCookie(c);

int j=Integer.parseInt(c.getValue());
if(j==1){

out.println("<h3>!!Welcome!! Now refresh your page</h3>");

}
10
PRN No: Advanced Java Technologies
else{
out.println("<h3>You visited "+i+" times</h3>");

}
i++;

}}

OUTPUT:

After 3rd Refresh:

11
PRN No: Advanced Java Technologies
C: Create a servlet demonstrating the use of session creation and destruction. Also
check whether the user has visited this page first time or has visited earlier also using
sessions. INPUT::
index.html
<html>

<head>
<title>Using Session</title>

</head>
<body>

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

Enter User Id:<input type="text" name="txtName"><br><br>


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

<input type="reset" value="Reset">


</form>

</body>

</html>

Session.java:
import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class Session extends HttpServlet


{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException,
ServletException

{
PrintWriter out=res.getWriter();
out.println("<html><body>"); HttpSession
hs=req.getSession(true); if(hs.isNew())

{
String name=req.getParameter("txtName");

12
PRN No: Advanced Java Technologies
hs.setAttribute("uname", name);
hs.setAttribute("visit","1");
out.println("<h2>Welcome First Time</h2>");

else

{
out.println("<h2>Welcome Again</h2>");
int visit=Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h2>You visited "+visit+" times</h2>");
hs.setAttribute("visit", visit);

out.println("<h2>Your Seesion Id "+hs.getId()+"</h2>");

out.println("<h2>You Loged in at "+new java.util.Date(hs.getCreationTime())+"</h2>");


out.println("<h2><a href=logoutServlet>Click to LOGOUT</a></h2>"); out.println("</body>");
out.println("</html>");

logoutServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class logoutServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException,


ServletException

{
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<body>");
HttpSession hs=req.getSession();

13
PRN No: Advanced Java Technologies
if(hs!=null);
{

hs.invalidate();
}

out.println("<h1>You are logout now...</h1>");


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

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

}
}

OUTPUT:

After Refreshing the Page: After clicking on the LOGOUT link:

14
PRN No: Advanced Java Technologies
PRACTICAL NO: 3
Aim:: Implement the Servlet IO and File applications.
A: Create a servlet application to upload and download a file.
Upload File:-
INPUT::
index.html:
<html>

<head>

<title>File_Upload</title>
</head>

<body>
<h2> File upload </h2> Select
the file to upload:<br>

<form action="uploadServlet" method ="POST" enctype="multipart/form-data">

<input type="file" name="file" size="50" value="Choose File"><br><br>

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


</form>

</body>
</html>

uploadServlet.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;
import com.oreilly.servlet.MultipartRequest;
public class uploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

15
PRN No: Advanced Java Technologies
PrintWriter out=response.getWriter();
MultipartRequest m=new MultipartRequest(request,"D:\\");
out.print("<html><body><h2>Sucessfully Uploaded</html></body></h2>");

OUTPUT:

16
PRN No: Advanced Java Technologies
Download File:-
INPUT::
index.html:
<html>

<head>
<title>Download File</title>

</head>
<body> <h2> File Download </h2>

<a href="download">Download the file</a>

</body></html>

download.java:
import java.io.*;

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

public class download extends HttpServlet{


public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException,
ServletException{
PrintWriter out=res.getWriter();

String filename="Practical3.txt"; String filepath="D:\\";


res.setHeader("Content-Disposition","attachment; filename= "+filename);
FileInputStream f=new FileInputStream(filepath+filename);
int i;
while((i=f.read())!=-
1){ out.write(i);

}
f.close();

}}

17
PRN No: Advanced Java Technologies
OUTPUT:

After clicking on “Download the file”:

18
PRN No: Advanced Java Technologies
B :Develop Simple Servlet Question Answer Application.
INPUT::
index.html:
<html>

<head>
<title>Question Answer Application</title>

</head>
<body>

<form method="post" action="checkServlet"><h1>My paper</h1>

Q. 1) Which is a valid keyword in java?<br>


<input type="radio" name="r1" value ="int"/>interface

<input type="radio" name="r1" value ="F"/>Float


<input type="radio" name="r1" value ="s"/>string
<input type="radio" name="r1" value ="all"/>All of the above<br><br>
Q. 2) What is sent to the user via HTTP, invoked using the HTTP protocol on the user's computer, and
run
on the user's computer as an application?<br>

<input type="radio" name="r2" value="app"/>A Java application

<input type="radio" name="r2" value="applet"/>A Java applet


<input type="radio" name="r2" value="servlet"/>A Java servlet

<input type="radio" name="r2" value="none"/>None of the above <br><br>


Q. 3) JDBC stands for:<br>

<input type="radio" name="r3" value="jdbc"/>Java Database Connectivity


<input type="radio" name="r3" value="jdbc1"/>Java Database Components
<input type="radio" name="r3" value="jdbc2"/>Java Database Control

<input type="radio" name="r3" value="none"/>None of the above <br><br>


Q. 4) What is bytecode?<br>

<input type="radio" name="r4" value="msc"/>Machine-specific code


<input type="radio" name="r4" value="jcn"/>Java code

<input type="radio" name="r4" value="mic"/>Machine-independent code

<input type="radio" name="r4" value="none"/>None of the above <br><br>


<input type="submit" value="Display Result"/>

19
PRN No: Advanced Java Technologies
<input type="reset" value="Reset Values"/>
</form>

</body>
</html>

checkServlet.java:
import java.io.*;
import javax.servlet.*;
public class checkServlet extends GenericServlet{

public void service(ServletRequest req, ServletResponse res)throws ServletException,


IOException{ res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
try{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page</title>");
out.println("</head>");
out.println("<body>");

String a1,a2,a3,a4;
a1=req.getParameter("r1");
a2=req.getParameter("r2");
a3=req.getParameter("r3");
a4=req.getParameter("r4");

if((a1.equals("int"))&&(a2.equals("app" ))&&(a3.equals("jdbc"))&&(a4.equals("mic"))){ out.println("<h1>


You are Pass</h1>");
}else{

out.println("<h1>Better luck Next time</h1>");}


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

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

}}}

20
PRN No: Advanced Java Technologies
OUTPUT:

21
PRN No: Advanced Java Technologies
PRACTICAL NO: 4
Aim:: Implement the following JSP applications.
A: Develop a simple JSP application to display values obtained from the use of
intrinsic objects of various types.
INPUT::
Implicit.jsp:
<html><body>

<h1>Use of implicit object in jsp</h1>


<h1>Request Object</h1>

Context Path <%=request.getContextPath()%><br>


Remote Host <%=request.getRemoteHost()%><br>
<h1>Response Object</h1>

Character Encoding Type <%=response.getCharacterEncoding()%><br>


Content Type <%=response.getContentType()%><br>
<h1>Session Object</h1>
ID <%=session.getId()%><br>

Creation Time <%=new java.util.Date(session.getCreationTime())%><br> Last

Access Time <%=new java.util.Date(session.getLastAccessedTime())%>


</body></html>

OUTPUT:

22
PRN No: Advanced Java Technologies
B: Develop a simple JSP application to pass from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
INPUT::
Index.html:
<html>

<head>
<title>JSP Validation</title>

</head>
<body>
<form action="validate.jsp">

Enter Your Name<input type="text" name="name" ><br>


Enter Your Age<input type="text" name="age" ><br>
Select Hobbies: <input type="checkbox" name="hob" value="Singing">Singing

<input type="checkbox" name="hob" value="Reading">Reading Books


<input type="checkbox" name="hob" value="Football">Playing Football<br>

Enter E-mail:<input type="email" name="email" ><br>


Select Gender: <input type="radio" name="gender" value="male">Male

<input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="other">Other<br><br>


<input type="hidden" name="error" value="">

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


<input type="reset" value="Reset">

</form>
</body>

</html>

CheckerBean.java:
package mypack;
public class CheckerBean

{
private String name, age, hob, email, gender,error;

public CheckerBean()

23
PRN No: Advanced Java Technologies
{
error="";

}
public void setName(String n)

{
name=n;

public void setAge(String a)


{

age=a;
}

public void setError(String e)


{ error=e;

public String
getName(){ return name;

public String getAge()


{ return age;

}
public String getError()
{ return error;

public boolean validate()


{ boolean res=true;
if(name==null)

{
error+="<br>Enter First Name"; res=false;

}
if(age==null)

24
PRN No: Advanced Java Technologies
error+="<br>Enter age";
res=false;

}
if(age.length() > 2 )

error+="<br>Age Invalid";
res=false;

}
return res;

validate.jsp:
<%@page import="mypack.*;" %>

<jsp:useBean id="obj" scope="request" class="mypack.CheckerBean">


<jsp:setProperty name="obj" property="*"/>

</jsp:useBean>

<%

if (obj.validate())

{
%>

<jsp:forward page="Successful.jsp"/>
<%

} else {
%>

<jsp:include page="index.html"/>

<%
}

%> <%= obj.getError() %>

25
PRN No: Advanced Java Technologies
Succesful.jsp:
<html>

<body>
<h1>DATA VALIDATED SUCCESSFULLY </h1>

<%
String s1=request.getParameter("name");
out.print(s1);

%>
</body>

</html>

OUTPUT:

26
PRN No: Advanced Java Technologies
C: Create a registration and login JSP application to register and authenticate the
user based on username and password using JDBC.
INPUT::
Index.html:
<html>

<head>
<title>JSP Registration</title>

</head>
<body>
<form action="Register.jsp">

<h1>New User Registration Page</h1>


Enter User Name <input type="text" name="txtName"><br> Enter
Password <input type="password" name="txtPass1"><br>

Re-Enter Password<input type="password" name="txtPass2"><br>


Enter Email<input type="text" name="txtEmail"><br>
Enter Country Name <input type="text" name="txtCon"><br><br>
<input type="submit" value="Register">

<input type="reset" value="Reset">

</form>
</body>
</html>

Register.jsp:
<%@page contentType="text/html" import="java.sql.*"%>

<%
String uname=request.getParameter("txtName");
String pass1 = request.getParameter("txtPass1");
String pass2 = request.getParameter("txtPass2");
String email = request.getParameter("txtEmail");
String ctry = request.getParameter("txtCon");
if(pass1.equals(pass2))

27
PRN No: Advanced Java Technologies
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","system");
PreparedStatement stmt = con.prepareStatement("insert into user values(?,?,?,?)");
stmt.setString(1, uname);
stmt.setString(2, pass1);
stmt.setString(3, email);

stmt.setString(4, ctry);
int row = stmt.executeUpdate();

if(row==1) {
out.println("<h2>Registration Successful</h2>");

else {
out.println("<h2>Registration FAILED!!!! </h2>");

catch(Exception e)
{ out.println(e);

}}

else {
out.println("<h2>Password Mismatch</h2>");

} %>

28
PRN No: Advanced Java Technologies
OUTPUT:

29
PRN No: Advanced Java Technologies
PRACTICAL NO: 5
Aim::Implement the following JSP, JSTL and EL Applications.
A: Create an html page with fields, eno, name, age, desg, salary. Now on submit
this data to a JSP page which will update the employee table of database with
matching eno.
INPUT::
index.html:
<html>

<head>

<title>JSP Employee</title>
</head>

<body>
<form action="Update.jsp">

Emp No.:<input type="text" name="t1"><br>


Emp Name:<input type="text" name="t2"><br>
Emp Age:<input type="text" name="t3"><br>

Emp Salary:<input type="text" name="t4"><br><br>


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

<input type="reset" value="Reset">


</form>

</body>
</html>

Update.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*" %>


<%

String eno=request.getParameter("t1");
String ename=request.getParameter("t2");
String age=request.getParameter("t3"); String
salary=request.getParameter("t4"); try

30
PRN No: Advanced Java Technologies
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp1","root","system");
PreparedStatement stmt=con.prepareStatement("select * from tab where eno=?");
stmt.setString(1,eno);
ResultSet rs=stmt.executeQuery();
if(rs.next())

{
PreparedStatement pst1=con.prepareStatement("update tab set ename=?,age=?,salary=? where eno=?);

pst1.setString(3,salary);
pst1.setString(2,age);
pst1.setString(1,ename);
pst1.setString(4,eno);
pst1.executeUpdate();
out.print("<h1>record updated</h1>");

else{
out.print("<h1> record not found</h1>");

catch(Exception
e){ out.print(e);

} %>

31
PRN No: Advanced Java Technologies
OUTPUT:
Before Update:

After Update:

32
PRN No: Advanced Java Technologies
B: Create a JSP page to demonstrate the use of Expression language.
INPUT::
Index.jsp:
<html>

<body>
<%

session.setAttribute("user","Shubham");
Cookie s=new Cookie("name","Shubh");
response.addCookie(s);

%>
<a href="Expression.jsp">Visit</a>

</body>
</html>

Expression.jsp:
<html>
<body>

<h1>Use of Expression Language</h1><br>

${"Learning Expression"}<br>
Expression1:${1>(4/2)}<br>
Expression2:${(10*10) ne 100}<br>
<h1>Use of implicit object in Expression Language</h1><br>
Expression Language session Scope Object:${sessionScope.user}<br>

Expression Language cookie:${cookie.name.value}


</body>

</html>

33
PRN No: Advanced Java Technologies
OUTPUT:

34
PRN No: Advanced Java Technologies

C: Create a JSP application to demonstrate the use of JSTL.


INPUT::
Function.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<body>
<c:set var="String" value="Welcome-to-tutorials-point"/><h3>
contains:${fn:contains(String,"tutorial")}<br>
indexOf:${fn:indexOf(String,"to")}<br>
replace:${fn:replace(String,"tutorials","javat")}<br>
toLowerCase:${fn:toLowerCase(String)}<br>
toUpperCase:${fn:toUpperCase(String)}<br>
substring:${fn:substring(String,5,11)}<br>
substringAfter:${fn:substringAfter(String,"to")}<br>

Length of the string is:${fn:length(String)}</h3>

</body>
</html>

OUTPUT:

35
PRN No: Advanced Java Technologies
PRACTICAL NO: 6
Aim::Implement the following EJB Applications.
A: Create a Currency Converter application using
EJB. INPUT::
index.html:

<html>
<head>

<title>Currency Converter</title>

</head>
<body>

<form action="CCServlet" >


Enter Amount:<input type="text" name="amt"><br>
Select Conversion Type:

<input type="radio" name="type" value="r2d" checked>Rupees to Dollar


<input type="radio" name="type" value="d2r" >Dollar to Rupees<br><br>

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


<input type="reset" value="RESET">

</form>

</body>
</html>

CCBean.java:
package mybean;
import javax.ejb.Stateless;
@Stateless

public class CCBean

public CCBean() {
}

public double r2Dollar(double r)


{ return r/83.99;

36
PRN No: Advanced Java Technologies
}
public double d2Rupees(double d)

{ return d*83.99;

CCServlet.java:
import mybean.CCBean;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.EJB;
public class CCServlet extends HttpServlet
{ @EJB CCBean obj;

public void doGet(HttpServletRequest request, HttpServletResponse response)throws


ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
double amt = Double.parseDouble(request.getParameter("amt"));
if(request.getParameter("type").equals("r2d")) {

out.println("<h1>"+amt+ " Rupees = "+obj.r2Dollar(amt)+" Dollars</h1>");


}

if(request.getParameter("type").equals("d2r")) {

out.println("<h1>"+amt+ " Dollars = "+obj.d2Rupees(amt)+" Rupees</h1>");


}

}
}

37
PRN No: Advanced Java Technologies
OUTPUT:

38
PRN No: Advanced Java Technologies
B: Develop a Simple Room Reservation System Application using EJB.
INPUT::
index.html:
<html><head>

<title>The Presidential Hotel</title>


</head><body>

<h1>Enter the Details for Room Reservation</h1><br>


<form action="Reserver" method="POST">

Enter Your Name:<input type="text" name="tname"><br>

Enter Your Address:<input type="text" name="tadd"><br>


Enter Your Phone Number:<input type="text" name="tphone" ><br>

Enter Room Type:<select name="troom">


<option id="g">GENERAL</option>

<option id="d">DELUXE</option>
<option id="s">SUITE</option></select><br>

Enter CheckIn Date: <input type="text" name="tin"><br> Enter


CheckOut Date:<input type="text" name="tout"><br> Enter
Payment Mode:<select name="tpay">

<option id="c">CASH</option>
<option id="e">CREDIT CARD</option></select><br><br>

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

<input type="reset" value="RESET">


</form>

</body>
</html>

Reserver.java:
import ejb.Details;
import java.io.*;

import java.util.ArrayList;
import javax.servlet.*;

39
PRN No: Advanced Java Technologies
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.ejb.EJB;
public class Reserver extends HttpServlet

Details d=new Details();


protected void doPost(HttpServletRequest request, HttpServletResponse response)throws
ServletException,

IOException

{
PrintWriter out = response.getWriter();

String cname=request.getParameter("tname");
String cadd=request.getParameter("tadd");
String cph=request.getParameter("tphone");
String croom=request.getParameter("troom");
String chin=request.getParameter("tin"); String
chout=request.getParameter("tout"); String
cmode=request.getParameter("tpay");

ArrayList<String> a=d.reserve(cname,cadd,cph,croom,chin,chout,cmode);
out.println("<html><body><h2>");

try

{
out.println(d.welmsg(cname)+"<br>");
out.println(d.rtype(croom)+"<br>");
out.println(d.roompay(croom)+"<br>");
out.println("<br>----Ur Details <br>");

out.println("<table border=1><tr><th>Name</th><th>Address</th><th>PhoneNo.</th>"
+ "<th>RoomType</th><th>ChechIn Date</th><th>CheckOut Date</th><th>Payment
Mode</th></tr>");
out.println("<tr>");
int i;
for(i=0;i<7;i++){

out.println("<td>" +a.get(i)+ "</td>");

40
PRN No: Advanced Java Technologies
}
out.println("</tr></table>");

}
catch(Exception

e){ out.println(e);

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

}}

Details.java:
package ejb;

import java.util.ArrayList;
public class Details{
public String welmsg(String cust){

return "Hello" + " " + cust.toUpperCase()+ " "+"</br>Welcome to The Presidential Hotel";
}

public String rtype(String rt){


return "You Have Selected" + " " + rt.toUpperCase()+" " +"Room";

public String roompay(String rt)


{

if(rt.equalsIgnoreCase("SUITE")) { return
"You have To Pay 8000Rs Only";

}
else if(rt.equalsIgnoreCase("DELUXE"))
{ return "You have To Pay 4000Rs Only";

}
else {

return "You Have To Pay 2000Rs Only";


}}
public ArrayList<String> reserve(String cust,String add,String ph,String checkin,String
checkout,String rt1,String pyr)

41
PRN No: Advanced Java Technologies
{
ArrayList<String> a=new ArrayList();

a.add(cust);
a.add(add);

a.add(ph);
a.add(checkin);
a.add(checkout);
a.add(rt1);
a.add(pyr);
return a;

}}

OUTPUT:

42
PRN No: Advanced Java Technologies
PRACTICAL NO: 7
Aim::Implement the following EJB Applications with different types of Beans.
A: Develop simple EJB application to demonstrate Servlet Hit count using Singleton
Session Beans.
INPUT::
index.html:
<html>

<head>
<meta http-equiv="Refresh" content="0; URL=ServletClient">

</head
</html>

ServletClient.java:
import ejb.HitsBean; import
java.io.IOException; import
java.io.PrintWriter; import
javax.ejb.EJB;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class

ServletClient extends HttpServlet


{ @EJB

HitsBean counter;

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException

PrintWriter out= res.getWriter();


try

out.print("<h1> number of times this servlet is accessed: </h1>" + counter.getHitCnt());


}

finally
{

43
PRN No: Advanced Java Technologies
out.close();
}

}}

HitsBean.java:
package ejb;

import javax.ejb.Singleton;
@Singleton
public class HitsBean

{
private int hitcnt;

public int getHitCnt()

return hitcnt++;

OUTPUT:

After 4 times refreshing the page:

44
PRN No: Advanced Java Technologies
B: Develop simple Marks Entry Application to demonstrate accessing Database
using EJB.
INPUT::
index.html:
<html>

<head>
<title>Marks Entry Application</title>

</head>
<body>
<form action="MarkEntrySrv" method="get">

Enter Roll No.:<input type="text" name="sno"><br>


Enter Student Name:<input type="text" name="name"><br>
Enter Marks(EJ):<input type="text" name="m1"><br>

Enter marks(AWP):<input type="text" name="m2"><br><br>


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

<input type="reset" value="Reset">


</form>

</body>

</html>

MarkEntrySrv.java:
import ejb.MarksEntry;
import java.io.*; import
javax.ejb.EJB; import
javax.servlet.*;
import javax.servlet.http.*;

public class MarkEntrySrv extends HttpServlet

{ @EJB
MarksEntry bean;

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


IOException,ServletException{

PrintWriter out=res.getWriter();

45
PRN No: Advanced Java Technologies
String id,name,p1,p2;
id=req.getParameter("srno");
name=req.getParameter("name");
p1=req.getParameter("m1");
p2=req.getParameter("m2"); bean.entry(id,
name, p1, p2);
out.print(bean.submit(name));

}}

MarksEntry.java:
package ejb;
import java.sql.*;
import javax.ejb.Stateless;

@Stateless
public class MarksEntry{

public void entry(String srno,String sname,String p1,String p2){ try{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root
","system");
PreparedStatement ps=con.prepareStatement("insert into student values(?,?,?,?)");
ps.setString(1,srno);

ps.setString(2,sname);
ps.setString(3,p1);
ps.setString(4,p2);
ps.executeUpdate();

catch(Exception
e){ System.out.print(e);

}}
public String submit(String name){

return("<h1>"+name.toUpperCase()+ "<br>Record submitted successfully!!</h3>");

}}

46
PRN No: Advanced Java Technologies
OUTPUT:

47
PRN No: Advanced Java Technologies
PRACTICAL NO: 8
Aim::Implement the following Hibernate application.
A: Develop a Hibernate application to store and retrieve employee details in MySQL
Database.
INPUT::
Emp2.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"


"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypack.employee">

<id name="id" column="id">

<generator class="assigned"></generator> </id>


<property name="fname" column="firstname"></property>

<property name="lname" column="lastname"></property></class>


</hibernate-mapping>

Employee.java:
package mypack; public
class employee { private
int id;

private String fname,lname;


public int getId() {

return id; }
public void setId(int id)

{ this.id = id; }

public String getFname()


{ return fname;

}
public void setFname(String fname)
{ this.fname = fname;

48
PRN No: Advanced Java Technologies
public String getLname() { return
lname;

}
public void setLname(String lname)

{ this.lname = lname;

}}

Store.java:
package mypack;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import
org.hibernate.Session;
public class store {

public static void main(String[] args) {


Configuration cfg1=new Configuration().configure("hibernate.cfg.xml");
SessionFactory s=cfg1.buildSessionFactory();

Session s1= s.openSession();


Transaction t=s1.beginTransaction();
employee e=new employee();
e.setId(2);
e.setFname("abc");
e.setLname("xyz");
s1.save(e);

t.commit();
s1.close();

}}

Hybernet.cfg.xml:

⮚ Source:
<hibernate-configuration>
<session-factory>
49
PRN No: Advanced Java Technologies
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3307/mysql?zeroDat
eTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property> <property
name="hibernate.connection.password">system</property> <property
name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.sql_show">true</property>
<mapping resource="emp.hbm.xml"/>
</session-factory>

</hibernate-configuration>

Graphical view of Hybernet.cfg.xml:

OUTPUT:

50
PRN No: Advanced Java Technologies
B: Develop a Hibernate application to store Feedback of Website Visitor in MySQL
Database.
INPUT::
index.html:
<html> <body>
<form action="Store.jsp" method="get">

<table> <tr>
<td>Enter Name:</td>

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


</tr> <tr>

<td>Enter Message:</td>

<td><textarea rows="10" cols="50" name="message"></textarea></td>


</tr> <tr>

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

</tr> </table>

</form>
</body> </html>

FeedBack.java:
package mypack; public
class FeedBack {
private String visitorName;
private String message;

public void setVisitorName(String visitorName)


{ this.visitorName = visitorName;

public void setMessage(String message)


{ this.message = message;

public void setVisitorNo(int visitorNo) {

51
PRN No: Advanced Java Technologies
this.visitorNo = visitorNo;
}

public String getVisitorName()


{ return visitorName;

public String getMessage()


{ return message;

}
public int getVisitorNo()

{ return visitorNo;

private int visitorNo;

Fedd1.hbm.xml:
<hibernate-mapping>

<class name="mypack.FeedBack" >


<id name="visitorNo" column="visitor">

<generator class="increment"></generator>
</id>
<property name="visitorName" column="name"/>

<property name="message" column="feedback"/>


</class>

</hibernate-mapping>

Store.jsp:
<%@page import="java.io.IOException"%>

<%@page import="org.hibernate.*" %>

<%@page import="org.hibernate.cfg.*,mypack.*" %>


<%

SessionFactory sf;
org.hibernate.Session hibSession;

52
PRN No: Advanced Java Technologies
sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
hibSession = sf.openSession();
Transaction tx = null; FeedBack
f = new FeedBack(); try {

tx = hibSession.beginTransaction();
String username = request.getParameter("name");
String usermsg = request.getParameter("message");
f.setVisitorName(username); f.setMessage(usermsg);
hibSession.save(f);
tx.commit();

out.println("Thank You for Your Valuable feedback ");


} catch (Exception e)

{ out.println(e);

hibSession.close();

%>

Hibernet.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql?zeroDat
eTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">hassan11</property>

53
PRN No: Advanced Java Technologies
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<mapping resource="Feed1.hbm.xml"/>
</session-factory>

</hibernate-configuration>

Graphical view of Hybernet.cfg.xml:

OUTPUT:

54

You might also like