Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 85

T.Y.BSc.

IT(Sem-V) Roll No:893

Practical No.1

Topic: Implement the following Simple Servlet applications.

A] Aim: Create a simple calculator application using servlet.

Source Code:

Steps to create project:

1) Click on File and click on New Project.

2) Below Window appears then select Java Web in Categories section and select
Web Application in Projects section and click on Next.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

3) Enter your Project Name and you can also change your Project Location if
necessary and then click Next.

4) Keep the Settings as it is and click on Next.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

5) Keep the Frameworks Settings as it is and click on Next.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

index.html
<html>
<body>
<form action="" method="post">
<b> Enter Number1</b> <input type="number" name="N1"><br/><br/>
<b> Enter Number2</b> <input type="number" name="N2"><br/><br/>
Select operation <select name ="Cal">
<option value="Add"> + </option>
<option value="Subtract"> - </option>
<option value="Divide"> / </option>
<option value="Multiply"> *</option>
<option value="Mod"> % </option>
</select>
<input type="submit" value="Calculate">
</form>
</body>
</html>

Steps to create servlet:

1)Right click on the working Project and click on New and then click on
Servlet.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

2) Enter Class Name and Package according to your preference and click on Next.

3) Select the Checkbox and click on Finish.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

CalculatorServlet.java

package pck1;

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 CalculatorServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

PrintWriter out = response.getWriter() ;


int num1=Integer.parseInt(request.getParameter("N1"));
int num2=Integer.parseInt(request.getParameter("N2"));
String operation=(request.getParameter("Cal"));

if(operation.equals("Add"))
{
out.println("Addition of num1 & num2: " + (num1+num2));
}
else if(operation.equals("Subtract"))
{
out.println("Subtraction of num1 & num2: " + (num1-num2));
}
else if(operation.equals("Divide"))
{
out.println("Division of num1 & num2: " + (num1/num2));
}
else if(operation.equals("Multiply"))
{
out.println("Multiplication of num1 & num2: " + (num1*num2));
}
else if(operation.equals("Mod"))
{
out.println("Mod of num1 & num2: " + (num1%num2));
}
else
{
out.println("please select the operation");

}
}
}

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: create a servlet for login page if username and password are correct then it says
message “Hello” <username> else a message login failed]

index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="LoginServlet" method="post">
Enter Username:<input type="text" name="t1" placeholder="username"><br/><br/>
Enter Password:<input type="password" name="t2" placeholder="***********"
><br/><br/>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

<input type="submit" value="Click to Login" class="btn">


<input type="reset" value="Reset" class="btn">
</form>
</body>

LoginServlet.java

package pck2;

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 username = request.getParameter("t1");
String password = request.getParameter("t2");

if(username.equals(“Nitesh") && password.equals("00000"))


{
out.println("Hello " + username);
}
else
{
out.println("Login Failed");
}

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

}
}
Output:

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 database

index.html

<!DOCTYPE html>

<html>
    <head>
        <title> Employee Application</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="s1">
            Enter Username: <input type="text" name="t1" placeholder="Name"><br/>
            Enter Password: <input type="password" name="t2" placeholder="9886***"><br/>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

            Enter Email-id: <input type="email" name="t3"


placeholder="example@gmail.com"><br/>
            Enter Country: <input type="text" name="t4" placeholder="Country"><br/>
            <input type="submit" value="Register">
            <input type="reset" value="Reset">
            
             </form>
    </body>
</html>
s1.java
package pkgg;
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 s1 extends HttpServlet {

 protected void service(HttpServletRequest request, HttpServletResponse response)


            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String n=request.getParameter("t1");
        String p=request.getParameter("t2");
        String eid=request.getParameter("t3");
        String c=request.getParameter("t4");
        
        try
        {
            Class.forName("com.mysql.jdbc.Driver");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

            Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/b2_registrationdetails","roo
t","Amar@0000");
            PreparedStatement pst=con.prepareStatement("insert into b2_user value (?,?,?,?)");
            pst.setString(1, n);
            pst.setString(2, p);
            pst.setString(3, eid);
            pst.setString(4, c);
             int row=pst.executeUpdate();
             if (row==1)
             {
                 out.println("record inserted successfully");
             }
             else
             {
            
              out.println("not inserted successfully");
             }
             con.close();
             pst.close();
             
              }
        catch (Exception e){
            out.println(e);
                    }
         } }
