728 - Om Gupta - EJ

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

PRACTICAL NO: 1

TOPIC: IMPLEMENT THE FOLLOWING SERVLET APPLICATION.

1A] AIM: CREATE A SIMPLE CALCULATOR USING SERVLET.

Steps to create project:-

1] Click on file and then click New Project .Then Select Java Web and click on Next.
2] Give the project name and set the path where you want to save your project. Then click on
Next.
3] Then click on Finish.

Index.html:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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 method="get" action="calServlet">
enter no1: <input type="text" name="txt1"><br>
enter no2: <input type="text" name="txt2"><br>
<input type="radio" name="opr" value="+">ADDITION<br>
<input type="radio" name="opr" value="-">SUBTRACTION<br>
<input type="radio" name="opr" value="*">MULTIPLICATION<br>
<input type="radio" name="opr" value="/">DIVISION<br>
<input type="submit" value="Calculate">
<input type="reset">
</form>
</body>
</html>

Steps to create Servlet file:

1] Click on Source Package , then right click on default Package , then click on New and
click on Servlet_.
2] Give Class Name and Package name and click on Next.

3] Click on Finish.
Calservl //delete all the commnets:

package mypack;

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

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
double n1=Double.parseDouble(request.getParameter("txt1"));
double n2=Double.parseDouble(request.getParameter("txt2"));
double result=0;
String op=request.getParameter("opr");

if(op.equals("+"))
result=n1+n2;
if(op.equals("-"))
result=n1-n2;
if(op.equals("*"))
result=n1*n2;
if(op.equals("/"))
result=n1/n2;
out.println("The result of operation is"+result);
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left


to edit the code.">

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

Output:
1B]Aim:create servlet for login page if password and email correct then give it welcome
message.
Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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">
ENTER USER NAME:<input type="text" name="textUN"><br>
ENTER PASSWORD:<input type="password" name="textpaw"><br>
<input type="submit" value="Login"><br>
<input type="Reset">
</form>
</body>
</html>

loginServlet.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.RequestDispatcher;

public class loginServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uname=request.getParameter("textUN");
String pass=request.getParameter("textpaw");
if(pass.equals("servlet"))
{
RequestDispatcher rd=request.getRequestDispatcher("welcomeServlet");
rd.forward(request,response);

}
else
{
out.println("<h2>login failed!Try again</h2>");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);

}
}
}

welcomeServlet.java

package wpack;
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 welcomeServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uname=request.getParameter("textUN");
out.print("<h1>Welcome " +uname+ "</h1>");
}
}

Output:

1c]Aim:Create a registration servlet in java using jdbc . Accept details such as user name
,password ,email and country from the user using HTML form and store the registration
details in the database

Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="RgServletb1">
<h1>Welcome to Registration page</h1>
Enter User Name <input type="text" name="txtUid"><br>
Enter Password <input type="password" name="txtPass"><br>
Enter Email <input type="text" name="txtEmail" ><br>
Enter Country <input type="text" name="txtCon" ><br>
<input type="reset"><input type="submit" value="REGISTER">
</form>
</body>
</html>

Creating a database table. //Password;-patkar.

RgServletb1.java

package rpack;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
public class RgServletb1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String eid=request.getParameter("txtUid");
String ps=request.getParameter("textPass");
String em=request.getParameter("txtEmail");
String co=request.getParameter("txtCon");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/empregistrationb1","root","patkar");
PreparedStatement pst = con.prepareStatement("insert into userb1 values(?,?,?,?)");
pst.setString(1,eid);
pst.setString(2,ps);
pst.setString(3,em);
pst.setString(4,co);
int row = pst.executeUpdate();
if (row==1)
{
out.println("DataInserted successfully");
}
else
{
out.println("Data is not Inserted successfully");
}
pst.close();
con.close();
}
catch(Exception e)
{
out.println(e);
}
}
}

1) Click on window tab and select services.


2) Click on databases

3) Right click on MySQL server at Localhost choose connect.


4) Put password as patkar and click on ok.

5) Select empregistrationb1 right click and choose connect.

6) Put password and click on ok. Username;-om and password:-patkar.


7) Expand the jdbc url

8) Expand empregistrationb1 then table then userb1. Right click on userb1 and select
view data.

9) Go to the projects expand the libraries and select add JAR/Folders


