0% found this document useful (0 votes)
9 views87 pages

Java Servlet Registration and Validation

The document outlines a series of Java servlet exercises for a BSC-IT course, including creating a registration servlet that stores user details in a database, validating passwords with a welcome servlet, and using cookies to track user visits and change background colors. It provides detailed source code for HTML forms and Java servlet classes. The exercises are supervised by Mrs. Trupti Rongare and are part of practical assignments for the course.

Uploaded by

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

Java Servlet Registration and Validation

The document outlines a series of Java servlet exercises for a BSC-IT course, including creating a registration servlet that stores user details in a database, validating passwords with a welcome servlet, and using cookies to track user visits and change background colors. It provides detailed source code for HTML forms and Java servlet classes. The exercises are supervised by Mrs. Trupti Rongare and are part of practical assignments for the course.

Uploaded by

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

TY BSC-IT Sem-V Roll No :- 04

Advance Java Name : Ashish Bhandari

C. Create a registration servlet in Java using JDBC. Accept the details


such asUsername, Password, Email, and Country from the user using
HTML Form and store the registration details in the database.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="Registration" method="post">
User name: <input type="text" name="uname"> <br><br>
Password: <input type="password" name="pw"><br><br>
Email Id: <input type="text" name="email"> <br><br>
Country: <select name="coun">
<option>select...
<option> India
<option> Bangladesh
<option> Bhutan
<option> Canada
</select> <br><br>
<input type="submit" value=" Registration">

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

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import [Link].*;
import [Link];
import [Link].*;
import
[Link].*;
import [Link].*;

/**
*
* @author spdc
*/
public class Registration extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();


String name=[Link]("n");
String pass=[Link]("p");
String email=[Link]("e");
String
country=[Link]("c"); try{
[Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost:3306/mysql","root","12345");
PreparedStatement ps=[Link]("insert into register value(?,?,?,?)");
[Link](1,name);

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link](2,pass);
[Link](3,email);
[Link](4,country);
[Link]();
/* TODO output your page here. You may use following sample code. */ [Link]("<!
DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet
Registration</title>"); [Link]("</head>");
[Link]("<body>");
[Link]("<h1>" + " Data Insert Successfully</h1>");
}
catch(Exception e)
{[Link](e);}
[Link]("</body>");
[Link]("</html>");

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left


to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{ return "Short description";
}// </editor-fold>

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.2

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 [Link] page and an error message will be displayed.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<body>
<form method="post" action="Validate">
UserName:<input type="text" name="un"><br><br>
Password:<input type="password" name="pw"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>

[Link]

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import [Link].*;

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

import [Link].*;
import
[Link].*;

/**
*
* @author spdc
*/
public class Validate extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
[Link]("text/html");
PrintWriter out=[Link]();
String
Username=[Link]("un");
String
Password=[Link]("pw");
if([Link]("Servlet"))
{
[Link]("s1username",Username);
[Link]("s1password",Password);
RequestDispatcher rd=[Link]("/Welcome");
[Link](request,response);

}
else
{
[Link]("Incorrect password");
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

RequestDispatcher
rd=[Link]("/[Link]");
[Link](request,response);

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the +


sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

* @return a String containing servlet description


*/
@Override
public String getServletInfo()
{ return "Short description";
}// </editor-fold>

[Link]

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import [Link].*; import


[Link].*;
import [Link].*;

/**
*
* @author spdc
*/
public class Welcome extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try (PrintWriter out = [Link]()) {


String s2username=(String)[Link]("s1username");
String s2password=(String)[Link]("s2password");
[Link]("Welcome "+s2username);
}
}
}

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

B. Create a servlet that uses Cookies to store the number of times a user
has visited a servlet.

Source Code :-
[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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" method="GET">
Click for Visit <input type="Submit" value="Go">
</form>
</body>
</html>

Servlet1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import [Link].*;
import
[Link].*;
import [Link].*;

/**
*
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

* @author spdc
*/
public class Servlet1 extends HttpServlet
{
private int i=1;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
[Link]("text/html");
PrintWriter out = [Link]();
String k=[Link](i); Cookie
c=new Cookie("visit",k);
[Link](c);
int j=[Link]([Link]());
if(j==1)
{
[Link]("This is the first time you are visiting this page");
}
else
{
synchronized([Link])
{
[Link]("You visited this page"+i+"times");
}
}
i++;
}
}
Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