Steps for connecting database(//(Add Screenshot for each step)

1) Open the NetBeans IDE click on Windows and click on Services.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

2) Under Services Section expand the Database section and right click the MySQL
Server at localhost:3306 to connect.

3) Click on Connect.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

4) Enter the Password to connect with Database.

5) Expand MySQL Server at localhost:3306 and right click on the database you
created and click on Connect.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

6) Now you can find jdbc:mysql://localhost:3306/empRegistrationB2 below where


you can also find the database you created.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

7) After expanding the database, you can see your table under Tables section right
click your table name and click on View Data to view the table.

8) You can now view the created table.

9) Now come back in the Project section and right click on Libraries and click on Add
JAR Folder.

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

10) Select the MySQL Connector file from appropriate folder.

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ff

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No. 2

Topic: Implement the following servlet application with cookies and session

A] Aim: using request dispatcher user interface create a servlet which will validate the
password enter by the user ,if the user has enter “ servlet” as password ,then he will be
forwarded to welcome servlet else the user will stay on the index.html page and error
message will be display]

index.html

<!DOCTYPE html>
<html>
<head>
<title>LOGIN</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="post">
Enter Username:<input type="text" name="t1" placeholder="username"><br/><br/>
Enter Password:<input type="password" name="t2" placeholder="***********"
><br/><br/>
<input type="submit" value="Click to Login" class="btn">
<input type="reset" value="Reset" class="btn">
</form>
</body>

s1.java
package pkgc;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class s1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String psw = request.getParameter("t2");
if(psw.equals("servlet"))
{
RequestDispatcher rd = request.getRequestDispatcher("s2");
rd.forward(request, response);
}
else
{
RequestDispatcher pd = request.getRequestDispatcher("index.html");
pd.include(request, response);

out.println("<h1 style=\"color:red;\">"+"Password incorrect please try again


"+"</h1>");
}
}
}
s2.java
package pkgc;
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 s2 extends HttpServlet {

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

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


out.println("Welcome " +uname);

}
}
Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: Create a servlet that uses cookies to store the number of times a user has visited
servlet

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="p1" method="post">
Enter Username: <input type="text" name="t1" placeholder="username"><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>

p1.java
package pack;
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 p1 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 1</title></head>");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

out.println("<body bgcolor=cyan>");
String un = request.getParameter("t1");
out.println("<h1> Welcome </t>" + un + "</h1>");
Cookie ck1 = new Cookie("Username",un);
Cookie ck2 = new Cookie("Visit","1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<a href=P2>Click here to Visit P2</a>");
}
}
p2.java
package pack;
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.Cookie;
@WebServlet(urlPatterns = {"/P2"})
public class P2 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>");
Cookie []ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("visit"))
{
if(Integer.parseInt(ck[i].getValue())<=1)

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

{
out.println("<body bgcolor= red>");
int count = Integer.parseInt(ck[i].getValue())+1;
out.println("<b>Visit "+ count +"</b>");
ck[i]=new Cookie("visit",count+" ");
}
response.addCookie(ck[i]);
}
else
{
out.println("<body bgcolor= green>");
out.println("<i>u have visited this page before</i>");
}
}
out.println("<a href=P3>click here to visit p3</a>");

}
}
p3.java
package pack;
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;
import javax.servlet.http.Cookie;
@WebServlet(urlPatterns = {"/P3"})
public class P3 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page 3</title></head>");
Cookie []ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("Visit"))
{
out.println("<body bgcolor=yellow>");
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<b>Visit"+count+"</b>");
ck[i]=new Cookie("Visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println(ck[i].getName()+"="+ck[i].getValue());
out.println("<body bgcolor=green>");
out.println("<a href=P2>Click Here To Visit Pg2</a>");
}
}

}
}

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

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

index.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="p1" method="post">
Log In:<input type="text" name="t1" placeholder="username"><br/><br/>
<input type="submit" value="Submit" class="btn">
</body>
</html>
p1.java
package pkgc;
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;
public class p1 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 1 </title></head></html>");


HttpSession ob1 = request.getSession(true);

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

