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

java

Uploaded by

tanishka zemse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

java

Uploaded by

tanishka zemse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

sPractical No:1

Aim: Creating a Calculator


Code:

Index.html
<!DOCTYPE html>
<!<html>
<head>
<title>Calculator</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action="Calculator71" method="POST">
Enter the First Number: <input type="text" name
="num1"><br>
Enter the Second Number: <input type="text" name
="num2"><br>
Select The Operation: <select name="op">
<option value="Addition">+</option>
<option value="Subtraction">-</option>
<option value="Multiplication">*</option>

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
<option value="Division">/</option>
</select> <br>
<input type="submit" value="Calculate">
</form>
</body>
</html>

.java file
package Prem71;
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 = {"/Calculator71"})
public class Calculator71 extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

String number1=request.getParameter("num1");
String number2=request.getParameter("num2");
String operation=request.getParameter("op");

if(operation.equals("Addition"))
{
out.println("Additon is:"+(Integer.parseInt(number1)+
(Integer.parseInt(number2))));
}

else if(operation.equals("Subtraction"))
{
out.println("Subtration is:"+(Integer.parseInt(number1)-
(Integer.parseInt(number2))));
}
else if(operation.equals("Multiplication"))
{
out.println("Multiplication is:"+
(Integer.parseInt(number1)*(Integer.parseInt(number2))));
}
else

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
{
out.println("Division is:" +(Integer.parseInt(no1) /
Integer.parseInt(no2)));
}
}
}
}

Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No:2

Aim: Creating a servlet for login page


Code:
.html File
<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">
UserName:<input type="text" name="uname"><br>
Password:<input type="password" name="pw"><br>
<input type="submit" value="LOGIN">
</form>
</body>
</html>

.java File

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try(PrintWriter out=response.getWriter()){
String username=request.getParameter("uname");
String password=request.getParameter("pw");
String msg="";
if(username.equals("Tanishka")&& password.equals("Tanishka123"))
msg="Hello"+" "+username;
else
msg="Login failed";

out.println("<b>"+msg+"</b>");
}
}

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No:3
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

<html>
<head>
<title>Register71</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="JdbcServlet71" method="post">
Name:<input type="text" name="uname"><br><br>
Age:<input type="text" name="age"><br><br>
Email:<input type="text" name="email"><br><br>
Country:
<select name="country">
<option>India</option>
<option>Pakistan</option>
<option>China</option>
</select><br><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>

