Site icon DataFlair

Session Management using HttpSession Object in JSP Part – 1

Program 1

<%-- 
    Document   : StudentInfo
    Created on : Dec 2, 2023, 2:32:30 PM
    Author     : admin
--%>

<%@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>
        <center>
         <form method="post" action="TestJspSession1.jsp">
             <table border="1">
                 <tr>
                     <th>Student Name</th>
                     <td><input type="text" name="txtname"></td>
                 </tr>                     
                 <tr>
                     <th>Password</th>
                     <td><input type="password" name="txtpass"></td>
                 </tr>                     
                 <tr>
                     <th>Course</th>
                     <td><input type="text" name="txtcourse"></td>
                 </tr>                     
                 <tr>
                     <th>Email Id</th>
                     <td><input type="text" name="txtemail"></td>
                 </tr>    
                 <tr>
                     <td></td>
                     <td><input type="submit" value="Submit"></td>
                 </tr> 
             </table>    
         </form>
     </center>         
    </body>
</html>

Program 2

<%-- 
    Document   : TestJspSession1
    Created on : Dec 2, 2023, 2:34:05 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page  session="true"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <A href="signout.jsp">signout</A>
        <center>
        <h1>Hello World This is first Page!</h1>
        <% 
            String sname,spass,scourse,semail;
            
            sname=request.getParameter("txtname");
            spass=request.getParameter("txtpass");
            scourse=request.getParameter("txtcourse");
            semail=request.getParameter("txtemail");
            
            out.println("<br>Name : "+sname);
            out.println("<br>Password : "+spass);
            out.println("<br>Course : "+scourse);
            out.println("<br>Email : "+semail);
            session.setAttribute("sname", sname);
            session.setAttribute("spass", spass);
            session.setAttribute("scourse", scourse);
            session.setAttribute("semail", semail);
        %>
        <form method="post" action="TestJspSession2.jsp">
            Enter Mobile No<input type="text" name="txtmobile">
            Enter City<input type="text" name="txtcity">
            <input type="submit" value="Submit">
        </form>    
        </center>
    </body>
</html>

Program 3

<%-- 
    Document   : TestJspSession2
    Created on : Dec 2, 2023, 2:40:57 PM
    Author     : admin
--%>

<%@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>
        <A href="signout.jsp">signout</A>
        <center>
        <h1>Hello World this is page 2!</h1>
        <% 
           String name,pass,course,email,mobile,city;
           name=session.getAttribute("sname").toString();
           pass=session.getAttribute("spass").toString();
           course=session.getAttribute("scourse").toString();
           email=session.getAttribute("semail").toString();
           mobile=request.getParameter("txtmobile");
           city=request.getParameter("txtcity");
           session.setAttribute("smobile", mobile);
           session.setAttribute("scity", city);
            out.println("<br>Name : "+name);
            out.println("<br>Password : "+pass);
            out.println("<br>Course : "+course);
            out.println("<br>Email : "+email);
            out.println("<br>Mobile No : "+mobile);
            out.println("<br>city : "+city);
           
        %>
        <form method="post" action="TestJspSession3.jsp">
            <input type="submit" value="Submit">
        </form>    
        </center> 
    </body>
</html>

Program 4

<%-- 
    Document   : TestJspSession3
    Created on : Dec 2, 2023, 2:48:15 PM
    Author     : admin
--%>

<%@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>
        <A href="signout.jsp">signout</A>
        <center>
        <h1>Hello World This is Page 3!</h1>
        <% 
         String name,pass,course,email,mobile,city;
           name=session.getAttribute("sname").toString();
           pass=session.getAttribute("spass").toString();
           course=session.getAttribute("scourse").toString();
           email=session.getAttribute("semail").toString();
           mobile=session.getAttribute("smobile").toString();
           city=session.getAttribute("scity").toString();
            out.println("<br>Name : "+name);
            out.println("<br>Password : "+pass);
            out.println("<br>Course : "+course);
            out.println("<br>Email : "+email);
            out.println("<br>Mobile No : "+mobile);
            out.println("<br>city : "+city);
        
        
        %>
        
        </center>
    </body>
</html>

Program 5

<!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>
        <div>TODO write content</div>
        <H1>This is Login Page</H1>
        <H2>This is Login Page</H2>
    </body>
</html>

Program 6

<%-- 
    Document   : signout
    Created on : Dec 2, 2023, 2:54:21 PM
    Author     : admin
--%>

<%@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>
    <%
        session.invalidate();
        session=null;
        response.sendRedirect("StudentInfo.jsp");
    %> 
    </body>
</html>
Exit mobile version