if(ob1.isNew())
{
out.println("<body bgcolor=yellow>");
String uname = request.getParameter("t1");
ob1.setAttribute("Username", uname);
ob1.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");
int visit = Integer.parseInt((String)ob1.getAttribute("visit"))+1; //Here we convert
string to integer
out.println("<h1>Welcome back "+ob1.getAttribute("Username")+" </h1>");
out.println("<h2>Visit : "+visit+"</h2>");
ob1.setAttribute("visit", visit+"");
}
out.println("<h2>Session Id : "+ob1.getId()+"<br> Timeout:
"+ob1.getMaxInactiveInterval()+" </h2>");
out.println("<h2>Last Accessed Time:"+ob1.getLastAccessedTime()+"<br> Creation
Time:"+new Date(ob1.getCreationTime())+" </h2>");
out.println("<a href=p2>CLICK HERE TO VISIT PAGE 2 </a>");
out.println("<br><a href=p3>CLICK HERE TO VISIT PAGE 3 </a>");
out.println("<br><a href=LogOut>CLICK HERE TO LOGOUT PAGE </a>");
}
}
p2.java
package pkgc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
public class p2 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></html>");


HttpSession ob2 = request.getSession(false);

if(ob2.isNew())
{
out.println("<body bgcolor=blue>");
String uname = request.getParameter("t1");
ob2.setAttribute("Username", uname);
ob2.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");
int visit = Integer.parseInt((String)ob2.getAttribute("visit"))+1; //Here we convert
string to integer
out.println("<h1>Welcome back " +ob2.getAttribute("Username")+" </h1>");
out.println("<h2>Visit : "+visit+"</h2>");
ob2.setAttribute("visit", visit+"");
}
out.println("<h2>Session Id : "+ob2.getId()+"<br> Timeout:
"+ob2.getMaxInactiveInterval()+" </h2>");
out.println("<h2>Last Accessed Time:"+ob2.getLastAccessedTime()+"<br> Creation
Time:"+new Date(ob2.getCreationTime())+" </h2>");
out.println("<a href=p1>CLICK HERE TO VISIT PAGE 1 </a>");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

out.println("<br><a href=p3>CLICK HERE TO VISIT PAGE 3 </a>");


out.println("<br><a href=LogOut>CLICK HERE TO LOGOUT PAGE </a>");

}
}
p3.java
package pkgc;
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;
public class p3 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></html>");
HttpSession ob3 = request.getSession(false);
if(ob3.isNew())
{
out.println("<body bgcolor=blue>");

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

ob3.setAttribute("Username", uname);
ob3.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

int visit = Integer.parseInt((String)ob3.getAttribute("visit"))+1; //Here we convert


string to integer
out.println("<h1>Welcome back "+ob3.getAttribute("Username")+" </h1>");
out.println("<h2>Visit : "+visit+"</h2>");
ob3.setAttribute("visit", visit+"");
}
out.println("<h2>Session Id : "+ob3.getId()+"<br> Timeout:
"+ob3.getMaxInactiveInterval()+" </h2>");
out.println("<h2>Last Accessed Time:"+ob3.getLastAccessedTime()+"<br> Creation
Time:"+new Date(ob3.getCreationTime())+" </h2>");
out.println("<a href=p1>CLICK HERE TO VISIT PAGE 1 </a>");
out.println("<br><a href=p2>CLICK HERE TO VISIT PAGE 2 </a>");
out.println("<br><a href=LogOut>CLICK HERE TO LOGOUT PAGE </a>");
}
}
LogOut.java
package pkgc;
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 LogOut extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession hs = request.getSession(false);
hs.invalidate();
out.println("<h4>You are successfully logout....</h4>");
request.getRequestDispatcher("index.html").include(request, response);

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

}
}

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No.3

Topic: implement the servlet input output and file operation

A] Aim: Create a servlet application to upload a file

index.html

<html>
<head>
<title>Upload File</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="post" enctype="multipart/form-data">
<h1> Upload File</h1>
Select File: <input type="file" name="file"><br/>
Destination: <input type="text" name="Destination"><br/>
<input type="submit" value="Upload">
</form>
</body>
</html>

s1.java
package pckk;
import javax.servlet.annotation.MultipartConfig;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

@MultipartConfig
public class s1 extends HttpServlet {

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

protected void service(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();
out.println("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("File Uploaded successfully");

}
catch(FileNotFoundException e)

{
out.println(e);
}}}

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: Create a servlet application to download a file 

index.html
<!DOCTYPE html>
<html>
<head>
<title> Download File</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Download Application</h1>
click<a href="s1?filename=cat.jpg">Docs</a>
</body>
</html>