.java

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
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 JdbcServlet71 extends HttpServlet {

/**
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String n=request.getParameter("uname");
String p=request.getParameter("age");
String e=request.getParameter("email");
String c=request.getParameter("country");
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/registration71","ro
ot","root");
PreparedStatement ps=con.prepareStatement("Insert into reg71
values (?,?,?,?)");
ps.setString(1,n);
ps.setString(2,p);
ps.setString(3,e);
ps.setString(4,c);
int i = ps.executeUpdate();
if(i>0)
out.print("You are successfully Registered");
}
catch(Exception e)
{

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
System.out.println(e);
}
}
}
Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No: 4

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.
Program:

Index.html
<html>
<body>
<form method="post" action="ValidateServlet">
User Name: <input type="text" name ="un"><br>
Password: <input type="password" name ="pw"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

ValidateServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ValidateServlet extends HttpServlet
{ public void doPost(HttpServletRequest req, HttpServletResponse res)throw
s IOException, ServletException

{ res.setContentType("text/html");
PrintWriter out=res.getWriter();
String username=req.getParameter("un");
String password=req.getParameter("pw");

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
if(password.equals("Servlet"))
{
req.setAttribute("s1username",username);
req.setAttribute("s1password",password);
RequestDispatcher rd= req.getRequestDispatcher("/WelcomeServlet");
rd.forward(req, res);
}
else
{
out.print("Incorrect password");
RequestDispatcher rd= req.getRequestDispatcher("/index.html");
rd.include(req, res);
}
}
}

WelcomeServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WelcomeServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
try (PrintWriter out = res.getWriter()) {
String s2username = (String)req.getAttribute("s1username");
String s2password = (String)req.getAttribute("s2password");
out.println("Welcome "+s2username);
}
}
}

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No: 5

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

Code:
Index.html
<html>
<head>
<title>Cookie Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="cookies113" method="post">
<input type="submit" value="Go">
</form>
</body>
</html>

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Cookies113.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
public class cookies85 extends HttpServlet {
static int count=1;
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String initialCount=String.valueOf(count);
Cookie myCookie=new Cookie("noOfVisit",initialCount);
response.addCookie(myCookie);
int cookieVal=Integer.parseInt(myCookie.getValue());
if(cookieVal==1)
{
out.println("Welcome");
}
else
{

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
out.println("You visited<b> "+count+"</b> times");
}
count++;
}
}
}
Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No :6

Aim: Develop Simple Servlet Question Answer Application using Database.

Database Creation:
mysql> create database quizdatabase71;
mysql> use quizdatabase71;

Table Creation:
mysql> create table quiz071(qno varchar(5) PRIMARY KEY,question
varchar(50),op1 varchar(5),op2 varchar(5),op3 varchar(5),op4 varchar(5),ans
varchar(50));

Inserting Values:
mysql> insert into quiz071 values('001','Who is Prime Minister of
India','Narendra Modi','Arvind Kejariwal','Amit Shaha','Rahul
Gandhi','Narendra Modi');

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
mysql> insert into quiz071 values('002','Which is Current Trending
Programming language','java','python','kotlin','javascript','python');

mysql> insert into quiz071 values('003','Birth Place of Chatrapati Shivaji


Maharaj?','Raigad','Shivneri','Rajgad','Purandar','Shivneri');

mysql> insert into quiz071 values('004','1st Fort Conquered By Chatrapati


Shivaji Maharaj?','Raigad','Torana','Rajgad','Purandar','Torana');

mysql> insert into quiz071 values('005','Rajyabhishek Place of Chatrapati


Shivaji Maharaj?','Raigad','Shivneri','Rajgad','Kondhana','Raigad');

Displaying Table:

Index.html
<html>
<head>
<title>Quiz</title>
<meta charset="UTF-8">

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Welcome to Quiz Servlet</h1>
<h1><a href="QuizServlet71" >CLICK TO START QUIZ</a></h1>
</body></html>
QuizServlet71.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

public class QuizServlet71 extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<form action=ShowResult >");
try {
Class.forName("com.mysql.jdbc.Driver");

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/quizdatabase71","r
oot","root");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select * from quiz071");
out.println("<table border=1 >");
int qno=0;
while(res.next()){
qno++;
out.println("<tr><td>"+res.getString(1)+"</td>");
out.println("<td>"+res.getString(2)+"</td></tr>");
out.println("<tr><td><input type=radio name="+qno+"
value="+res.getString(3)+"></td><td>"+res.getString(3)+"</td></tr>");
out.println("<tr><td><input type=radio name="+qno+"
value="+res.getString(4)+"></td><td>"+res.getString(4)+"</td></tr>");
out.println("<tr><td><input type=radio name="+qno+"
value="+res.getString(5)+"></td><td>"+res.getString(5)+"</td></tr>");
out.println("<tr><td><input type=radio name="+qno+"
value="+res.getString(6)+"></td><td>"+res.getString(6)+"</td></tr>");
}
}catch(Exception e){out.println(e);}
out.println("</table>");
out.println("<input type=reset >");
out.println("<input type=submit value=SUBMIT >");
out.println("</form>"); } }

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
ShowResult.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

public class ShowResult extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/quizdatabase71","r
oot","root");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select ans from quiz071");
int count =0, qno=0;
while(res.next()){
if(res.getString(1).equals(request.getParameter(""+(++qno))))

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
{ count++;
out.println("<h1>Correct </h1>");
}
else {
out.println("<h1>Incorrect </h1>");
}}
out.println("<h1>Your Score is "+count+" </h1>");}
catch(Exception e){out.println(e);} }}

Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Name: Tanishka Mahesh Zemse Roll No.:113
Class: TY(IT) Div:B
Practical No:7

Aim: Create a JSP page to demonstrate the use of JSTL.


Add jstl-1.2.2.jar file from projects library.

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>
<form action="JstlDemo71.jsp" method="post">
First Name: <input type="text" name="fname"/><br>
Last Name: <input type="text" name="laname"/><br>
<input type="submit" value="check"/>
</form>
</body>
</html>

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
JstlDemo71.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome JSTL Page</title>
</head>
<body>
First Name: <b> <c:out value="${param.fname}"></c:out></b><br/>
Last Name: <b> <c:out value="${param.laname}"></c:out></b><br/>
<br>
Use of if Statement:
<br>
<c:set var="mycount" value="25"/>
<c:if test="${mycount==25}">
<b> <c:out value="Your count is 25"/>
</b>
</c:if><br>
<br>

Use of forEach statement:


<br>
<c:forEach var="count" begin="101" end="105"><b><c:out value="$
{count}"/></b>
</c:forEach><br><br>
Name: Tanishka Mahesh Zemse Roll No.:113
Class: TY(IT) Div:B
Exception catching example:
<br>
<p>
<c:catch var="myException">
<% int number =10/0;%>
</c:catch>
<b> The Exception is: ${myException}</b> </p>
</body>
</html>

Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Practical No: 8

Aim: Create a Currency Converter application using EJB .

index.html

<html>
<head>
<title>Currency Converter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="CCServlet71">

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Enter The Amount
<input type="text" name="amt"><br><br>
<b>Select Conversion Type</b>
<input type="radio" name="type" value="r2d" checked>Rupees to
Dollar
<input type="radio" name="type" value="d2r" >Dollar to Rupees
<br><br>
<input type="reset">&nbsp;&nbsp;<input type="submit"
value="CONVERT">
</form>
</body>
</html>

CCBean.java

package mybeans;
import javax.ejb.Stateless;
@Stateless
public class CCBean implements CCBeanLocal {

public double r2Dollar(double r)


{
return r*82.74;
}
public double d2Rupess(double d)
{

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
return d/82.74;
}
}

CCBeanLocal.java

package mybeans;
import javax.ejb.Local;

@Local
public interface CCBeanLocal
{
public double r2Dollar(double r);
public double d2Rupess(double d);
}

CCServlet71.java

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;

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
import mybeans.CCBeanLocal;

public class CCServlet71 extends HttpServlet {


@EJB CCBeanLocal obj;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
double amt=Double.parseDouble(request.getParameter("amt"));
if(request.getParameter("type").equals("r2d"))
{
out.println("<h1>"+amt+" Rupees="+obj.r2Dollar(amt)+"
Dollars"+"</h1>");
}
else
{
out.println("<h1>"+amt+" Dollar="+obj.d2Rupess(amt)+"
Rupees"+"</h1>");
}
}

Output:

Name: Tanishka Mahesh Zemse Roll No.:113


Class: TY(IT) Div:B
Name: Tanishka Mahesh Zemse Roll No.:113
Class: TY(IT) Div:B

You might also like