10) Click on MySQL connector then build and run the program.
Output:
PRACTICAL NO: 2
TOPIC: IMPLEMENT FOLLOWING SERVELT APPLICATION WITH COOKIES AND SESSION.

A) Aim: using Request Dispatcher interface create servlet which validate the password
enter by user if the user has enter “servlet” as password then he will be forward to
welcome servlet else will use stay on the index html and error will be displayed.
Code:

Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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 method="POST" action="NewServlet">
Enter User ID: <input type="text" name="txtID" value="" size="20"><br>
Enter Password: <input type="text" name="txtpw" value="" size="20"><br>
<input type="submit" value="login">
<input type="reset" value="clear" >
</form>
</body>
</html>

NewServlet.java

package mypack;

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

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String uname=request.getParameter("txtID");
String pass=request.getParameter("txtpw");
if(uname.equals("admin")&& pass.equals("admin123"))
{
out.println("<body bgcolor=blue>");
out.println("<h1>Welcome!!!"+uname+"</h1>");
}
else
{
out.println("<body bgcolor=red>");
out.println("<h1>Login Failed</h1>");
}
}
}
}
2B]Aim:Create a servlet that uses Cookies to store the number of times a
user has visited a servlet.
Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="page1Servlet">
ENTER YOUR NAME:<input type="text" name="txtUN">
<input type="submit" value="submit">
</form>
</body>
</html>

page1Servlet.java

package p1pack;

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

public class page1Servlet 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>");
out.println("<head>");
out.println("<title>Servlet page1Servlet</title>");
out.println("</head>");
out.println("<body bgcolor=cyan>");
String uname=request.getParameter("txtUN");
out.println("<h1>WELCOME "+uname+"<h1>");
Cookie ck1=new Cookie("username",uname);
Cookie ck2=new Cookie("visit","1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<h3><a href=page2Servlet>CLICK TO VISIT PAGE 2</h3>");
out.println("</body>");
out.println("</html>");
}

Page2Servlet.java

package p2pack;

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

public class page2Servlet 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>");
out.println("<head>");
out.println("<title>Servlet page2Servlet</title>");
out.println("</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)
{
out.println("<body bgcolor=yellow>");
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>visit no:"+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);

}
else
{
out.println("<body bgcolor=red>");
out.println("you have visited this page before");
}
}
}
out.println("<h3><a href=page3Servlet>CLICK TO VISIT PAGE 3</a></h3>");
out.println("</body>");
out.println("</html>");
}
}

Page3Servlet.java

package p3pack;

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

public class page3Servlet 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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page3Servlet</title>");
out.println("</head>");
out.println("<body bgcolor=orange>");
Cookie[] ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("visit"))
{

int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>visit no:"+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);

}
else
{
out.println(ck[i].getName()+"="+ck[i].getValue());
}

out.println("<h3><a href=page2Servlet>click to go back to page 2</a></h3>");

out.println("</body>");
out.println("</html>");
}
}
2c)Create a servlet demosnstatring the use of session creation and destruction.Whether
the user has visited this page first time or has visited earlier also using the session.
Source of index.html :
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="p">
ENTER USER ID:<input type="text" name="txtName"><br>
<input type="reset">
<input type="submit">
</form>
</body>
package ppack;
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 p 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>p</title></head>");
HttpSession hs= request.getSession(true);
if(hs.isNew())
{
out.println("<body bgcolor=yellow");
String name = request.getParameter("txtName");
hs.setAttribute("uname", name);
hs.setAttribute("visit","1");
out.println("<h1>Welcome First Time</h1>");
}
else
{
out.println("<h1>Welcome agian</h1>");
int visit = Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Times</h1>");
hs.setAttribute("visit",""+visit);
}
out.println("<h1>Your SessionID"+hs.getId()+"</h1>");
out.println("<h1>You logged at"+new java.util.Date(hs.getCreationTime())+"(/h1)");
out.println("<h1><a href=p2>Click on the page 2</a></h1>");
out.println("</body>");
out.println("</html>");
}
}
Source code of p2.java :
package p2pack;

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 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>p2</title></head>");
HttpSession hs= request.getSession(false);
out.print("<h1>Welcome Again on Page no:2</h1>");
int visit = Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Times</h1>");
hs.setAttribute("visit",""+visit);

out.println("<h1>Your SessionID"+hs.getId()+"</h1>");
out.println("<h1>You logged at"+new java.util.Date(hs.getCreationTime())+"(/h1)");
out.println("<h1><a href=p>Click on the page 1</a></h1>");
out.println("<h1><a href=p3>Click on the page 3</a></h1>");
out.println("<h1><a href=p4>Click on the page 4</a></h1>");
out.println("<h1><a href=Logout>Click for termination Session</a></h1>");
out.println("</body>");
out.println("</html>");
}
}
Source code of p3.java:
package p3pack;

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


protected void processRequest(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. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet p3</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet p3 at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on


the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

Source code of p4.java:


package p4pack;
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 p4 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


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>Servlet p4</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet p4 at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on


the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
Source code of Logout.java:
package logpack;

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

out.println("<html><head><title>SERVLET LOGOUT</title></head>");
out.println("<body");
javax.servlet.http.HttpSession hs = request.getSession();
if(hs!= null)hs.invalidate();
out.println("<h1>YOU ARE LOGGED NOW</h1>");
out.println("</body>");
out.println("</html>");
}
}
PRACTICAL NO: 3
Topic:Implement the Servlet IO and File applications.

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

Uploading file

Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="uploadServlet" method="post" enctype="multipart/form-data">
<h1> Uploading the 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>
uploadServlet
package upack;

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

import javax.servlet.annotation.MultipartConfig;
@MultipartConfig
public class uploadServlet extends HttpServlet {

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().toString();
out.println("FileName"+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 uploaded successfully");
}
catch(FileNotFoundException e)
{
out.print(e);
}
}
}
Output:

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

Create project then copy file which you want to download then paste this file in Project
under Webpages

Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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>
<h1>File download Application</h1>
Click<a href="downloadServlet?filename=TYBscIT.pdf.pdf">Sample file</a>
</body>
</html>

downloadServlet

package dpack;

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

public class downloadServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)


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

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

Output:
PRACTICAL NO 4
implement the folowing jsp application.
Aim:4A]devolopve simple to display values often from the use of Intrinsic object of
various type.
Index.html
Source code:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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>
<h1>Registration Page</h1>
<form action="simple.jsp">
Username:
<input type="text" name="txtusr"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Simple.jsp
Source code:
<%--
Document : simple
Created on : Jul 27, 2021, 11:41:53 AM
Author : sneha
--%>

<%@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>Use of Intrinsic object in jsp <h1>
<h1> Request Object </h1>
Query String<%=request.getQueryString() %><br>
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>
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())%><br>

</body>
</html>

Output:

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

Index.html
Source code:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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>
<h1>Registration Page</h1>
<form action="Registration.jsp">
Enter User Name:
<input type="text" name="txtName"><br>
Enter Password:
<input type="password" name="pass"><br>
Re-enter Password:
<input type="password" name="repass"><br>
Email id:
<input type="email" name="eid"><br>
Enter country name:
<input type="text" name="con"><br>
<input type="reset" value="Reset"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

Creating a database table. //Password;-patkar.

Registration.jsp
<%--
Document : Registration
Created on : Jul 27, 2021, 12:50:54 PM
Author : sneha
--%>

<%@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>
<h1>Registration JSP Page</h1>
<%
String uname=request.getParameter("txtName");
String pass1=request.getParameter("pass");
String pass2=request.getParameter("repass");
String email=request.getParameter("eid");
String ctry=request.getParameter("con");
if(pass1.equals(pass2))
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jspregb1","root","patkar");
PreparedStatement stmt=con.prepareStatement("insert into rgb1 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 Successfully");
}
else
{
out.println("Registration Failed!!!");
%><jsp:include page="index.html"/>
<%
}
}catch(Exception e)
{
out.println(e);
}
}
else
{
out.println("<h1>Password Mismatch</h1>" );
%><jsp:include page="index.html"/>
<%
}

%>
</body>
</html>

1) Click on window tab and select services.


\

2) Click on databases.
8) Right click on MySQL server at Localhost choose connect.

9) Put password as patkar and click on ok.


Select empregistrationb1 right click and choose connect.

10) Put password and click on ok. Username;-om and password:-patkar.

7)Expand the jdbc url


8) Expand empregistrationb1 then table then userb1. Right click on userb1 and select
view data.
9) Go to the projects expand the libraries and select add JAR/Folders

10) Click on MySQL connector then build and run the program.

Output:
PRACTICAL NO 5

Q5) Implement the following JSP JSTEL, EL application

5A)Create an HTML page with fields employee number ,name ,age , designation ,salary.
Now on submit this data to a JSP page which will update the employee table of database with
matching employee number.
Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
form{text-align:center;margin:230px 70px;}
</style>
</head>
<body style="background-color:red;">
<form action="Updateemp.jsp">
Enter Employee Number:<input type="text" name="txtEno"><br>
<br>
Enter Name:<input type="text" name="txtName"><br>
<br>
Enter age:<input type="text" name="txtAge"><br>

<br>
Enter Designation:<input type="text" name="txtdesg"><br>
<br>
Enter Salary:<input type="text" name="txtSal"><br>
<br>
<br><input type="submit" style="background-color:lightcoral">
<input type="reset" style="background-color:lightcoral">
</form>
</body>
</html>
Updateemp.jsp
<%--
Document : Updateemp
Created on : Aug 3, 2021, 12:40:14 PM
Author : mange
--%>

<%@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>
<h1>EMPLOYEE RECORD DETAIL</h1>
<%
String eno=request.getParameter("txtEno");
String ename =request.getParameter("txtName");
String eage=request.getParameter("txtAge");
String desg=request.getParameter("txtdesg");
String esal=request.getParameter("txtSal");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeb1","root","patkar");
PreparedStatement stmt=con.prepareStatement("select*from emp where eid=?");
stmt.setString(1,eno);
ResultSet rs=stmt.executeQuery();
if(rs.next())
{
PreparedStatement pst1=con.prepareStatement("update emp set salary=?where eid=?");
pst1.setString(1,esal);
pst1.setString(2,eno);
PreparedStatement pst2=con.prepareStatement("update emp set age=?where eid=?");
pst2.setString(1,eage);
pst2.setString(2,eno);
pst1.executeUpdate();
pst2.executeUpdate();
out.println("<h1>---Employee"+ename+"Exist-----<br>Data Update Successfully</h1>");
}
else
{
out.println("<h1>Employee record not Exist!!!!</h1>");
}
}catch(Exception e)
{
out.println(e);
}

%>
</body>
</html>
Before updating

After updating
5B]Create a JSP page to demonstrate the use of expression language.

Newjsp.jsp //run this file only not entire project.

<%--

Document : newjsp

Created on : Aug 10, 2021, 12:26:52 PM

Author : mange

--%>

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

<%

application.setAttribute("author","TYIT");

session.setAttribute("country", "India");

%>

<!DOCTYPE html>

<html>

<head>

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

<title>JSP Page</title>

<style>

body{text-align: center; margin: 230px 70px; background-color: blanchedalmond}

h1{color: red}

</style>

</head>

<body>

<form action="show.jsp">
<h1>Provide your details</h1>

User First Name:<input type="text" name="ufirst" /><br>

User Last Name:<input type="text" name="ulast" /><br>

<input type="submit" style="background-color: lightcoral">

</form>

</body>

</html>

Show.jsp

<%--

Document : show

Created on : Aug 10, 2021, 12:42:39 PM

Author : mange

--%>

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

<style>

body{text-align: center; margin: 230px 70px; background-color: blanchedalmond}

h1{color: red}

</style>

</head>

<body>

<b>WELCOME ${param.ufirst} ${param.ulast}</b>

<p>Author NAME:<b>${applicationScope.author}</b></p>

<p>Counrty Name:<b>${sessionScope.country}</b></p>

<p style="background-color: hotpink"> Relational operator<br>


Is 7 is less than 10? ${7<10}<br>

Does 7 is equal 7? ${7==7}<br>

Is 5 greater than 7? ${5 gt 7}<br>

</p>

<p style="background-color: firebrick">

NOW FOR SOME MATHS:<br>

6+7=${6+7}<br>

8*9=${8*9}<br>

</p>

</body>

</html>

5C]create a jsp page to demonstrate the use of JSTL.


Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="JSTLDemo.jsp ">
<h1>Provide your details</h1>
User's First Name:<input type="text" name="ufname" /><br>
<br>User's Last Name:<input type="text" name="ulname" /><br>
<br>Enter Input:<input type='number' name="input"/><br>
<br> <input type="submit" >
</form>
</body>
</html>

JSTLDemo.jsp
<%--
Document : JSTLDemo
Created on : Aug 10, 2021, 1:22:59 PM
Author : mange
--%>

