Ej Prac
Ej Prac
Practical No. 1
2. Select ‘Java Web’ in Categories and ‘Web Application’ in Projects. Click ‘Next’.
4. Click ‘Next’.
5. Click ‘Finish’.
Code:
Index.html
<html>
<head>
<title>CALCULATOR</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> Simple Calculator</h1>
<form action="SimpleCal">
Enter Number1:<input type="text" name="txtnum1"> <br><br>
Enter Number2:<input type="text" name="txtnum2"> <br><br>
<input type="radio" name="opr" value="+">Addition<br>
<input type="radio" name="opr" value="-">Subtraction<br>
<input type="radio" name="opr" value="*">Multiplication<br>
<input type="radio" name="opr" value="/">Division<br> <br>
<input type="reset" value="clear">
<input type="submit" value="Calculate">
</form> </body> </html>
SimpleCal.java:
package DemoCal;
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;
Output:
Part B.
Aim: Create a servlet for a login page. If the username and password are
correct then it says message “Hello ” else a message “login failed”.
Steps for creating project and creating servlets are the same as Practical 1 Part A.
Names Given:
Project Name:Prac1B
Package Name: demoServlet
Java File: LoginServlet.java
Code:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Login page </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="LoginServlet">
Enter Username: <input type="text" name="txtUserName"> <br> <br>
Enter Password: <input type="password" name="txtPassword"> <br> <br>
<input type="submit" value="Login"> <input type="reset" value="Clear">
</form> </body> </html>
LoginServlet.java:
package demoServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uname, password;
uname=request.getParameter("txtUserName");
password=request.getParameter("txtPassword");
if(uname.equals("Dipsha") && password.equals("dipsha123"))
{ out.println("Welcome "+uname); }
else
{ out.println("Login Failed!"); }
}}
Output:
Valid
Invalid:
Part C:
Aim: 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.
Code:
Index.html:
<!DOCTYPE html>
<html>
<head>
<title>Practical 1 C</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Welcome to Registration page</h1><br>
<form action="EmpAppServlet" method="post">
Enter User Name:<input type="text" name="USN"><br>
Enter Password:<input type="password" name="PASS"><br>
Enter Email:<input type="text" name="EMAIL"><br>
Enter Country:<input type="text" name="CON"><br>
<input type="reset">
<input type="submit" value="REGISTER">
</form> </body> </html>
EmpAppServlet.java:
package demoDB;
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 java.sql.*;
public class EmpAppServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String UserN=request.getParameter("USN");
String pass=request.getParameter("PASS");
String email=request.getParameter("EMAIL");
String country=request.getParameter("CON");
Try {
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeA","root","root");
PreparedStatement pst=con.prepareStatement("insert into user values(?,?,?,?)");
pst.setString(1,UserN);
pst.setString(2,pass);
pst.setString(3,email);
pst.setString(4,country);
int row=pst.executeUpdate();
if(row==1)
{ out.println("Data inserted successfully"); }
else
{ out.println("Data could not be inserted"); }
}
Dipsha Poojary- Roll No. 49
TY IT Sem 5 Enterprise Java
catch(Exception e)
{ out.println(e);
} } }
MySQL:
i. Add the jar/folder:
Output:
Practical No. 2
Part A:
AIM: 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.
Files Used:
CODE:
Index.html:
<!DOCTYPE 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="LoginServlet" method="POST">
<br> <hr> <br>
Enter USERNAME:<input type="text" name="txtUname"><br> <br>
Enter PASSWORD:<input type="password" name="txtPwd"><br>
<br> <hr> <br>
<input type="submit" value="Login">
<input type="reset" value="Reset">
<br> <hr> <br>
</form> </body> </html>
LoginServlet.java:
package demoLogin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
} }}
Welcome.java:
package demoLogin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Welcome extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name=request.getParameter("txtUname");
PrintWriter out = response.getWriter();
out.println("<h1><b>WELCOME "+name+"</b></h1>");
}
}
OUTPUT:
Part B:
AIM: Create a servlet that uses Cookies to store the number of times a user
has visited
servlet.
Files Used:
CODE:
INDEX:
<!DOCTYPE html> <html> <head>
<title>Cookies demo </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body> <form action="Servlet1" >
Enter Your Name <input type="text" name="txtName"><br>
<input type="submit" value="~~~ Click to Enter ~~~">
</form> </body> </html>
servlet1:
package demoCookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
Servlet2:
package demoCookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page2</title></head>");
out.println("<body bgcolor=yellow >");
Cookie [] ck = request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("Visit"))
{
int count = Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No : "+count+"</h1>");
ck[i] = new Cookie("Visit",count+"");
out.println(ck[i].getName()+ck[i].getValue());
response.addCookie(ck[i]);
}
else
{ out.println(ck[i].getName()+ " = "+ck[i].getValue());
}}
out.println("<h1><a href=Servlet3 >Click to visit Page 3 </a></h1>");
out.println("<h1><a href=Servlet4 >Click to visit Page 4 </a></h1>");
out.println("<h1><a href=Servlet5 >Click to visit Page 5 </a></h1>");
out.println("</body>");
out.println("</html>");
} }
servlet3:
package demoCookie;
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 javax.servlet.http.Cookie;
public class Servlet3 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page3</title></head>");
out.println("<body bgcolor=pink>");
Cookie [] ck = request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("Visit"))
{
int count = Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No : "+count+"</h1>");
ck[i] = new Cookie("Visit",count+"");
out.println(ck[i].getName()+ck[i].getValue());
response.addCookie(ck[i]);
}
else
{ out.println(ck[i].getName()+ " = "+ck[i].getValue());
} }
out.println("<h1><a href=Servlet2 >Click to visit Page 2 </a></h1>");
servlet4:
package demoCookie;
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 javax.servlet.http.Cookie;
public class Servlet4 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page4</title></head>");
out.println("<body bgcolor=FFECAF >");
Cookie [] ck = request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("Visit"))
{
int count = Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No : "+count+"</h1>");
ck[i] = new Cookie("Visit",count+"");
out.println(ck[i].getName()+ck[i].getValue());
response.addCookie(ck[i]);
}
else
{ out.println(ck[i].getName()+ " = "+ck[i].getValue());
} }
out.println("<h1><a href=Servlet3 >Click to visit Page 3 </a></h1>");
out.println("<h1><a href=Servlet2 >Click to visit Page 2 </a></h1>");
out.println("<h1><a href=Servlet5 >Click to visit Page 5 </a></h1>");
out.println("</body>");
out.println("</html>");
}}
servlet5:
package demoCookie;
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 javax.servlet.http.Cookie;
public class Servlet5 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
OUTPUT:
Part C:
AIM: 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.
CODE:
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="Page1Serv" method="get">
Enter Username: <input type="text" name="txtName"><br/>
<input type="submit" value="Submit">
</form> </body> </html>
Page1Serv:
package demoSession;
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 javax.servlet.http.HttpSession;
import java.util.Date;
} }
Page2Serv:
package demoSession;
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 javax.servlet.http.HttpSession;
public class Page2Serv extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page 2</title></head>");
out.println("<body bgcolor=FFCACC>");
HttpSession hs1 = request.getSession(false);
out.println("<h1>Welcome to Page No. 2</h1>");
int visit = Integer.parseInt((String)hs1.getAttribute("visit"))+1;
out.println("<h1>You have visited "+visit+" times</h1>");
hs1.setAttribute("visit", ""+visit);
out.println("<h1>Your Session ID "+hs1.getId()+"</h1>");
out.println("<h1>You Logged in at "+new java.util.Date(hs1.getCreationTime()) +"</h1>");
out.println("<h1><a href=Page1Serv>Click for Page 1</a></h1>");
out.println("<h1><a href=Page3Serv>Click for Page 3</a></h1>");
out.println("<h1><a href=LogoutServlet>Click to Logout</a></h1>");
} }
Page3Serv:
package demoSession;
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 javax.servlet.http.HttpSession;
public class Page3Serv extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page 3</title></head>");
out.println("<body bgcolor=DBC4F0>");
HttpSession hs1 = request.getSession(false);
out.println("<h1>Welcome to Page No. 3</h1>");
int visit = Integer.parseInt((String)hs1.getAttribute("visit"))+1;
out.println("<h1>You have visited "+visit+" times</h1>");
hs1.setAttribute("visit", ""+visit);
out.println("<h1>Your Session ID "+hs1.getId()+"</h1>");
out.println("<h1>You Logged in at "+new java.util.Date(hs1.getCreationTime()) +"</h1>");
out.println("<h1><a href=Page1Serv>Click for Page 1</a></h1>");
out.println("<h1><a href=Page2Serv>Click for Page 2</a></h1>");
out.println("<h1><a href=LogoutServlet>Click to Logout</a></h1>");
} }
LogoutServlet:
package demoSession;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogoutServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
javax.servlet.http.HttpSession hs = request.getSession();
if(hs != null)
{
hs.invalidate();
}
out.println("<h3>You are now logged out....To Re-join, kindly login again</h3>");
}}
OUTPUT:
Practical No. 3
Implement the Servlet IO and File applications.
Part A:
AIM: Create a Servlet application to upload file.
Files Used:
CODE:
OUTPUT:
Part B:
AIM: Create a Servlet application to download files.
Files Used:
STEPS:
CODE:
OUTPUT:
Part C:
AIM: Create simple Servlet application to demonstrate Non-Blocking Read
Operation.
Files Used:
STEPS:
1. Create a text file ‘testin’ in D drive.
2. To check output go to D drive. File ‘testout’ will be created. It will have same content as
‘textin’.
CODE:
Index.html:
<!DOCTYPE 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>
<div>TODO write content</div>
</body> </html>
NIOservlet:
package demoNIO;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.*;
import java.nio.channels.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NIOservlet extends HttpServlet {
public static void main(String args[]) throws IOException
{
FileInputStream input = new FileInputStream ("D:\\testin.txt");
ReadableByteChannel source = input.getChannel();
FileOutputStream output = new FileOutputStream ("D:\\testout.txt");
WritableByteChannel destination = output.getChannel();
copyData(source, destination);
source.close();
destination.close();
}
private static void copyData(ReadableByteChannel src, WritableByteChannel dest) throws
IOException
{
ByteBuffer buffer = ByteBuffer.allocateDirect(20 * 1024);
while (src.read(buffer) != -1)
{
buffer.flip();
while (buffer.hasRemaining())
{ dest.write(buffer);
}
buffer.clear();
System.out.println("hello done");
} } }
OUTPUT:
Practical No. 5
Implement the following JSP JSTL and EL Applications.
Part A:
AIM: 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.
Files Used:
CODE:
OUTPUT:
Part B:
AIM: Create a JSP page to demonstrate the use of Expression language.
Files Used:
CODE:
Index.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>
<h1>Welcome to MY page</h1>
<% Cookie ck=new Cookie("color","powder blue"); response.addCookie(ck);%>
<% application.setAttribute("name","Dipsha");
session.setAttribute("country","India"); %>
<form action="ExpLang.jsp">
Enter Name:<input type="text" name="name" /><br/><br/>
<input type="submit" value="Submit"/>
</form> </body> </html>
ExpLang.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>
<h1>Accessing Attribute: Application Level </h1>
Welcome ${ applicationScope.name }
<h1>Accessing Attributes: Session Level </h1>
Country of Residence: ${ sessionScope.country}
<h1>Accessing Cookie </h1>
Cookie Color has Value: ${cookie.color.value}
<%-- arithmetic operators --%>
<h1> Arithmetic Operators </h1>
OUTPUT:
Part C:
AIM: Create a JSP application to demonstrate the use of JSTL.
Files Used:
CODE:
index.html
<!DOCTYPE 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 ="JSTLDemo.jsp">
User First Name:<input type="text" name="ufname"><br/> <br/>
User Last Name:<input type="text" name="ulname"><br/> <br/>
Enter a Number: <input type="number" name="input"/><br> <br/>
<input type="submit"><br/>
</form> </body> </html>
JSTLDemo.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
OUTPUT:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="CCServlet">
select conversion:<br>
<input type="reset">
</form>
</body>
</html>
CCServlet:
package demoS;
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 javax.ejb.EJB;
import mybean.CCBean;
response.setContentType("text/html;charset=UTF-8");
double amt=Double.parseDouble(request.getParameter("amt"));
if(request.getParameter("type").equals("r2d"))
if(request.getParameter("type").equals("r2e"))
if(request.getParameter("type").equals("d2r"))
if(request.getParameter("type").equals("d2e"))
if(request.getParameter("type").equals("e2r"))
if(request.getParameter("type").equals("e2d"))
CCBean:
package mybean;
import javax.ejb.Stateless;
@Stateless
return r/83.8;
return r/90.65;
return d*83.8;
return d*83.8;
3. Click on Finish
6b:
<html>
<head>
<title>practical 6B </title>
<meta charset="UTF-8">
</head>
<body>
<form action="CounterServlet">
</form>
</body>
</html>
CCServlet.java
package demoS;
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 javax.ejb.EJB;
import mybean.CounterBean;
response.setContentType("text/html;charset=UTF-8");
CounterBean.
package mybean;
import javax.ejb.Singleton;
@Singleton
hitcount=hitcount+1;
return hitcount;