0% found this document useful (0 votes)
63 views34 pages

Ex. No: 03 Simple Program Using Servlet

This document describes a program that uses servlets to perform login, registration, and logout functions. It includes HTML files for the registration, login, and profile pages. It also includes Java servlet classes to handle the registration, login, and other functions. The registration servlet stores user details in the session. The login servlet validates the user and directs to the profile page if correct. The profile page allows displaying details and logging out by invalidating the session.

Uploaded by

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

Ex. No: 03 Simple Program Using Servlet

This document describes a program that uses servlets to perform login, registration, and logout functions. It includes HTML files for the registration, login, and profile pages. It also includes Java servlet classes to handle the registration, login, and other functions. The registration servlet stores user details in the session. The login servlet validates the user and directs to the profile page if correct. The profile page allows displaying details and logging out by invalidating the session.

Uploaded by

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

Ex.

No: 03 SIMPLE PROGRAM USING SERVLET

AIM:
To create program using servlet and perform login, Register and logout
operation.

PROCEDURE:
 Create a web project using tomcat server.

 And create login.html, Register.html, profile.html files in web pages.

 Then create loginservlet.java, profileservlet.java, registerservlet.java,


logoutservlet.java and displayservlet.java in source file.

 Connect the html files with servlet using action in form.

 The Rigester.html helps to register your details and store it using Session
and it will redirect you into the login page.

 In login page you can login by your name and password and can go to
profile page in the details are correct else it will show your name or
input is incorrect.

 Then in Profile I added two forms and two buttons. The do display and
logout operation.
 The display function collect the information from the session and display
it by displayservlet.

 The logout button will delete the values stored in the session by using
invalidate and redirect you to the login page.

PROGRAM:
REGISTER.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>REGISTER FORM</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
th{text-align: left;padding: 15px;color:wheat; font-style:italic}
</style>
</head>
<body style="background-color:bisque;">
<center>
<form action="registerservlet" method="post" style="width:
50%;background-color:brown;box-shadow: 5px 5px 15px black">
<Fieldset style="background-color: brown;">
<legend style="color:greenyellow">Register Form</legend>
<table>
<tr>
<th>Enter Name : </th>
<th><input type="text" name="name"></th>
</tr>
<tr>
<th>Enter Password : </th>
<th><input type="password" name="password" style="width:
149px;"></th>
</tr>
<tr>
<th>Enter Phone number : </th>
<th><input type="number" name="num"></th>
</tr>
<tr>
<th>Enter Your EMAIL : </th>
<th><input type="email" name="email"></th>
</tr>
<tr>
<th>Enter Your Age : </th>
<th><input type="number" name="age"></th>
</tr>
<tr>
<th>Enter Date_Of_Birth : </th>
<th><input type="date" name="dob"></th>
</tr>
<tr>
<th>Enter Your City : </th>
<th><input type="text" name="city"></th>
</tr>
<tr>
<th colspan="2" style="text-align: center;"><input type="submit"
value="submit" style="width: 80px;font-weight: bolder;background-color:
lightskyblue;"></th>
</tr>
</table>
</Fieldset>
</form>
</center>
</body>
</html>
LOGIN.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">
<style>
th{text-align: left;padding: 15px;color:wheat; font-style:italic}
</style>
</head>
<body style="background-color:bisque;margin-top: 10%">
<center>
<form action="loginservlet" method="post" style="width: 50%;background-
color:brown;box-shadow: 5px 5px 15px black">
<Fieldset style="background-color:brown">
<legend style="color:greenyellow">Login Form</legend>
<table>
<tr>
<th>Enter Name : </th>
<th><input type="text" name="name"></th>
</tr>
<tr>
<th>Enter Password : </th>
<th><input type="password" name="password" style="width:
149px;"></th>
</tr>
<tr>
<th style="text-align: center;"><input type="submit" value="Submit"
style="width: 80px;background-color: lightskyblue;font-weight: bolder; "></th>
<th style="text-align: center;"><a href="register.html"><input
type="button" value="Register" style="width: 80px;background-color:
lightsalmon;font-weight: bolder"></a></th>
</tr>
</table>
</Fieldset>
</form>
</center>
</body>
</html>
PROFILE.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>Profile Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
th{padding: 50px;}
</style>
</head>
<body style="background-color:lightpink">
<center>
<h1 style="color: green">Welcome !!!</h1><br>
<table>
<tr>
<th>To Do Logout:</th>
<th>To Display your details:</th>
</tr>
<tr>
<th style="width:200px;"><form action="logoutservlet"
method="post" style="box-shadow: 5px 5px 15px black">
<Fieldset>
<legend></legend>
<input type="submit" value="Logout" style="width:
80px;background-color: lightskyblue;font-weight: bolder; ">
</Fieldset>
</form>
</th>