s1.java
package pkgc;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class s1 extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("APPLICATION/OCTATE-STREAM");
String filename=request.getParameter("filename");
ServletContext context=getServletContext();
InputStream is=context.getResourceAsStream("/"+filename);

response.setHeader("Content-Disposition","attachment:filename=\""+filename+"\"");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

int i;
while((i=is.read())!=-1)
{
out.write(i);
}
is.close();
out.close();
}

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No. 4

Topic: Implement the following JSP applications.


A] Aim: Develop a simple JSP application to display values obtained from the use of
intrinsic objects of various types.

index.html

<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="jsp1.jsp" >
<h1>Registration form</h1>
Enter username : <input type="text" name="t1">
<input type="submit" value="Submit">
</form>
</body>
</html>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

jsp1.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><b>Use of request object</b></h1>
Query String = <%=request.getQueryString()%><br><br>
Context Path = <%=request.getContextPath() %><br><br>
Remote Host = <%=request.getRemoteHost() %><br><br>
<h1><b>Use of Response object</b></h1>
Character Encoding type = <%=request.getCharacterEncoding() %><br><br>
content = <%=request.getContentType() %><br><br>
Local = <%=request.getLocale()%><br><br>
<h1><b>Use of Session object</b></h1>
session id = <%=session.getId()%><br><br>
Last accessed time = <%=session.getLastAccessedTime() %><br><br>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

creation time = <%=new java.util.Date(session.getCreationTime())%><br><br>


</body>
</html>
Output:

B] Aim: Create a registration and login JSP application to register and authenticate the user
based on username and password using JDBC.

index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

<body>
<form action="Registration.jsp">
Enter Username:
<input type="text" name="t1"><br/>
Enter Password:
<input type="password" name="t2"><br/>
Re-enter Password:
<input type="password" name="t3"><br/>
Enter Email ID:
<input type="email" name="t4"><br/>
Country Name:
<input type="text" name="t5"><br/>
<input type="submit" value="Register">
<input type="reset">
</form>
</body>
</html>

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

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Enter Username:

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

Enter Password:

<input type="password" name="p3"><br/>

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

<input type="reset">

</form>

</body>

</html>