<%@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>JSP Page</title>
</head>
<body>
First Name:<b><c:out value="${param.ufname}"/></b><br>
Last Name:<b><c:out value="${param.ulname}"></c:out></b><br>
<br>
Use of if:
<c:set var="mycount" value="25"></c:set>
<c:if test="${param.input==25}">
<b><c:out value="Your Count is 25"></c:out></b>
</c:if>
<br>
<br>
<br>
Use of For Each:
<c:forEach var="count" begin="10" end="50" step="5">
<b>
<c:out value="${count}"></c:out>
</b>
</c:forEach>

</body>
</html>
Output:
PRACTICAL NO 6
Q6]Implement the following EJB Application .
6A]Aim:Create a currency converter using EJB.
Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="convertServ">
<div>
Enter Amount:<input type="text" name="amt"><br>
Select Conversion:
<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" name="Reset">
<input type="submit" name="CONVERT">
</div>

</form>

</body>
</html>

convertBean.java
package myservfile;

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 mybeanfile.convertBean;

public class convertServ 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)+"Dollar</h1>");
}

if(request.getParameter("type").equals("d2r"))
{
out.println("<h1>"+amt+"Dollar="+obj.d2Rupees(amt)+"Repees</h1>");
}
}
}

convertBean.java

package mybeanfile;

import javax.ejb.Stateless;

@Stateless
public class convertBean {
public convertBean(){}
public double r2Dollar(double r)
{
return r/73.65;
}
public double d2Rupees(double d)
{
return d*73.65;
}

}
Output:
Implement the following EJB application

6B]Develop a simple room reservation system application using EJB.


Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="RoomBookServ">
<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> Delux</option>
<option> Super</option>
</select>
<br> <br> <br> <br> <br>
Enter Your Name:<input type="text" name="txtName"><br>
Enter Your Mobile No:<input type="text" name="textMob"><br>
<input type="Submit" value="Book a Roome">
<input type="reset">

</form>
</body>
</html>
RoomBookServ.java
package servfile;

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

public class RoomBookServ 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 beanfile;
import javax.ejb.Stateless;

import javax.ejb.*;
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","patkar");

PreparedStatement pst = con.prepareStatement("select * from rbooking where rtype=? and


rstatus='Not Booked'");
pst.setString(1, rtype);//servlet ka
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();
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:

/*
mysql> use roomreservedb3;
Database changed
mysql> create table rbooking(
-> roomId varchar(5) primary key,
-> Rtype varchar(20),
-> rcharge int(10),
-> CustName varchar(15),
-> CustMobno varchar(15),
-> Rstatus varchar(15)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into rbooking value('R101','single',2500.00,'','','not booking');


Query OK, 1 row affected (0.01 sec)

mysql> insert into rbooking value('R102','double',5000.00,'','','not booking');


Query OK, 1 row affected (0.01 sec)

mysql> insert into rbooking value('R103','double',5000.00,'','','not booking');


Query OK, 1 row affected (0.01 sec)

mysql> insert into rbooking value('R104','delux',6000.00,'','','not booking');


Query OK, 1 row affected (0.01 sec)

mysql> insert into rbooking value('R105','super',7500.00,'','','not booking');


Query OK, 1 row affected (0.01 sec)
*/
Output:
PRACTICAL NO: 7
Implement the following EJB Application with Different types of Beans

7A]Aim:Develop a simple EJB application to demonstrate the Servlet Hit count using singleton
Session Beans.
Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="HitCountServ" method="post">
Check the No. of Hits on the page:
<input type="submit" value="Check Count">
</form>
</body>
</html>

HitCountServ.java

package myserv;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;
import mybean.HitCountBean;

public class HitCountServ extends HttpServlet {

@EJB
HitCountBean 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 the page"+obj.verifyCount()+"hits on the page");

HitCountBean.java

package mybean;

import javax.ejb.Singleton;

@Singleton
public class HitCountBean {

private int hit=1;


public HitCountBean()
{}
public int verifyCount()
{

return hit++;
}
}

Output:
7B]Aim:develop simple mask entry to demonstrate the accessing database using EJB.

Step to create database :


create table stmarks(sname varchar(10),smark1 int,smark2 int, smark3 int,total int);

