Ej Hello
Ej Hello
CODE:
Index.html:-
<html>
<head>
<title>Calculator</title>
</head>
<body>
<br/><br/>
</form>
</body>
</html>
MyCalcServ.java: -
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Sumeet Waghmare
@WebServlet(urlPatterns = {"/MyCalcServ"})
response.setContentType("text/html;charset=UTF-8");
PrintWriter pr = response.getWriter();
int a = Integer.parseInt(request.getParameter("num1"));
int b = Integer.parseInt(request.getParameter("num2"));
int c =0;
String op = request.getParameter("btn");
if(op.equals("+"))
c = a+b ;
}else if(op.equals("-"))
c = a-b;
}else if(op.equals("*"))
c = a*b;
}else{
c = a/b;
pr.println("<br>"+ a+op+b+"="+c+"<br>");
Output: -
Sumeet Waghmare
Sumeet Waghmare
1 b) Create a servlet for a login page. If the username and password are correct then it says
message “Hello ” else a message “login failed”.
CODE:
Index.html :-
<html>
<head>
<title></title>
</head>
<body>
</form>
</body>
</html>
LoginServlet.java: -
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/LoginServlet"})
Sumeet Waghmare
response.setContentType("text/html;charset=UTF-8");
PrintWriter pr = response.getWriter();
String username=request.getParameter("uname");
String password=request.getParameter("pw");
String msg="";
msg="Hello "+username;
else {
msg="Login failed";
pr.println("<b>"+msg+"<b>");
Output: -
Sumeet Waghmare
1 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.
Code:
1. Select services -> expand databases -> right click on MySQL server at
localhost:3306[disconnected] -> click on connect -> enter password (Ajay) ->
OK
2. Again, right click on MySQL server at localhost:3306 -> select Create database -
> enter database name and select the check box to grant permission.
3. Right click on Table under your database
4. Enter table name Users by replacing untitled. Click on Add column, name ->
Name, type-> varchar, size-> 25, select checkbox of primary key, again click on
Add column Email varchar size 25, again click on Add column Password
varchar(25), again click Add column Country varchar(15);
5. add mysql-connector to library folder of the current application
a. Index.html: -
<html>
<head>
<title>Sumeet Registeration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="RegisterServ" method="post">
User name : <input type="text" name="uname"><br/>
Password:<input type="password" name="pw"><br/>
Email Id:<input type="text" name="email"><br/>
Country:
<select name="coun">
<option>select..</option>
<option>India</option>
<option>Bangladesh</option>
<option>Bhutan</option>
Sumeet Waghmare
<option>Canada</option>
</select><br/>
<input type="submit" value="Register">
</form>
</body>
</html>
b. RegisterServ.java: -
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServ extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
Connection con = null;
PreparedStatement ps = null;
String password = request.getParameter("pw");
String username = request.getParameter("uname");
String email = request.getParameter("email");
String country = request.getParameter("coun");
try{
Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/RegisterDB?autoReconnect=true&use
SSL=false","root","sumeet");
Sumeet Waghmare
pw.println("Connection done successfully...");
ps = con.prepareStatement("insert into users values(?,?,?,?)");
ps.setString(1, username);
ps.setString(2, email);
ps.setString(3, password);
ps.setString(4, country);
ps.execute();
pw.println("Data inserted Successfully!!!!");
} catch (ClassNotFoundException ex) {
pw.println(ex);
} catch (SQLException ex) {
pw.println(ex);
Logger.getLogger(RegisterServ.class.getName()).log(Level.SEVERE, null, ex);
}
pw.println("<b>"+"<b>");
}
}
Output:-
Sumeet Waghmare
PRACTICAL 2
Q.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 index.html page and an error message will be
displayed.
Login.html: -
<html>
<head>
<title>Login Validation</title>
</head>
<body>
</form>
</body>
</html>
LoginValid.java: -
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/LoginValid"})
Sumeet Waghmare
throws ServletException, IOException {
res.setContentType("text/html;charset=UTF-8");
PrintWriter out=res.getWriter();
String username=req.getParameter("un");
String password=req.getParameter("pw");
if(password.equals("sumeet"))
req.setAttribute("userName",username);
rd.forward(req, res);
}else{
out.print("Incorrect password");
rd.include(req, res);
WelcomeServlet.java :-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/WelcomeServlet"})
Sumeet Waghmare
response.setContentType("text/html;charset=UTF-8");
PrintWriter pr = response.getWriter();
pr.println("Welcome "+name); }
Output:-
Sumeet Waghmare
b) Create a servlet that uses Cookies to store the number of times a user has visited servlet.
Index.html: -
<html> <head>
<body>
<form action="CookiePage1">
</form>
</body>
</html>
CookiePage1.java : -
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/CookiePage1"})
@Override
response.setContentType("text/html;charset=UTF-8");
pr.println("<!DOCTYPE html>");
pr.println("<html>");
Sumeet Waghmare
pr.println("<head>");
pr.println("<title>Servlet CookieServlet</title>");
pr.println("</head>");
pr.println("<body bgcolor=Blue>");
response.addCookie(ck1);
response.addCookie(ck2);
pr.println("</body>");
pr.println("</html>");
CookiePage2.java : -
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/CookiePage2"})
@Override
Sumeet Waghmare
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Page2</title>");
out.println("</head>");
out.println("<body bgcolor=yellow>");
if (cks[i].getName().equals("visit")) {
response.addCookie(cks[i]);
} else {
out.println("<h1><a href=https://www.flipkart.com/>Flipkart</a></h1>");
out.println("</body>");
out.println("</html>");
Output:-
Sumeet Waghmare
Sumeet Waghmare
2 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.
CODE:
CalcVisiteServlet.java : -
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/CalcVisiteServlet"})
@Override
response.setContentType("text/html");
if (session.isNew()) {
++counter;
} else {
synchronized (this) {
if (counter == 10) {
session.invalidate();
Sumeet Waghmare
counter = 0;
request.getSession(false);
} else {
Output:-
Sumeet Waghmare
Practical no :- 3
a. upload file using servlet
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
name="destination">
<br>
</form>
</body>
</html>
FileUploadServlet.java
package fileupload;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
Sumeet Waghmare
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@MultipartConfig
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String path=request.getParameter("destination");
Part filePart=request.getPart("file");
String filename=filePart.getSubmittedFileName().toString();
OutputStream os=null;
InputStream is=null;
try{
os.write(read);
catch(FileNotFoundException e){out.print(e);}
Output:-
Sumeet Waghmare
download file using servlet
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Download.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html;charset=UTF-8");
Sumeet Waghmare
PrintWriter out=response.getWriter();
String pdfName="try.docx";
String pdfPath="C:\\catch\\";
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment: filename=\"" +pdfName+ "\"");
String pdfpath;
int in;
while((in=inputStream.read()) != -1){
out.write(in);
inputStream.close();
out.close();
Output:-
Sumeet Waghmare
PRACTICAL 4
Q.4 a) Develop a simple JSP application to display values obtained from the use of intrinsic
objects of various types.
CODE:
Index.html:-
<html>
<head>
<title>Implicit Object</title>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
ImplicitObj.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
Sumeet Waghmare
</head>
<body>
<h1>Request Object</h1>
Query String<%=request.getQueryString()%><br/>
Context Path<%=request.getContextPath()%><br/>
<h1>Response Object</h1>
Content Type<%=response.getContentType()%><br/>
Locale<%=response.getLocale()%>
<h1>Session Object</h1>
ID<%=session.getId()%><br/>
</body>
</html>
Output:-
Sumeet Waghmare
Sumeet Waghmare
4 b) Develop a simple JSP application to pass values from one page to another with validations.
(Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
CODE:
Practical4B.html: -
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="Validate.jsp">
</form >
</body>
</html>
Validate.jsp: -
<!DOCTYPE html>
<html>
<head>
Sumeet Waghmare
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Validate JSP</title>
</head>
<body>
<h1>Validation Page</h1>
</jsp:useBean>
<jsp:forward page="Welcome.jsp"/>
<jsp:include page="Practical4B.html"/>
<% }%>
<%= obj.getError()%>
</body>
</html>
CheckerBean.java: -
package myPackage;
int age;
public CheckerBean() {
name = "";
hob = "";
email = "";
gender = "";
error = "";
age = 0;
Sumeet Waghmare
name = n;
return name;
age = a;
return age;
hob = h;
error+=h;
return hob;
email = em;
return email;
gender = g;
return gender;
Sumeet Waghmare
return error;
if (name.trim().equals("")) {
res = false; }
res = false;
res=false;
boolean b = email.matches(emailregex);
if (!b) {
res = false;
return res;
Welcome.jsp: -
<!DOCTYPE html>
<html>
<head>
Sumeet Waghmare
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome JSP</title>
</head>
<body>
</body>
</html>
Output:-
Sumeet Waghmare
4 c) Create a registration and login JSP application to register and authenticate the user
based on username and password using JDBC.
a. index.html: -
<html>
<head>
<title>New User Registration Page</title>
</head>
<body>
<form action="MyRegistration.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:<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">
</form>
</body>
</html>
b. MyRegistration.jsp:-
Sumeet Waghmare
.
getConnection("jdbc:mysql://localhost:3306/LoginDB?autoReconnect=true&useSSL=false",
"root","Ajay");
PreparedStatement stmt = con.prepareStatement("insert into Users 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("Registration Successful");
} else {
out.println("Registration FAILED!!!!");
%>
<jsp:include page="index.html"></jsp:include>
<%
}
} catch (Exception e) {
out.println(e);
}
} else {
out.println("<h1>Password Mismatch</h1>");
%>
<jsp:include page="index.html"></jsp:include>
<% }
%>
</body>
</html>
c. Login.html: -
<html>
<body>
<h1>Login Page</h1>
<form action="Login.jsp">
</form>
</body>
</html>
d. Login.jsp:-
Sumeet Waghmare
<%@page contentType="text/html" import="java.sql.*"%>
<html>
<body>
<%
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/LoginDB?autoReconnect=true&useSS
L=false","root","Ajay");
Statement stmt=con.createStatement();
rs.next();
if(pass.equals(rs.getString(1)))
out.println("<h1>~~~LOGIN SUCCESSFULLL~~~</h1>");
} else {
%>
<jsp:include page="index.html"></jsp:include>
<%
} catch (Exception e) {
%>
<jsp:include page="index.html"></jsp:include>
<%
%>
Sumeet Waghmare
</body>
</html>
Output:-
Sumeet Waghmare
PRACTICAL 5
a) Create an html page with fields, eid, name, age, desg, salary. Now on submit this data
to a JSP page which will update the employee table of database with matching eid.
CODE:
a. Index.html :-
<html>
<head>
<title>Employees Update Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="UpdateEmplo.jsp" method="Post">
Enter Employee Id:<input type="text" name="txtId"><br/>
Enter Salary to Update:<input type="text" name="txtSalary"><br/>
<input type="reset" value="Reset"><input type="submit" value="Submit">
</form>
</body>
</html>
b. UpdateEmplo.jsp: -
<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%
PrintWriter pw = response.getWriter();
Sumeet Waghmare
try{
Class.forName("com.mysql.jdbc.Driver");
"root", "sumeet");
ResultSet rs = slct.executeQuery();
if(rs.next())
upstmt.setString(1, slry);
upstmt.setString(2, eid);
upstmt.executeUpdate();
}else{
}catch(Exception e)
pw.print("Error:"+e.getMessage());
%>
</body>
</html>
Output: -
Sumeet Waghmare
Sumeet Waghmare
b) Create a JSP page to demonstrate the use of Expression language.
CODE:
a. myCookies.jsp: -
<html>
<head>
<title>My Cookies</title>
</head>
<body>
<%
session.setAttribute("user", "Sumeet");
%>
<%
response.addCookie(ck);
%>
<form action="MyExpressionLanguages.jsp">
</form> </body>
</html>
b. MyExpressionLanguages.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
Sumeet Waghmare
<body>
Welcome, ${param.userName}<br/>
</body>
</html>
c. ArithemeticOperation: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
2.4E4+0.4: ${2.4E4+0.4}<br>
</body>
</html>
d. RelationalOpr.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
Sumeet Waghmare
<body>
<h2>Relational Operator</h2>
11 ge 7: ${11 ge 7} <br>
8 le 4: ${8 le 4}
</body>
</html>
e. ConditionalOpr.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h2>Conditional Operation</h2>
</body>
</html>
f. emptyOpr.jsp: -
<!DOCTYPE html>
Sumeet Waghmare
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</body>
</html>
OUTPUT:-
Sumeet Waghmare
Sumeet Waghmare
c) Create a JSP application to demonstrate the use of JSTL (JSP Standard Tag Library).
CODE:
Index.html: -
<html>
<head>
<title>JSTL Tags</title>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
setDemo.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
${pageTitle}
</body>
Sumeet Waghmare
</html>
Maxif.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action="IFDemo.jsp">
</form>
</body>
</html>
IFDemo.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<c:if test="${x>y}">
Sumeet Waghmare
<c:out value="${x} is greater than ${y}"/>
</c:if>
</body>
</html>
foreachDemo.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</c:forEach>
</body>
</html>
outDemo.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
Sumeet Waghmare
</body>
</html>
urlDemo.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>Url Demo</title>
</head>
<body>
<c:url value="/index.html"/>
</body>
</html>
choose_when_otherwise.jsp: -
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<c:choose>
</c:when>
Sumeet Waghmare
<c:when test="${Income>1000}">
Income is Good
</c:when>
<c:otherwise>
Income is Undetermined
</c:otherwise>
</c:choose>
</body>
</html>
OUTPUT:-
Sumeet Waghmare
Sumeet Waghmare
Practical 6
a.Currency convertor application using EJB
Index.html
<head>
<title>CC</title>
</head>
<body>
<form action="CCServlet">
Dollar
</form>
</body>
</html>
CCBeanLocal.java
package mybeans;
import javax.ejb.Local;
@Local
CCBean.java
package mybeans;
import javax.ejb.Stateless;
Sumeet Waghmare
@Stateless
return r/90;
return d*90;
CCServlet.java
package mybeans;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response)
response.setContentType("text/html;charset=UTF-8");
if(request.getParameter("type").equals("r2d")){
out.println("<h1>"+amt+"Rupees="+obj.r2Dollar(amt)+"Dollar</h1>");
if(request.getParameter("type").equals("d2r")){
out.println("<h1>"+amt+"Dollars="+obj.d2Rupees(amt)+"Rupees</h1>");
Sumeet Waghmare
}
OUTPUT:-
Sumeet Waghmare