<th style="width:200px;"><form action="displayservlet"


method="post" style="box-shadow: 5px 5px 15px black">
<Fieldset>
<legend></legend>
<input type="submit" value="Display" style="width:
80px;background-color: lightskyblue;font-weight: bolder;">
</Fieldset>
</form>
</th>
</tr>
</table>
</center>
</body>
</html>
REGISTERSERVLET.JAVA:
package servletpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author User
*/
@WebServlet(name = "registerservlet", urlPatterns = {"/registerservlet"})
public class registerservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
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. */

String n=request.getParameter("name");
String p=request.getParameter("password");
String num=request.getParameter("num");
String e=request.getParameter("email");
String c=request.getParameter("city");

HttpSession session=request.getSession();
session.setAttribute("name", n);
session.setAttribute("password", p);
session.setAttribute("number", num);
session.setAttribute("email", e);
session.setAttribute("city", c);
//session.invalidate();
response.sendRedirect("login.html");

/*out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet registerservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet registerservlet 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>

LOGINSERVLET.JAVA:
package servletpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author User
*/
@WebServlet(name = "loginservlet", urlPatterns = {"/loginservlet"})
public class loginservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{ response.setContentType("text/html;charset=UTF-
8");

PrintWriter out = response.getWriter();


String n=request.getParameter("name");
String p=request.getParameter("password");

HttpSession a=request.getSession(true);
String n2=(String)a.getAttribute("name");
String p2=(String)a.getAttribute("password");

out.println("Your name : "+n2);


out.println("Your password : "+p2);

if(n.equals(n2) && p.equals(p2))


{ response.sendRedirect("profile.html"
);
}
else{
out.println("-----> Invalid Username || Password");
}
/*try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet loginservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet loginservlet 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>

}
PROFILESERVLET.JAVA:
package servletpackage;

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;

/**
*
* @author User
*/
@WebServlet(name = "profile", urlPatterns = {"/profile"})
public class Profileservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
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.print("WELCOME USER !!!");
/* out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet profile</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet profile 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>

}
DISPLAYSERVLET.JAVA:
package servletpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author User
*/
@WebServlet(name = "displayservlet", urlPatterns = {"/displayservlet"})
public class displayservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
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. */

HttpSession a=request.getSession(true);
String n2=(String)a.getAttribute("name");
String p2=(String)a.getAttribute("password");
String num=(String)a.getAttribute("number");
String e=(String)a.getAttribute("email");
String c=(String)a.getAttribute("city");

out.println("Your name : "+n2+", ");


out.println("Your password : "+p2+", Your Number : "+num+", Your email :
"+e+", Your City : "+c+";");
/* out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet displayservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet displayservlet 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>

LOGOUTSERVLET.JAVA:
package servletpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author User
*/
@WebServlet(name = "logoutservlet", urlPatterns = {"/logoutservlet"})
public class logoutservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
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. */

HttpSession session=request.getSession();
session.invalidate();
response.sendRedirect("login.html");
out.println("");

/* out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet logoutservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet logoutservlet 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>}
OUTPUT:
After doing Logout:
RESULT:
Hence the simple program using servlet is executed successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy