0% found this document useful (0 votes)
25 views

Ej Hello

search karne par first number per aaye

Uploaded by

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

Ej Hello

search karne par first number per aaye

Uploaded by

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

Practical 1

1 a) Create a simple calculator application using servlet.

CODE:

Index.html:-
<html>

<head>

<title>Calculator</title>

</head>

<body>

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

Enter First Number:<input type="text" name="num1">

Enter First Number:<input type="text" name="num2">

<br/><br/>

<input type="submit" value="+" name="btn">

<input type="submit" value="-" name="btn">

<input type="submit" value="*" name="btn">

<input type="submit" value="/" name="btn">

</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"})

public class MyCalcServ extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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 action="LoginServlet" method="post">

UserName : <input type="text" name="uname"><br>

Password: <input type="password" name="pw"> <br>

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

</form>

</body>

</html>

LoginServlet.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 = {"/LoginServlet"})

public class LoginServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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="";

if (username.equals("Sumeet Waghmare") && password.equals("Sumeet123")){

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:

MySql Command from mysql software: -

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 method="post" action="LoginValid" >

User Name: <input type="text" name ="un"><br>

Password: <input type="password" name ="pw"><br>

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

</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"})

public class LoginValid extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res)

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

RequestDispatcher rd= req.getRequestDispatcher("/WelcomeServlet");