B. Create a Servlet that dynamically changing the color of a page using


cookie

Source Code :

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="ColorServlet" method="post">
<label>Select Background Color</label>
<input type="color" name="bgColor" value="#ffffff">
<input type="submit" value="Apply Color">
</form>
</body>
</html>

[Link]>

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

import [Link].*;
import
[Link].*;
import [Link].*;
/**
*
* @author spdc
*/
public class ColorServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,HttpServletResponse response)


throws IOException, ServletException
{
String bgC =[Link]("bgColor");

Cookie colorCookie=new Cookie("bgColor", bgC);


[Link](60*60*24*7);
[Link](colorCookie);
[Link]("DisplayServlet");
}
}

DisplayServlet>

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import [Link].*;
import
[Link].*;
import [Link].*;
/**
*
* @author spdc
*/
public class DisplayServlet extends HttpServlet {
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bgColor="#ffffff";

Cookie[]
cookies=[Link]();
if(cookies !=null){
for(Cookie cookie: cookies){
if("bgColor".equals([Link]())){
bgColor=[Link]();
break;
}
}
}
[Link]("text/html");
PrintWriter out = [Link]();
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Colored
Page</title>"); [Link]("</head>");
[Link]("<body style='background-color: "+bgColor+ ";' >");
[Link]("<h1>Welcome!</h1>");
[Link]("<p>Your preferred background color has been applied.</p>");
[Link]("<a href='[Link]'>Change Color</a>");
[Link]("</body>");
[Link]("</html>");
}
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

protected void doPost(HttpServletRequest request,HttpServletResponse


response) throws IOException, ServletException{
doGet(request, response);
}
}

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.3

A. Create a Servlet application to upload and download a file.

Uploading file :-

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="FileUploadServlet"
method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="file" id="file">
Destination<input type="text" value="/tmp" name="destination">
<br>
<input type="submit" value="Upload file" name="upload" id="upload">
</form>
</body>
</html>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]

package
fileservletapp; import
[Link].*; import
[Link].*;
import [Link].*;
import
[Link];
import [Link];
@MultipartConfig

/**
*
* @author SPDC
*/
public class FileUploadServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/

public void doPost(HttpServletRequest req, HttpServletResponse res)


throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String path = [Link]("destination");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Part filePart = [Link]("file");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

String
sfilePart=[Link]("file").toString();
[Link]("<br>filePart:"+sfilePart);
String
filename=[Link]().toString();
[Link]("br><br><hr>file name:"+filename);
OutputStream os=null;
InputStream
is=null; try {
os= new FileOutputStream(new File(path + [Link] + filename));
is= [Link]();
int read = 0;
byte[] b= new byte[1024];
while ((read = [Link](b)) != -1)
{
[Link](b,0, read);
}
[Link]("<br>file uploaded sucessfully...!!");
}
catch(FileNotFoundException e){[Link](e);}
}}

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Downloading a file :-

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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>
<h1>File Download Application</h1>
Click <a href="FileDownloadServlet?filename=[Link]">Sample Chapter</a>
<br/><br/>
</body>

[Link]

package
filedownloadapp; import
[Link].*;
import [Link].*;
import
[Link].*;
public class FileDownloadServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

throws ServletException, IOException

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

{ [Link]("APPLICATION/OCTET-STREAM");
String filename = [Link]("filename");
ServletContext context = getServletContext();
InputStream is = [Link]("/" + filename);
ServletOutputStream os = [Link]();
[Link]("Content-Disposition","attachment; filename=\"" + filename +
"\"");
// if comment this statement then it will ask you about the editor with which you want
to open the file
int i;
byte b[]=new byte[1024];
while ((i=[Link](b)) != -1) {
[Link](b);
}
[Link]();
[Link]();
}
}

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

B. Develop Simple Servlet Question Answer Application using

Database. Source Code :-

[Link]
<!DOCTYPE html>
<html>
<head>
<title>Online Exam</title>
</head>
<body>

<center>Online Exam</center>
<form action="Marks" method="post">

<hr>
<div>
1. What is RDBMS?<br>
<input type="radio" name="q1" value="1" required> Relational Database
System<br>
<input type="radio" name="q1" value="2"> Relational<br>
<input type="radio" name="q1" value="3"> Database<br>
<input type="radio" name="q1" value="4"> Redbms<br>
<input type="hidden" name="ans1" value="1">
</div>