Registartion.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Page</title>
</head>
<body>
<%
String n = request.getParameter("t1");
String p = request.getParameter("t2");
String ep = request.getParameter("t3");
String eid = request.getParameter("t4");
String c= request.getParameter("t5");
if(p.equals(ep))
{
try{
Class.forName("com.mysql.jdbc.Driver");// Register Driver

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeinfo","root","Ashish@
000");
//Established Connection
PreparedStatement pst = con.prepareStatement("insert into empdtls values(?,?,?,?)"); //
Create Statement
pst.setString(1,n);
pst.setString(2,p);
pst.setString(3,eid);
pst.setString(4,c);
int row = pst.executeUpdate();
if(row==1)
{
out.println("Record inserted Successfully!!!");
out.println("<a href=Login.html>Click here to Login</a>");
}
else
{
out.println("Registration Failed");
%>
<jsp:include page="index.html"/>
<%
}
con.close();
pst.close();
}
catch(Exception e)
{
out.println(e);
}
}
else
{
out.println("password mismatch");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

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

Login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
<%
String uname = request.getParameter("t1");
String psw = request.getParameter("t2");
try
{
Class.forName("com.mysql.jdbc.Driver");// Register Driver
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeinfo","root","Ashish@
000"); // Established Connection
PreparedStatement pst = con.prepareStatement("select password from empdtls
where name=?"); // Create Statement
pst.setString(1,uname);

ResultSet rs = pst.executeQuery(); // all the data present in rs object


if(rs.next()) // To check every data in database
{
if(psw.equals(rs.getString(1))) // fetching Values

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

{
out.println("Login Successful!!!");
}
else
{
out.println("Username & Password Incorrect");
%>
<jsp:include page="Login.html"/>
<%
}
}
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>

output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No. 5

Topic: Implement the following JSP JSTL and EL Applications


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 the database with matching eno.

index.html
<!DOCTYPE html>
<html>
<head>
<title>Update Query</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="box">
<center> <h1> Employee Records</h1></center>
<form method="post" action="update.jsp">
Enter Number : &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
<input type="text" name="eno" class="t"><br>
Enter name : &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
<input type="text" name="name" class="t"><br>
Enter Age : &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
&nbsp;
<input type="text" name="age" class="t"><br>
Enter Designation :&nbsp; &nbsp;
<input type="text" name="desi" class="t"><br>
Enter Salary : &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
<input type="text" name="sal" class="t"><br>
<input type="submit" value="Update Query" class="submit">
<input type="reset" value= "Reset" class="re submit ">
</form>
</div>
</body>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

</html>

update.jsp

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>JSP Page</title>
</head>
<body>
<%
String eno = request.getParameter("eno");
String name = request.getParameter("name");
String age = request.getParameter("age");
String designation = request.getParameter("desi");
String salary = request.getParameter("sal");
try{
Class.forName("com.mysql.jdbc.Driver");// Register Driver
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp_update","root","Ashish@0
00"); // Established Connection
PreparedStatement stmt=con.prepareStatement("select* from emp_b2 where
eno=?"); // Create Statement
stmt.setString(1,eno); // Replacement
ResultSet rs= stmt.executeQuery(); // all the data present in rs object
if(rs.next()) // To check every data in database
{
PreparedStatement pst1=con.prepareStatement("update emp_b2 set
salary=? where eno=?");
pst1.setString(1,salary);
pst1.setString(2,eno);
PreparedStatement pst2=con.prepareStatement("update emp_b2 set age=?
where eno=?");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

pst2.setString(1,age);
pst2.setString(2,eno);
pst1.executeUpdate();
pst2.executeUpdate();
out.println("<h1>Employee "+name+"Exist!!<br> data updated
successfully!!</h1>");
}
else
{
out.println("<h1>Employee doesn't exist!!</h1>");
}
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: create a JSP page to demonstrate the use of expression language.

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="p1.jsp" method="post">

<h1>This is login page</h1>

First Name:<input type="text" name="t1"><br><br>

Last Name:<input type="text" name="t2"><br><br>

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

</form>

</body>

</html>

p1.jsp

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


<% application.setAttribute("author","TYIT");
   session.setAttribute("author", "TYIT");
   %>
<!DOCTYPE html>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="p2.jsp"   method="post">
            <h1>This is login page</h1>
            First Name:<input type="text" name="t1"><br><br>
            Last Name:<input type="text" name="t2"><br><br>
            <input type="submit" value="Submit Query">
        </form>
       
    </body>
</html>

p2.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 ${param.t1}${param.t2}</h1>
        <h1>Author Name: ${applicationScope.author}</h1>
         <h1>Country Name: ${sessionScope.country}</h1>
         <p>
             Relational Operator<br>
             Is 7 less than 10 : ${7<10}<br>
             Does 7 equals to 7 :  ${7==7}<br>
             Is 5 greater than 7 : ${7 gt 7}<br>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

         <p>MATH</p>
         6+7 = ${6+7}<br>
         8*9 = ${8*9}<br>
         </p>
        
    </body>
</html>
Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

C] Aim: Create a JSP application to demonstrate the use of JSTL

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="j1.jsp"   method="post">
            <h1>This is login page</h1>
            First Name:<input type="text" name="t1"><br><br>
            Last Name:<input type="text" name="t2"><br><br>
            Enter Input:<input type="number" name="input"><br><br>
            <input type="submit" value="Submit">
        </form>
       
    </body>
</html>

j1.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%--<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

        <title>JSP Page</title>
    </head>
    <body>
        First Name:<b><c:out value="${param.t1}"></c:out></b><br>   
        Last Name:<b><c:out value="${param.t2}"></c:out></b><br>
        Use of If:
        <c:set var="mycount" value="25" ></c:set>
        <c:if test = "${param.input==25}">
        <b><c:out value="Your value is 25"></c:out></b>
        </c:if><br><br>
         Use of for Each:
        <c:forEach var="count" begin="15" end="50" step="5">
        <b><c:out value="${count}"></c:out></b>
        </c:forEach>
    </body>
</html>
Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No. 6

Topic: Implement the following EJB Applications


A] Aim: Create a Currency Converter application using EJB.

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="s1">
            Enter Amount: <input type="text" name="amt"><br> 
Select Conversion Type: 
<input type="radio" name="type" value="r2d" 
checked>Rupees to Dollar<br/> 
<input type="radio" name="type" value="d2r">Dollar to Rupees<br/> 
<input type="submit" value="convert"><br/> 
<input type="reset" value="reset"><br/> 
        </form>
    </body>
</html>

s1.java

package pkg;

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

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ejb.EJB; 
import convertSession.convertBean;

public class s1 extends HttpServlet {


    @EJB
    convertBean obj;

  
    protected void service(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.d2Rupees(amt)+"Rupees</h1>" ); 
}
    }

}
convert.Bean.java

package convertSession;

import javax.ejb.Stateless;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

@Stateless
public class convertBean {
public convertBean(){} 
public double r2Dollar(double r){ 
return r/79.88 ; 

public double d2Rupees(double d){ 
return d*79.88 ; 
}
}

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: Develop a Simple Room Reservation System Application Using EJB

index.html

<!DOCTYPE html>
<html>
<head>
<title>Room Reservation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>
<div class="box">
<form action="s1">
<h2>Welcome to our portal for Room Reservation</h2><br>
Select the type of room
<select name="roomType">
<option>Single</option>
<option>Double</option>
<option>Deluxe</option>
<option>Super Deluxe</option>
</select>

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


Name"><br><br>

Enter Your Mobile No:<input type="text" name="txtMob" placeholder="Enter


Mobile Number"><br>

<input type="submit" value="Book a Room" class=" submit ">


<input type="reset" value="Reset" class="re submit ">
</form>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

</div>

</body>
</html>

s1.java
package pck8;

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 mybeanpack.RoomBookBean;

public class s1 extends HttpServlet {


@EJB
RoomBookBean obj;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String rtype = request.getParameter("roomType");
String cname = request.getParameter("txtName");
String cmob = request.getParameter("txtMob");
String confirmationMsg = obj.rBook(rtype,cname,cmob); out.println(confirmationMsg);
}
}
RoomBookBean.java
package mybeanpack;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class RoomBookBean {
public RoomBookBean(){}
public String rBook(String rtype, String cname, String cmob) {
String confirmationMsg = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/roomreservedb","root","Ashish
@000");
PreparedStatement pst = con.prepareStatement("select * from rbooking where
rtype=? and rstatus='Not Booked'");
pst.setString(1, rtype);
ResultSet rs = pst.executeQuery();
if(rs.next())
{
String roomno=rs.getString(1);
PreparedStatement stm1=con.prepareStatement("update rbooking set
custname=? where roomId=?");
stm1.setString(1, cname);
stm1.setString(2, roomno);
PreparedStatement stm2=con.prepareStatement("update rbooking set
custmobno=? where roomId=?");
stm2.setString(1, cmob);
stm2.setString(2, roomno);
PreparedStatement stm3=con.prepareStatement("update rbooking set
rstatus='Booked' where roomId=?");
stm3.setString(1, roomno);
stm1.executeUpdate();
stm2.executeUpdate();
stm3.executeUpdate();

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

confirmationMsg="Room "+roomno+" has been reserved <br> The charges


applicable for the room is Rs. "+rs.getString(3);
}
else
{
confirmationMsg="Room "+rtype+" is currently not available ...please try again
for another room";
}
}
catch(Exception e)
{
confirmationMsg=""+e;
}
return confirmationMsg;
}
}
Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No.7

Topic: Implement the following EJB applications with different types of Beans

A] Aim: Develop a simple EJB application to demonstrate servlet hit count using singleton
session beans

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="s1">
            <h2>Check the number of hits on the page</h2>
            <input type="submit" value="Check count"><br>
        </form>
    </body>
</html>

s1.java

package pkg1;

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

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.servlet.http.HttpServletResponse;

import javax.ejb.EJB;
import pk.bean1;

public class s1 extends HttpServlet {


@EJB
bean1 obj;
    
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("You have visited this page: " + obj.verifyCount()+" Hits on Page");
    }
}

Bean.java
Bean1.java
package pk;

import javax.ejb.Singleton;

@Singleton
public class bean1 {
    private int hit=0;
    public bean1(){}
    public int verifyCount()
            
    { return hit++;
    }
    }
    

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

B] Aim: Develop simple marks entry application into demonstrate accessing database using
EJB.

index.html
<!DOCTYPE html>

<html>
<head>
<title>Student Details</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="POST">
<h1><b>Student Details<b></h1>
Enter Student Name: <input type="text" name="t1"><br><br>
Enter marks of subject 1: <input type="number" name="t2" ><br><br>
Enter marks of subject 2: <input type="number" name="t3"><br><br>
Enter marks of subject 3: <input type="number" name="t4"><br><br>
Enter marks of subject 4: <input type="number" name="t5"><br><br>
Enter marks of subject 5: <input type="number" name="t6"><br><br>
<input type="submit" value="insert" >
<input type="reset" value="clear" >
</form>
</body>
</html>

s1.java

package pkv;

import java.io.IOException;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

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 s1 extends HttpServlet {

@EJB
b1 obj;

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String n=request.getParameter("t1");
int s1=Integer.parseInt(request.getParameter("t2"));
int s2=Integer.parseInt(request.getParameter("t3"));
int s3=Integer.parseInt(request.getParameter("t4"));
int s4=Integer.parseInt(request.getParameter("t5"));
int s5=Integer.parseInt(request.getParameter("t6"));

int totalmarks=s1+s2+s3+s4+s5;

String Message = obj.marks(n, s1, s2, s3, s4, s5, totalmarks);


out.println(Message);
}
}

b1.java

package pkv;

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

import javax.ejb.Stateless;
import java.sql.*;

@Stateless
public class b1 {

public b1() {
}

public String marks(String n, Integer s1, Integer s2, Integer s3, Integer s4, Integer s5,
Integer totalmarks) {
String Confirmationmsg = "";

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdetails", "root",
"Ashish@000");
PreparedStatement pst = con.prepareStatement("insert into marksheet
values(?,?,?,?,?,?,?)");
pst.setString(1, n);
pst.setInt(2, s1);
pst.setInt(3, s2);
pst.setInt(4, s3);
pst.setInt(5, s4);
pst.setInt(6, s5);
pst.setInt(7, totalmarks);
int rs = pst.executeUpdate();
if (rs == 1) {
Confirmationmsg = "Marks Successfully updates";
} else {
Confirmationmsg = "Try Again";
}
con.close();

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

pst.close();
}
catch(Exception e)
{
Confirmationmsg=""+e;
}
return Confirmationmsg;
}
}
Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Practical No. 8

A] Aim: Develop a 5 pages application site using any 2 or 3 Java EE Technology.

index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
application.setAttribute("user","Guest");
session.setAttribute("country","India");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><b>Login Credentials</b></h1>
<form action="s2" method="POST">
Enter Username:<input type="text" placeholder="Username" name="text1"
class="t"><br><br>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Enter Password: <input type="password" placeholder="Password" name="text2"


class="t"><br><br>
<input type="submit" value="Submit" class="btn">
<input type="reset" value="Reset" class="btn">
</form>

</body>
</html>

s2.java

package xhz;

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;

public class s2 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("<head><title>Servlet Login</title></head>");
String name = request.getParameter("text1");
String pass = request.getParameter("text2");
if (name.equals("TYIT") && pass.equals("home"))
{

RequestDispatcher rd = request.getRequestDispatcher("Demo.jsp");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

rd.forward(request, response);

}
else {
RequestDispatcher rd = request.getRequestDispatcher("Fail.jsp");
rd.include(request, response);
}

}
Demo.jsp

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


