How Session Managment Works in servlet/jsp

/*
 * 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.
 */

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.sql.*;

/**
 *
 * @author pandey
 */
public class Registration extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        HttpSession hs = request.getSession();

        /*================== Get First Form Data  =======================*/
        String id = request.getParameter("fno");
        if (id.equals("1")) {

            String name = request.getParameter("name");
            String fname = request.getParameter("fname");
            String mname = request.getParameter("mname");

            hs.setAttribute("name", name);
            hs.setAttribute("fname", fname);
            hs.setAttribute("mname", mname);

            response.sendRedirect("./index1.jsp");

        }
        if (id.equals("2")) {

            String contact = request.getParameter("contact");
            String email = request.getParameter("email");
            String adds = request.getParameter("adds");

            hs.setAttribute("contact", contact);
            hs.setAttribute("email", email);
            hs.setAttribute("adds", adds);

            response.sendRedirect("./index2.jsp");
        }
        if (id.equals("3")) {

            String qual = request.getParameter("qual");
            String per = request.getParameter("per");

            String name = (String) hs.getAttribute("name");
            String fname = (String) hs.getAttribute("fname");
            String mname = (String) hs.getAttribute("mname");

            String contact = (String) hs.getAttribute("contact");
            String email = (String) hs.getAttribute("email");
            String adds = (String) hs.getAttribute("adds");

            try {
                /*============================== Store Data into DataBase ==============*/

                Connection c = null;
                PreparedStatement stmt = null;

                Class.forName("org.postgresql.Driver");
                c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Anand", "postgres", "password");
                System.out.println("Opened database successfully");
                stmt = c.prepareStatement("insert into adhar values(?,?,?,?,?,?,?,?)");
                stmt.setString(1, name);
                stmt.setString(2, fname);
                stmt.setString(3, mname);
                stmt.setString(4, contact);
                stmt.setString(5, email);
                stmt.setString(6, adds);
                stmt.setString(7, qual);
                stmt.setString(8, per);

                int i = stmt.executeUpdate();
                if (i != 0) {
                    out.println("<h2>registration Sucess</h2>");
                } else {
                    out.println("Registration Failed");
                }
                c.close();
                stmt.close();

            } catch (Exception e) {
                System.out.println("Exception Occur on Catch Block " + e.getMessage());
                //e.printStackTrace();
            }
        }

    }
}


==============index.jsp ===========


<%--
    Document   : index
    Created on : Mar 15, 2016, 4:15:12 PM
    Author     : ananddw
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>IRCTC  Page</title>
    </head>
    <body bgcolor="blue" style="padding-left: 45%; padding-right: 10%;padding-top: 8%;">
        <form action="./Reg">
            <table border="5">
                <pre>
    <tr><td>
           Name : <input type="text" name="name" id="name"/>
    </td></tr>
    </br></br>
    <tr><td>
            Father Name : <input type="text" name="fname" id="fname"/>
    </td></tr>
    </br></br>
    <tr><td>           Mother Name : <input type="text" name="mname" id="mname"/>
    </td></tr>

    </br></br>
    <tr><td>

              <input type="submit" name="Submit" value="Next>>"/>
    </td></tr>

<input type="hidden" name="fno" value="1"/>

                </pre>
            </table>
        </form>
    </body>
</html>
=====index1.jsp============
<%--
    Document   : index1
    Created on : Mar 15, 2016, 4:24:26 PM
    Author     : ananddw
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>IRCTC Page</title>
    </head>
    <body bgcolor="blue" style="padding-left: 45%; padding-right: 10%;padding-top: 8%;">
        <form action="./Reg" >
            <table border="5">


                <tr><td>
                        Contact : <input type="text" name="contact" id="contact"/>
                    </td></tr>
                <br></br>  
                <tr><td>
                        Email : <input type="text" name="email" id="email"/>
                    </td></tr>
                <br></br>
                <tr><td>
                        Address: <textarea rows="5" cols="15" name="adds"></textarea>
                    </td></tr>
                <br></br>        
                <tr><td>        
                        <input type="submit" name="Submit" value="Next>>"/>

                    </td></tr>
                <br></br>
                <input type="hidden" name="fno" value="2"/>

            </table>


        </form>
    </body>
</html>
============index2.jsp=====
<%--
    Document   : index2
    Created on : Mar 15, 2016, 4:30:46 PM
    Author     : ananddw
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>IRCTC Page</title>
    </head>
    <body bgcolor="blue" style="padding-left: 45%; padding-right: 10%;padding-top: 8%;">
        <form action="./Reg">
            <table border="5">

                <tr><td>  
                        Qualification :<input type="text" name="qual" id="qual"/>
                    </td></tr>
                <br></br>
                <tr><td>
                        Percentage: <input type="text" name="per" id="per"/>
                    </td></tr>
                <br></br>
                <tr><td>
                        <input type="Submit" name="Submit" value="Submit"/>
                    </td></tr>
                <br></br>
            </table>
            <input type="hidden" name="fno" value="3"/>

        </form>
    </body>
</html>
=================web.xml===========

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Registration</servlet-name>
        <servlet-class>Registration</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Registration</servlet-name>
        <url-pattern>/Reg</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>



For any suggestion please drop a mail on : ananddwivedi92@gmail.com

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map