<hr>
<div>
2. What is computer?<br>
<input type="radio" name="q2" value="1" required> Brain<br>
<input type="radio" name="q2" value="2"> Electronic device<br>
<input type="radio" name="q2" value="3"> Calculator<br>
<input type="radio" name="q2" value="4"> Mobile<br>
<input type="hidden" name="ans2" value="2">
</div>

<hr>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

<div>
3. Who is joker?<br>
<input type="radio" name="q3" value="1" required> Clown<br>
<input type="radio" name="q3" value="2"> Man<br>
<input type="radio" name="q3" value="3"> Dog<br>
<input type="radio" name="q3" value="4"> Funny man<br>
<input type="hidden" name="ans3" value="1">
</div>

<hr>
<input type="hidden" name="total" value="3">
<input type="submit" value="Submit">

</form>

</body>
</html>

[Link]

package dbapp;

import

[Link].*;
import
[Link].*;
import [Link].*;
import [Link].*;

public class QueAnsDBServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();

try {
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("<html><head><title>Online
Exam</title></head><body>"); [Link]("<form method='post'
action='Marks'>");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("<center><h2>Online Exam</h2></center>");

// Load JDBC driver


[Link]("[Link]"); // Updated for newer MySQL
versions

// Connect to DB
Connection con =
[Link]("jdbc:mysql://localhost:3306/mysql", "root",
"12345"); Statement st = [Link]();

String sql = "SELECT * FROM queans";


ResultSet rs = [Link](sql);

int i = 0;

while ([Link]()) {
i++;
[Link]("<hr>");
[Link]("<b>Q" + [Link](1) + ": " + [Link](2) + "</b><br>");
[Link]("<input type='radio' name='" + i + "' value='1'> " +
[Link](3) + "<br>");
[Link]("<input type='radio' name='" + i + "' value='2'> " +
[Link](4) + "<br>");
[Link]("<input type='radio' name='" + i + "' value='3'> " +
[Link](5) + "<br>");
[Link]("<input type='radio' name='" + i + "' value='4'> " +
[Link](6) + "<br>");

// Store correct answer in hidden field


[Link]("<input type='hidden' name='ans" + i + "' value='" +
[Link](7) + "'>");
}

// Pass total number of questions


[Link]("<input type='hidden' name='total' value='" + i + "'>");
[Link]("<br><input type='submit' value='Submit'>");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("</form></body></html>");

// Close resources
[Link]();
[Link]();
[Link]();

} catch (Exception e) {
[Link]("<p style='color:red;'>ERROR: " + [Link]() + "</p>");
}
}
}

[Link]

package dbapp;

import

[Link].*;
import
[Link].*;
import [Link].*;

public class Marks extends HttpServlet {


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();

try {
[Link]("<html><head><title>Result</title></head><body>");
[Link]("<center><h2>Result</h2></center>");

int total = [Link]([Link]("total")); // Total number of


questions
int marks = 0;

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

for (int i = 1; i <= total; i++) {


// Get user's selected answer for question i

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

String selected = [Link]("q" + i);

// Get correct answer for question i (sent as hidden field)


String correct = [Link]("ans" + i);

if (selected != null && [Link](correct)) {


marks++;
}
}
int totalMarks = marks * 10; // Assuming 10 marks per question
[Link]("<h3>Total Marks: " + totalMarks + " out of " + (total * 10) +
"</h3>");
[Link]("</body></html>");
} catch (Exception e) {
[Link]("<p style='color:red;'>ERROR: " + [Link]() + "</p>");
}
}
}

Output :-

● After select all option correct

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

OR

● If we select option incorrect

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

C. Create a simple Servlet application to demonstrate Non-Blocking


Read Operation.

Source Code :-
[Link]

<html>

<body>

<a href="NonBlockingServlet">Non Blocking Servlet</a>

</body>

</html>

[Link]
package nonblkapp;

import [Link].*;

import [Link];

import

[Link];

import [Link].*;

public class ReadingListener implements ReadListener