<%@ taglib prefix="f" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Welcome Nitesh<b><f:out value="${param.name}"></f:out></b></h1><br/>
<a href="home"> Click to view home page</a>
</body>
</html>

Fail.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Fail</title>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

</head>
<body>
<h1>Login failed!</h1><br/>

<b>${param.name}</b><br/>
<p>Author name:<b>${applicationScope.user}</b></p><br/>
<p>country name:<b>${sessionScope.country}</b></p><br/>
<a href="ViewLog.jsp">Click to view details</a>
</body>
</html>
Home.java

package xhz;

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;

public class home extends HttpServlet {


@EJB
Beans obj;

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(obj.display());
out.println("<form action=index.jsp>");
out.println("<input type=submit value=click to go back>");
out.println("</form>");

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

}
Beans.java

package xhz;

import javax.ejb.Stateless;

@Stateless
public class Beans {

public String display() {


return "This is home page";
}

}
ViewLog.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ViewLog</title>
</head>
<body>
<h1>Log details:</h1>
<h1>Request object</h1>
Context Path:<%=request.getContextPath()%><br>
Remote Host:<%=request.getRemoteHost()%><br>

<h1>Response object</h1>
Character encoding type:<%=response.getCharacterEncoding()%><br>
Content type:<%=response.getContentType()%><br>

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

Locale:<%=response.getLocale()%><br>

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

</body>
</html>

Output:

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893

ENTERPRISE JAVA

You might also like