Index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="StudentmarksServlet">
<h1>Student Details</h1>
Enter Student Name:<input type="text" name="sname"><br>
Enter Student1 marks:<input type="number" name="sub1"><br>
Enter Student2 marks:<input type="number" name="sub2"><br>
Enter Student3 marks:<input type="number" name="sub3"><br>
<input type="submit" value="Insert">
<input type="reset" value="Clear">
</form>
</body>
</html>

StudentmarksServlet.java
package StudMarksPackage;

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 MyBeanPackage.StudentMarksBean;

public class StudentmarksServlet extends HttpServlet {

@EJB
StudentMarksBean obj;

protected void service(HttpServletRequest request, HttpServletResponse response)


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

String Studname=request.getParameter("sname");
int mark1=Integer.parseInt(request.getParameter("sub1"));
int mark2=Integer.parseInt(request.getParameter("sub2"));
int mark3=Integer.parseInt(request.getParameter("sub3"));
int totalmarks=mark1+mark2+mark3;
msg=obj.marksEntry(Studname,mark1,mark2,mark3,totalmarks);
out.println(msg);

}
StudentMarksBean.java
package MyBeanPackage;

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

@Stateless
public class StudentMarksBean {
public StudentMarksBean(){}

public String marksEntry(String Studname,int mark1,int mark2,int mark3,int totalmarks)


{
String confirmationmsg;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentmarks","root","patkar");
PreparedStatement ps=con.prepareStatement("insert into stmarks value(?,?,?,?,?)");
ps.setString(1, Studname);
ps.setInt(2, mark1);
ps.setInt(3, mark2);
ps.setInt(4, mark3);
ps.setInt(5, totalmarks);
int r=ps.executeUpdate();
if(r==1)
{
confirmationmsg="<h2><b>"+Studname+"data has been updated successfully</b></h2>";

}
else
{
confirmationmsg="<h2>could not updated data successfully.Please try again</h2>";
}

}
catch(Exception e)
{
confirmationmsg=""+e;
}
return confirmationmsg;
}

Output:
PRACTICAL NO 8

8A]Aim:create a application using differnent technology.


Pract8.jsp
<%--
Document : Pract8
Created on : Oct 2, 2021, 4:13:58 PM
Author : mange
--%>

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


<%
application.setAttribute("author", "Om");
session.setAttribute("country", "INDIA");
%>

<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:pink;
text-align:center;
margin:230px 70px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>LOGIN PAGE</h1>
<form action="Pract8a" method="post">
Enter User ID: <input type="text" name="uid"><br>
Enter Password: <input type="password" name="pass"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Pract8a.java
package mypack;

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.RequestDispatcher;

public class Pract8a extends HttpServlet {


protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String uname=request.getParameter("uid");
String pass=request.getParameter("pass");

if(pass.equals("om"))
{
RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("failed.jsp");
rd.forward(request, response);
}
}

}
welcome.jsp
<%--
Document : welcome
Created on : Oct 2, 2021, 4:18:48 PM
Author : mange
--%>

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


<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:pink;
text-align:center;
margin:230px 70px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>WELCOME <b><f:out value="${param.uid}"></f:out></b></h1><br>
<a href="home">Click To View Home Page</a>
</body>
</html>
failed.jsp
<%--
Document : failed
Created on : Oct 2, 2021, 4:20:04 PM
Author : mange
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:pink;
text-align:center;
margin:230px 70px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Login Failes!!!</h1>
<b>Author Name: ${applicationScope.author}</b><br>
<b>Country Name: ${sessionScope.country}</b><br>
<a href="logview.jsp">Click To View Log Details</a><br>
<a href="Pract8.jsp">Go Back To Login Page</a>
</body>
</html>
logview.jsp
<%--
Document : logview
Created on : Oct 2, 2021, 4:20:50 PM
Author : mange
--%>

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


<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:pink;
text-align:center;
margin:230px 70px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Log View 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>
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())%><br>

<a href="failed.jsp">Go Back</a>


</body>
</html>
home.java
package homepack;

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 displaypack.display;

public class home extends HttpServlet {


@EJB
display obj;

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<title>Home Page</title>");
out.println(obj.display());
out.println("<form action=Pract8.jsp>");
out.println("<input type=submit value=GoBack>");
out.println("</form>");
}
}
display.java
package displaypack;

import javax.ejb.Stateless;

@Stateless
public class display {

public display(){}

public String display()


{
return "This is Home Page";
}
}
Output:

You might also like