{ ServletInputStream input = null;

AsyncContext ac = null;

ReadingListener(ServletInputStream in, AsyncContext c)

{ input = in;

ac = c;

@Override

public void onDataAvailable() {


Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

public void onAllDataRead()

{ [Link]();

public void onError(Throwable t)

{ [Link]();

[Link]();

[Link]
package nonblkapp;

import [Link].*;

import

[Link].*;

import [Link];

import [Link].*;

@WebServlet (name = "ReadingNonBlockingServlet", urlPatterns =

{"/ReadingNonBlockingServlet"},asyncSupported = true )

public class ReadingNonBlockingServlet extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{ [Link]("text/html");

AsyncContext ac = [Link]();

ServletInputStream

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

in=[Link]();

[Link](new ReadingListener(in,ac));

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]:
package

nonblkapp; import

[Link].*;

import

[Link];

import [Link];

import [Link];

import

[Link];

import [Link].*;

import [Link];

import [Link].*;

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

public class NonBlockingServlet extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

[Link]("text/html");

PrintWriter out = [Link]();

String filename = "[Link]";

ServletContext c = getServletContext();

InputStream is = [Link]("/"+filename);

InputStreamReader isr = new InputStreamReader(is);


Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

BufferedReader br = new BufferedReader(isr);

String path = "[Link] + [Link]() + ":" + [Link]() +

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]() + "/ReadingNonBlockingServlet";

[Link]("<h1>File Reader</h1>");

//[Link]();

URL url = new URL(path);

HttpURLConnection hc = (HttpURLConnection) [Link]();

[Link](2); //2bytes at a time

[Link](true); // true if URL connection done

[Link]();

String text =
"";

[Link]("Reading started...");

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter([Link]()));

while ((text = [Link]()) != null)

[Link](text);

[Link]();

[Link](text+"<br>");

[Link]();

try

[Link](1000);

catch (Exception ex)

[Link](ex);

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("Reading completed...");

[Link]();

[Link]();

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.4

A. Develop a simple JSP application to display values obtained from


the use of intrinsic objects of various types.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="[Link]">
Enter your name:<input type="text" name="myname"<br>
Enter your mail:<input type="text" name="mymailid"<br>
<input type="submit" value="submit">
</form>
</body>
</html>

[Link]

<%--
Document : ImplicitObjectEx
Created on : 4 Sep, 2025, 10:21:35
AM Author : spdc

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

--%>
<%@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>
<h1>Use of Intrinsic Objects in JSP</h1>
<h1>Request Object</h1>
Query String<%=[Link]()%><br>
Context Path<%=[Link]()%><br>
Remote Host<%=[Link]()
%><br>

<h1>Response Object</h1>
Character Encoding Type<%=[Link]()
%><br> Content Type<%=[Link]()%><br> Locale<
%=[Link]()%><br>

<h1>Session Object</h1> ID<


%=[Link]()%><br>
Creation Time<%=new [Link]([Link]())%><br>
Last Access Time<%=new [Link]([Link]())
%><br>
</body>
</html>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

B. Develop a simple JSP application to pass values from one page


to another page with validation

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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>
<div>Shopping Cart</div>
<form action="[Link]" method="post">
<table>
<tr>
<td>
Select Product!
</td>
<td><select name="Pname">
<option value="T-shirt"> T-shirt</option>
<option value="shirt"> shirt</option>
<option value="Jeans"> Jeans</option>
<option value="Trouser"> Trouser</option>

</select>
</td>

</tr>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

<tr>
<td>Enter the product quantity:</td>
<td><input type="text" name="t1" value=""/></td>
</tr>
<tr><td>
<input type="submit" value="Click" />

</td></tr>
</table>
</form>
</body>
</html>

[Link]

<%--
Document : Brand
Created on : Sep 4, 2025, 10:52:59
AM Author : SPDC1
--%>

<%@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>
<%
String prd=[Link]("Pname");
int q=[Link]([Link]("t1"));
[Link]("<h1>You have selected an order for"+q+" "+prd+"</h></br>");
[Link]("Pname",prd);
[Link]("qut", q);

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

%>
<form action="[Link]">
<table>
<tr>
<td><select name="brd">
<option value="LEE Copper">LEE Copper</option>
<option value="Zara">Zara</option>
<option value="Nike">Nike</option>
<option value="Ajio">Ajio</option>
<option value="H&M">H&M</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Checkout"/>
</td>
</tr>
</table>
</form>
</body>
</html>

[Link]

<%--
Document : Checkout
Created on : 4 Sep, 2025, 12:32:39
PM Author : spdc
--%>

<%@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>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

</head>
<body>
<%
[Link]("Product Name: "+[Link]("Pname")+"</br>");
[Link]("Product Quantity: "+[Link]("qut")+"</br>");
[Link]("Brand Name: "+[Link]("brd")+"</br>");
%>
</body>
</html>

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.5

A. Create a registration and login JSP application to register and


authenticate the user based on username and password using
JDBC.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>New User Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form Action="[Link]">
<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<select name="txtCon">
<option>India</option>
<option>France</option>
<option>England</option>
<option>Argentina</option>
</select><br>
<input type="submit" value="REGISTER"><input type="reset">

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

</form>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

</body>
</html>

[Link]

<%--
Document : REGISTRATION
Created on : 11 Sep, 2025, 10:47:22
AM Author : spdc
--%>

<%@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>
<%@page contentType="text/html" import="[Link].*"%>
<html><body>
<h1>Registration JSP Page</h1>
<%
String
uname=[Link]("txtName");
String pass1=[Link]("txtPass1");
String pass2=[Link]("txtPass2");
String email=[Link]("txtEmail");
String ctry=[Link]("txtCon");
if([Link](pass2))
{
try
{
[Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost:3306/mysql","root","1234
5");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

PreparedStatement stmt=[Link]("insert into registration4c


values(?,?,?,?)");
[Link](1,uname);
[Link](2,pass1);
[Link](3,email);
[Link](4,ctry);
int
row=[Link]();
if(row==1)
{
[Link]("Registration
Successful");} else
{
[Link]("Registration FAILED!!!!");
%>
<jsp:include page="[Link]"></jsp:include>
<%
}
}catch(Exception e){[Link](e);}
}
else
{
[Link]("<h1>Password Mismatch</h1>");
%>
<jsp:include page="[Link]"></jsp:include>
<% }
%>
</body>
</html>
</body>
</html>

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

and open the template in the editor.


-->
<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>
<div>TODO write content</div>
</body>
</html>

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

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

Source Code :-

[Link]

<html>
<body>
<form action="[Link]" >
Enter Employee Number<input type="text"
name="txtEno" ><br> Enter Salary to update<input
type="text" name="txtSal" ><br>

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

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

[Link]
<%@page contentType="text/html" import="[Link].*" %>
<html>
<body>
<h1>Updating Employee Record</h1>
<%
String
eno=[Link]("txt
Eno"); String sal =
[Link]("txtSal")
; try{

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("[Link]
[Link]"); Connection con =

[Link]("jdbc:mysql://localhost:3306/emp
db","root","tiger"); PreparedStatement stmt =
[Link]("select * from emp where empno=?");

[Link](1, eno);

ResultSet rs =
[Link]();
if([Link]()){

[Link]("<h1> Employee "+[Link](2)+" Exist


</h1>"); PreparedStatement pst=
[Link]("update emp set salary=? where
empno=?");

[Link]
g(1, sal);
[Link]
g(2, eno);
[Link]
Update();

[Link]("<h1>Employee Record updated !!!!!</h1>");

}
else{
[Link]("<h1>Employee Record not exist !!!!!</h1>");

}catch(Exception e){[Link](e);}

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

%>
</body>
</html>

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.6

[Link] a Currency Converter application using EJB.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Currency Converter</title></head>
<body>
<form action="beanServlet">
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>
<input type="reset"><input type="submit" value="CONVERT">

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

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

CC Bean

package MyBean;

import [Link];

/**
*
* @author spdc
*/
@Stateless
public class CCBean implements CCBeanLocal {

public double r2Dollar(double r)


{
return r/65.65;
}
public double d2Rupees(double d)
{
return d*65.65;
}
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")

@Override
public double d2Rupees(double d) {
throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
}

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

CCBeanLocal

package MyBean;
import
[Link];

/**
*
* @author spdc
*/
@Local
public interface CCBeanLocal {

public double r2Dollar(double r);


public double d2Rupees(double d);

CCServlet

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyPack;
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

import [Link].*;

import

[Link].*;
import [Link].*;
import
[Link];
import [Link];

/**
*
* @author spdc
*/
public class beanServlet extends HttpServlet
{ @EJB CCBeanLocal obj;

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/

/* TODO output your page here. You may use following sample code. */
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

throws ServletException, IOException {


[Link]("text/html;charset=UTF-8");

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

double amt = [Link]([Link]("amt"));


try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
if ([Link]("type").equals("r2d"))
{
[Link]("<h1>"+amt+"Rupees ="+obj.r2Dollar(amt)+"Dollar</h1>");
}
if ([Link]("type").equals("d2r"))
{
[Link]("<h1>"+amt+"Dollars ="+obj.d2Rupees(amt)+"Rupees</h1>");
}
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the +


sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{ return "Short description";
}// </editor-fold>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Output :-

● Converting Rupees to Dollar

● Converting Dollar to Rupees

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link] a Simple Room Reservation System Application Using EJB.

Source Code :-

[Link]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Room Reservation</title>
</head>
<body>
<form method="post" action="RoomsClient">
<br> No of Rooms <input type=text name="t1">
<br> <input type="submit" name="btn" value="CheckIN">
<br> <input type="submit" name="btn" value="CheckOUT">

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

BhanduBeans

package MyBeans;
import
[Link];
import [Link].*;
@Stateless
public class BhanduBeans implements BhanduBeansLocal {
@Override
public int checkin(int no) {
try
{
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost:3306/mysql","root","1234
5");
String sql1 = "select * from room";
Statement
st=[Link](); ResultSet
rs=[Link](sql1); [Link]();
int
total=[Link](1);
int occ=[Link](2);
int free=total-occ;
[Link](total);
[Link](free);
if (free>=no)
{
String sql2="update room set occ=?";
PreparedStatement
ps=[Link](sql2); [Link](1,
occ+no);

int
res=[Link]();
return res;
}
else return 0;
}
catch(Exception e)
{
return 0;
}
}
@Override
public int checkout(int no) {
try
{ [Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost/roomdb","root","tiger");
String sql1 = "select * from room";
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Statement
st=[Link](); ResultSet
rs=[Link](sql1);

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]();
int
total=[Link](1);
int occ=[Link](2);
if (occ>=no)
{
String sql2="update room set occ=?";
PreparedStatement
ps=[Link](sql2); [Link](1, occ-
no);

int
res=[Link]();
return res;
}
else return 0;
}
catch(Exception e)
{
return 0;
}
}
}

BhanduBeansLocal
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyBeans;

import

[Link];

@Local
public interface BhanduBeansLocal

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

{
public int checkin(int no);
public int checkout(int no);
}

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Room [Link]
package mypack;
import
[Link];
import [Link].*;
import [Link];
import [Link].*;
import
[Link].*;
import [Link].*;
@WebServlet(name = "roomclient", urlPatterns = {"/roomclient"})
public class RoomsClient extends HttpServlet {
@EJB BhanduBeansLocal obj;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out =
[Link](); try {
int no=[Link]([Link]("t1"));
String b=[Link]("btn");
int res=0;
if([Link]("CheckIN"))
{
res=[Link](no);
//if (res==1)
[Link](no + " rooms check-in");
}
if([Link]("CheckOUT"))
{
res=[Link](no);
//if (res==1)
[Link](no + " rooms check-out");
}
//if(res==0)
[Link]("Not possible to do Check IN / OUT");
[Link]("<br><br><a href=[Link]> Back </a>");
}
finally {

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link]();

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

}
}
}

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.7

[Link] a simple EJB application to demonstrate Servlet Hit count using


Singleton Session Beans.

[Link]

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Refresh" content="0; URL=ServletClient">
</head>
<body>
<div>TODO write content</div>
</body>
</html>

[Link]

package ejb;
import
[Link];
@Singleton
public class CountServletHitsBean
{ private int hitCount;
public synchronized int getCount()
{
return hitCount++;
}
}

[Link]

package servlet;
import [Link];
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

import [Link].*;
import
[Link];
import [Link];
import [Link];
import [Link].*;
@WebServlet(name = "ServletClient", urlPatterns = {"/ServletClient"})
public class ServletClient extends HttpServlet {
@EJB CountServletHitsBean obj;
@Override
protected void service (HttpServletRequest req, HttpServletResponse res) throws
ServletException,
IOException
{
[Link]("text/html");
PrintWriter out=[Link]();
[Link]("<b>Number of times this Servlet is accessed </b>: "+[Link]());
}
}
Output :-

● After Hitting in button of Reload

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

[Link] a simple visitor Statistics application using Message Driven


Bean [Stateless Session Bean].

Web-> web application -> Pract7BVisitorStatisticsMDBApp -> select dedicated


folders for storing libraries -> finish.
Step 1:

Source Code :-
[Link]
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private static ConnectionFactory connectionFactory;
private static Queue queue;
Connection
connection=null; Session
mySession=null;
MessageProducer
messageProducer=null; TextMessage
message=null;

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

%>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
Welcome to My Home Page
<%
try{
InitialContext ic= new InitialContext();
queue=
(Queue)[Link]("jms/Queue");
connectionFactory=(ConnectionFactory)[Link]("jms/QueueFactory");
connection= [Link]();
mySession=[Link](false,
Session.AUTO_ACKNOWLEDGE);
messageProducer=[Link](queue);
message=[Link]();
[Link]([Link]());
[Link](message);
}
catch(JMSException e)
{
[Link]("Exception Occoured "+[Link]());
}
%>
</body>
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

</html>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Step2: Create a Database name visitorstat à Create table name à userstat à


column names

Firstvisitdt – timestamp

Hostname – varchar 30 Primary


Key Visits – int

Step3: Create a Session Bean named as VisitorStatBean à Select Stateless à


package name as ejb, do not select Local / Remote
package ejb;
import
[Link].*;
import
[Link];
import [Link];
import [Link];
@Stateless
public class VisitorStatBean {
private Connection
conn=null; private ResultSet
rs;
private Statement st=null;
private String query =null;
@PostConstruct
public void connect()
{
try {
[Link]("[Link]").newInstance();
conn=[Link]("jdbc:mysql://localhost/visitorstat", "root", "tiger");
}
catch (Exception e) { [Link]([Link]());

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

}
}

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

@PreDestroy
public void disconnect()
{
try {
[Link]();
} catch (Exception e) { [Link]([Link]());
}
}
public void addVisitor(String host)
{
try {
st= [Link]();
query="insert into userstat (hostname,visits) values ('"+host+"','1')";
[Link](query);
}
catch (SQLException e)
{
try {
st=[Link]();
query="update userstat set visits=visits+1 where hostname='"+host+"' ";
[Link](query);
}
catch (SQLException ex) {
[Link]("Cannot Update"+[Link]());
}
}
}
}

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Step 4: Right click on Source Packages à Select Newà Otherà Enterprise Java
Bean

à MessageDrivenBean à EJB Name: BasicMessageBean àPackage: ejbà Select


Project Destination à Click on Add Button à Destination Name: jms/Queue à
Destination Type select the option Queueà click on OKà Click on Next à
Activation Configuration Properties should be as it is. à Click on Finish

package ejb;

import [Link];

import [Link]; import [Link];

import [Link];

import [Link]; import [Link];

import [Link];

import [Link]; import [Link];

@MessageDriven(activationConfig = {

@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue =


"jms/Queue"), @ActivationConfigProperty(propertyName = "destinationType",
propertyValue =

"[Link]")

})

public class BasicMessageBean implements MessageListener { @EJB


VisitorStatBean vs;

@Resource

private MessageDrivenContext mdc; public BasicMessageBean() {

@Override

public void onMessage(Message message) { try {

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

if(message instanceof TextMessage){ TextMessage msg= (TextMessage) message;


[Link]([Link]());

catch (JMSException e) { [Link]();

}
Step 5:

Before deploying and running the application, Glassfish Server setting is


required. Browse the path:

Localhost:4848 on any browser.

Find Resources -> connectors -> Connector Resources double click on


Connector Resources -> click on ‘New’ Button -> write JNDI name as ->
jms/QueryFactory.

Find Admin Object Resources and double click on that -> click on ‘New’ Button
-> write JNDI name as -> jms/Queue.

Now run [Link] file.

Output :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Practical No.8
[Link] a Hibernate application to store Feedback of Website Visitor in
MySQL Database.
Hibernate – Feedback of Website Visitor (on index paper)

Step 1: MySql Command:-

Select Services -> right click on database -> connect -> password -> ok ->again
right click on database -> create database -> db -> ok.

Expand db -> Select and right click table -> click on Execute command ->

Create table guestbook (no int primary key auto_increment, name varchar(20), msg
varchar(100), dt varchar(40));

Step 2: Create a Hibernate Project :-

File -> New Project -> Java Web -> Web application - > Next -> give
the project name -> browse the location as required -> select the
checkbox – “dedicated folder for storing libraries” -> Next
Select glassfish server -> next
Select frame work - hibernate -> select the respective database connection ->
finish.

Step 3: Adding Reverse Engineering File :-

Right click on Project -> new -> other -> select Hibernate -> Hibernate
Reverse Engineering wizard file type -> next -> file name
([Link]) , folder -> click on browse and select src->java -> next
-> select guestbook table name from the available tables option -> click
add ( select the checkbox – include related files) -> finish.

Step 4: Adding Hibernate mapping files and POJOs from Database file type:-

Right click on Project -> new -> other -> select Hibernate -> Hibernate
mapping files and POJOs from Database file type) -> next -> keep the
default configuration file name file name ([Link]) and Hibernate
Reverse Engineering File ([Link]) -> type the package name
(hibernate) -> finish.

Step 5: Creating JSP File :-

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Right click on project -> new -> JSP -> filename -> guestbookview ->
select radiobutton -> JSP file (Standard syntax) -> Finish.

Source Code :-

File name - [Link]

package hibernate;

public class Guestbook implements [Link] { private Integer no;

private String name; private String msg; private String dt; public Guestbook() {

public Guestbook(String name, String msg, String dt) { [Link] = name;

[Link] = msg; [Link] = dt;

public Integer getNo() { return [Link];

public void setNo(Integer no) { [Link] = no;

public String getName() { return [Link];

public void setName(String name)

{ [Link] = name;

public String getMsg() { return [Link];

public void setMsg(String msg) { [Link] = msg;

public String getDt() { return [Link];

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

public void setDt(String dt) { [Link] = dt;

[Link]
<hibernate-configuration>

<session-factory>

<property name="[Link]">[Link]</property>

<property
name="[Link].driver_class">[Link]</property>

<property
name="[Link]">jdbc:mysql://localhost:3306/db</property>

<property name="[Link]">root</property>

<property name="[Link]">tiger</property>

<mapping resource="hibernate/[Link]"/>

</session-factory>

[Link]
<hibernate-mapping>

<class name="[Link]" table="guestbook" catalog="db">

<id name="no" type="[Link]">

<column name="no" />

<generator class="identity" />

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

</id>

<property name="name" type="string">

<column name="name" length="20" />

</property>

<property name="msg" type="string">

<column name="msg" length="100" />

</property>

<property name="dt" type="string">

<column name="dt" length="40" />

</property>

</class>

</hibernate-mapping>

[Link]
<html>

<head>

<title>Guest Book</title>

</head>

<body>

Guest Book <hr><br><br>

<form action="[Link]" >

Name <input type="text" name="name" maxlength="20"><br>

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Message <textarea rows="5" cols="40" maxlength="100" name="msg"></textarea>

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

</form>

</body>

</html>

[Link]

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%!

SessionFactory sf; [Link] ss; List<[Link]> gbook;

%>

<%

sf = new Configuration().configure().buildSessionFactory(); ss= [Link]();

Transaction tx=null;

Guestbook gb=new Guestbook(); try

Teacher Name : [Link] Rongare


TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

tx=[Link]();

String name=[Link]("name"); String


msg=[Link]("msg"); String dt=new [Link]().toString();
[Link](name);

[Link](msg);

[Link](dt); [Link](gb);

[Link]();

catch(Exception e){ [Link]("Error"+[Link]()); } try

{ [Link](); gbook=[Link]("from Guestbook").list();

catch(Exception e){ }

%>

<html>

<head>

<title>Guest View</title>

</head>

<body>

Guest

View

<br><br>

<% Iterator it=[Link](); while([Link]())

{
Teacher Name : [Link] Rongare
TY BSC-IT Sem-V Roll No :- 04
Advance Java Name : Ashish Bhandari

Guestbook eachrecord=(Guestbook)[Link](); [Link]([Link]()+" ");


[Link]([Link]()+"<br>"); [Link]([Link]()
+"<br><hr>");

%>

</body>

</html>

Output :-

Teacher Name : [Link] Rongare

You might also like