rd.forward(req, res);

}else{

out.print("Incorrect password");

RequestDispatcher rd= req.getRequestDispatcher("/login.html");

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"})

public class WelcomeServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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

PrintWriter pr = response.getWriter();

String name = (String)request.getAttribute("userName");

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>

<title>Cookie Demo</title> </head>

<body>

<form action="CookiePage1">

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

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

</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"})

public class CookiePage1 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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

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

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

String uname = request.getParameter("txtName");

pr.println("<h1> ~~~Welcome " + uname + "</h1>");

Cookie ck1 = new Cookie("userName", uname);

Cookie ck2 = new Cookie("visit", "1");

response.addCookie(ck1);

response.addCookie(ck2);

pr.println("<h1> <a href=CookiePage2>Click to visit page2</a></h1>");

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"})

public class CookiePage2 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

Sumeet Waghmare
throws ServletException, IOException {

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

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

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

out.println("<html>");

out.println("<head>");

out.println("<title>Page2</title>");

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

out.println("<body bgcolor=yellow>");

Cookie[] cks = request.getCookies();

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

if (cks[i].getName().equals("visit")) {

int count = Integer.parseInt(cks[i].getValue()) + 1;

out.print("<h1>Visit Number: " + count + "</h1>");

cks[i] = new Cookie("visit", count + "");

response.addCookie(cks[i]);

} else {

out.println(cks[i].getName() + "=" + cks[i].getValue());

out.println("<h1><a href=https://www.flipkart.com/>Flipkart</a></h1>");

out.println("<h1><a href=CookiePage3>Click to visit Page3</a></h1>");

out.println("<h1><a href=CookiePage4>Click to visit Page4</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"})

public class CalcVisiteServlet extends HttpServlet {

private int counter;

@Override

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HttpSession session = request.getSession(true);

if (session.isNew()) {

out.print("This is the first time you are visiting this page");

++counter;

} else {

synchronized (this) {

if (counter == 10) {

session.invalidate();

Sumeet Waghmare
counter = 0;

request.getSession(false);

} else {

out.println("You Have visit this page " + (++counter) + " times");

Output:-

Sumeet Waghmare
Practical no :- 3
a. upload file using servlet
<html>

<head>

<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="false">

destination <input type="text"


value="C:\Users\User-14\Documents\NetBeansProjects\WebApplication6"

name="destination">

<br>

<input type="submit" value="upload file" name="upload" id="upload">

</form>

</body>

</html>

FileUploadServlet.java

package fileupload;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;import java.io.InputStream;

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

public class FileUploadServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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

out.print("<br><br><br> file name:"+filename);

OutputStream os=null;

InputStream is=null;

try{

os=new FileOutputStream(new File(path +File.separator +filename));

is=filePart.getInputStream(); int read=0;

while ((read =is.read()) != -1) {

os.write(read);

out.println("<br> file upload successfully.......");

catch(FileNotFoundException e){out.print(e);}

Output:-

Sumeet Waghmare
download file using servlet
<html>

<head>

<meta charset="UTF-8">

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

</head>

<body>

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

<h3> download file here </h3>

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

</form>

</body>

</html>

Download.java

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class download extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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;

FileInputStream inputStream=new FileInputStream(pdfPath + pdfName);

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">

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

</head>

<body>

<form action="ImplicitObj.jsp" method="post">

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

Enter Your Password:<input type="text" name="txtEmail"><br/>

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

</form>

</body>

</html>

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

Sumeet Waghmare
</head>

<body>

<h1>Use of Intrinsic Objects in JSP</h1>

<h1>Request Object</h1>

Query String<%=request.getQueryString()%><br/>

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

Remote Host <%=request.getRemoteHost()%>

<h1>Response Object</h1>

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

Content Type<%=response.getContentType()%><br/>

Locale<%=response.getLocale()%>

<h1>Session Object</h1>

ID<%=session.getId()%><br/>

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

Last Access Time<%=new java.util.Date(session.getLastAccessedTime())%><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>

<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="Validate.jsp">

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

Enter Your Age: <input type="text" name="txtAge"><br>

Select Hobbies: <input type="checkbox" name="chbHob" value="Singing">Singing

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

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

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

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

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

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

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

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

</form >

</body>

</html>

Validate.jsp: -

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

<!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 id="obj" scope="request" class="myPackage.CheckerBean">

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

</jsp:useBean>

<% if (obj.validate()) {%>

<jsp:forward page="Welcome.jsp"/>

<%} else {%>

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

<% }%>

<%= obj.getError()%>

</body>

</html>

CheckerBean.java: -

package myPackage;

public class CheckerBean {

String name, hob, email, gender, error;

int age;

public CheckerBean() {

name = "";

hob = "";

email = "";

gender = "";

error = "";

age = 0;

public void settxtName(String n) {

Sumeet Waghmare
name = n;

public String gettxtName() {

return name;

public void settxtAge(int a) {

age = a;

public int gettxtAge() {

return age;

public void setchbHob(String h) {

hob = h;

error+=h;

public String getchbHob() {

return hob;

public void settxtEmail(String em) {

email = em;

public String gettxtEmail() {

return email;

public void setrdGen(String g) {

gender = g;

public String getrdGen() {

return gender;

public String getError() {

Sumeet Waghmare
return error;

public boolean validate() {

boolean res = true;

if (name.trim().equals("")) {

error += "<br>Enter First Name";

res = false; }

if (age < 0 || age > 99) {

error += "<br>Age Invalide";

res = false;

if(hob.equals("") && gender.contains(""))

error+="<br>Please Select One Hobbie and Gender";

res=false;

String emailregex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";

boolean b = email.matches(emailregex);

if (!b) {

error += "<br>Email Invalid "+b;

res = false;

return res;

Welcome.jsp: -

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

<!DOCTYPE html>

<html>

<head>

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

<title>Welcome JSP</title>

</head>

<body>

<h1>DATA VALIDATED SUCCESSFULLY</h1>

</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:-

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


<html>
<body>
<h1>Registration JSP Page</h1>
<%
String uname = request.getParameter("txtName");
String pass1 = request.getParameter("txtPass1");
String pass2 = request.getParameter("txtPass2");
String email = request.getParameter("txtEmail");
String ctry = request.getParameter("txtCon");
if (pass1.equals(pass2)) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager

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">

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

Enter Password<input type="password" name="txtPass"><br>

<input type="submit" value="~~~LOGIN~~"><input type="reset">

</form>

</body>

</html>

d. Login.jsp:-

Sumeet Waghmare
<%@page contentType="text/html" import="java.sql.*"%>

<html>

<body>

<h1>Registration JSP Page</h1>

<%

String uname = request.getParameter("txtName");

String pass = request.getParameter("txtPass");

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=stmt.executeQuery("select Pass1 from Users where Name ='"+uname+"' ");

rs.next();

if(pass.equals(rs.getString(1)))

out.println("<h1>~~~LOGIN SUCCESSFULLL~~~</h1>");

} else {

out.println("<h1>password does not match!!!!!</h1>");

%>

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

<%

} catch (Exception e) {

out.println("<h1>User does not exist!!!!!</h1>");

%>

<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.*"%>

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

<!DOCTYPE html>

<html>

<head>

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

<title>Employee Salary Update</title>

</head>

<body>

<h1>Updating Employees Salary...</h1>

<%

PrintWriter pw = response.getWriter();

String eid = request.getParameter("txtId");

String slry = request.getParameter("txtSalary");

Sumeet Waghmare
try{

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager .


getConnection("jdbc:mysql://localhost:3306/Employees?autoReconect=true&useSSL=false",

"root", "sumeet");

PreparedStatement slct = con.prepareStatement("select * from salary where EID="+eid);

ResultSet rs = slct.executeQuery();

if(rs.next())

pw.println("<h1>Employee Id: "+rs.getString(1)+" Exits");

PreparedStatement upstmt = con.prepareStatement("update salary set ESalary=? where


EID=?");

upstmt.setString(1, slry);

upstmt.setString(2, eid);

upstmt.executeUpdate();

pw.println("<h1>Employee Record updated !!!!!</h1>");

}else{

pw.println("<h1>Employee Record not exist !!!!!</h1>");

}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>

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

<title>My Cookies</title>

</head>

<body>

<h3>welcome to index page</h3>

<%

session.setAttribute("user", "Sumeet");

%>

<%

Cookie ck = new Cookie("name", "Waghmare");

response.addCookie(ck);

%>

<form action="MyExpressionLanguages.jsp">

Enter Name:<input type="text" name="userName" /><br/><br/>

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

</form> </body>

</html>

b. MyExpressionLanguages.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>

Sumeet Waghmare
<body>

Welcome, ${param.userName}<br/>

Session Value is ${sessionScope.user}<br/>

Name is, ${cookie.name.value}

</body>

</html>

c. ArithemeticOperation: -

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

10*5+6: ${10*5+6} <br>

2.4E4+0.4: ${2.4E4+0.4}<br>

15 mod 4: ${10 mod 4}<br>

15 div 4: ${15 div 3}<br>

</body>

</html>

d. RelationalOpr.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>

Sumeet Waghmare
<body>

<%-- RelationalOperator --%>

<h2>Relational Operator</h2>

15.0==(20-5): ${15.0==(20-5)} <br>

(20-5) eq 15: ${(20-5) eq 15} <br>

((6*50)!= 56): ${((6*50)!= 56)} <br>

(60-4) ne 56): ${(60-4) ne 56} <br>

11>=7: ${11>=7} <br>

11 ge 7: ${11 ge 7} <br>

7<10: ${7<10} <br>

7 lt 10: ${7 lt 10} <br>

4 <= 8: ${4 <= 8} <br>

8 le 4: ${8 le 4}

</body>

</html>

e. ConditionalOpr.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>

<h2>Conditional Operation</h2>

The result of 56>50 is :"${(56>50)?'greater’: ‘lesser'}"

</body>

</html>

f. emptyOpr.jsp: -

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

<!DOCTYPE html>

Sumeet Waghmare
<html>

<head>

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

<title>JSP Page</title>

</head>

<body>

<H1>Empty Operator Example</H1>

The Value for the Empty operator is:: ${empty "txxt"}

</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">

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

</head>

<body>

<a href="setDemo.jsp">Set Demo</a><br/>

<a href="Maxif.jsp">Max If</a><br/>

<a href="foreachDemo.jsp">For each Demo </a><br/>

<a href="outDemo.jsp">Out Demo </a><br/>

<a href="urlDemo.jsp">Url Demo </a><br/>

<a href="choose_when_otherwise.jsp">Choose When Otherwise</a>

</body>

</html>

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

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

<c:set var="pageTitle" value="Dukes Soccer League: Registration" scope="application"/>

${pageTitle}

</body>

Sumeet Waghmare
</html>

Maxif.jsp: -

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

<!DOCTYPE html>

<html>

<head>

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

<title>JSP Page</title>

</head>

<body>

<form action="IFDemo.jsp">

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

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

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

</form>

</body>

</html>

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

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

<c:set var="x" value="${param.x}"/>

<c:set var="y" value="${param.y}"/>

<c:if test="${x>y}">

<font color="blue"><h2>The Ans is :</h2></font>

Sumeet Waghmare
<c:out value="${x} is greater than ${y}"/>

</c:if>

</body>

</html>

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

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

<c:forEach begin="1" end="10" var="i">

The Square of <c:out value="${i}=${i*i}"/><br/>

</c:forEach>

</body>

</html>

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

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

<c:set var="name" value="Sumeet"/>

My Name is :<c:out value="${name}"/>

Sumeet Waghmare
</body>

</html>

urlDemo.jsp: -

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

<!DOCTYPE html>

<html>

<head>

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

<title>Url Demo</title>

</head>

<body>

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

<c:url value="/index.html"/>

</body>

</html>

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

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

<c:set var="Income" value="${4000*4}"/>

Your Income is :<c:out value="${Income}"/>

<c:choose>

<c:when test="${Income <= 1000}">

Income is Not Good

</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">

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>

CCBeanLocal.java

package mybeans;

import javax.ejb.Local;

@Local

public interface CCBeanLocal {

public double r2Dollar(double r);

public double d2Rupees(double d);

CCBean.java

package mybeans;

import javax.ejb.Stateless;

Sumeet Waghmare
@Stateless

public class CCBean implements CCBeanLocal {

public double r2Dollar(double r){

return r/90;

public double d2Rupees(double d){

return d*90;

CCServlet.java

package mybeans;

import java.io.IOException;

import java.io.PrintWriter;

import javax.ejb.EJB;import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class CCServlet extends HttpServlet {

@EJB CCBeanLocal obj;

protected void doGet(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

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

PrintWriter out = response.getWriter();

double amt = Double.parseDouble(request.getParameter("amt"));

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

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

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

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

Sumeet Waghmare
}

OUTPUT:-

Sumeet Waghmare

You might also like