The website has been updated, checkout below websites.

https://codingvariable.com/

Thursday, 25 June 2020

Servlet program to store data using sessions.

6. write a servlet program that accepts users' information using three pages, the first page accepts personal information, the second page accepts academic information and the third accepts extra-curricular information. The Servlet stores data in sessions and in the end displays all the information.

So Basically we will create 3 HTML files for input (namely PersonalInfo.html, ExtraCurr.html, and  Academic.html) and 4 Servlet files ( PersonalInfo.java, ExtraCurr.java, Academic.java, and ShowDetails.java ) 3 for saving sessions and 4th for displaying the result.

So Let's begin some coding.

PersonalInfo.html
 

<!DOCTYPE html>
<html>
  <head>
    <title>PersonalInfo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <form method="post" action="PersonalInfo">
          Name :    <input type="text" name="name"/> <br><br>
          Contact : <input type="text" name="contact"/><br><br>
          Email Id :<input type="text" name="email"/><br><br><br>
          <input type="submit" Value="Submit"/>
          <input type="reset" value="Reset"/>
      </form>
  </body>
</html>


Fig 1. PersonalInfo.html Output



PersonalInfo.java


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 Shubham Singh (https://shubhamsingh.dev/)
 */
@WebServlet(urlPatterns = {"/PersonalInfo"})
public class PersonalInfo 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("<html>");
            out.println("<head>");
            out.println("<title>Servlet PersonalInfo</title>");  
            out.println("</head>");
            out.println("<body>");

            HttpSession s = request.getSession();
            s.setAttribute("name", request.getParameter("name"));
            s.setAttribute("contact", request.getParameter("contact"));
            s.setAttribute("email", request.getParameter("email"));

            response.sendRedirect("ExtraCurr.html");

            out.println("</body>");
            out.println("</html>");
        }
    }

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

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}


ExtraCurr.html


<!DOCTYPE html>
<html>
  <head>
    <title>ExtraCurr</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <form method="post" action="ExtraCurr">
          Hobbie : <input type="text" name="hobbie"/>
          <br>
          <input type="submit" value="Submit"/>
      </form>
  </body>
</html>


Fig 2. ExtraCurr.html Output


ExtraCurr.java


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 Shubham Singh (https://shubhamsingh.dev/)
 */
@WebServlet(urlPatterns = {"/ExtraCurr"})
public class ExtraCurr 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("<html>");           
            out.println("<body>");

            HttpSession s = request.getSession();
            s.setAttribute("hobbie",request.getParameter("hobbie"));
            response.sendRedirect("Academic.html");

            out.println("</body>");
            out.println("</html>");
        }
    }

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

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}


Academic.html


<!DOCTYPE html>
<html>
  <head>
    <title>Academic</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <form method="post" action="Academic">
          S.S.C <input type="text" name="ssc"/><br>
          H.S.C <input type="text" name="hsc"/><br>
          Sem 1 <input type="text" name="sem1"/><br>
          Sem 2 <input type="text" name="sem2"/><br>
          Sem 3 <input type="text" name="sem3"/><br>
          Sem 4 <input type="text" name="sem4"/><br><br>
          <input type="submit" value="Submit"/>
          <input type="reset" value="Reset"/>
      </form>
  </body>
</html>


Fig 3. Academin.html Output


Academic.java

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 shubham singh ( https://shubhamsingh.dev/ )
 */
@WebServlet(urlPatterns = {"/Academic"})
public class Academic 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("<html>");           
            out.println("<body>");

            HttpSession s = request.getSession();
            s.setAttribute("ssc",request.getParameter("ssc"));
            s.setAttribute("hsc",request.getParameter("hsc"));
            s.setAttribute("sem1",request.getParameter("sem1"));
            s.setAttribute("sem2",request.getParameter("sem2"));
            s.setAttribute("sem3",request.getParameter("sem3"));
            s.setAttribute("sem4",request.getParameter("sem4"));
            response.sendRedirect("ShowDetails");

            out.println("</body>");
            out.println("</html>");
        }
    }

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

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}


ShowDetails.java


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 Shubham Singh (https://shubhamsingh.dev/)
 */
@WebServlet(urlPatterns = {"/ShowDetails"})
public class ShowDetails 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("<html>");
            out.println("<body>");

            HttpSession s = request.getSession();
            out.println(" Name : " + s.getAttribute("name"));
            out.println(" Contact : " + s.getAttribute("contact"));
            out.println(" Email Id : " + s.getAttribute("email"));
            out.println(" Hobbie : " + s.getAttribute("hobbie"));
            out.println(" S.S.C  : " + s.getAttribute("ssc"));
            out.println(" H.S.C : " + s.getAttribute("hsc"));
            out.println(" Semester 1 : " + s.getAttribute("sem1"));
            out.println(" Semester 2 : " + s.getAttribute("sem2"));
            out.println(" Semester 3 : " + s.getAttribute("sem3"));
            out.println(" Semester 4 : " + s.getAttribute("sem4"));
            
            out.println("</body>");
            out.println("</html>");
        }
    }

    @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";
    }
}

Fig 4. Final Output


There are many different ways to save a user's sessions namely by using Cookies, Hidden Form Field, URL Rewriting, HttpSessions. The one we are have used above is HttpSessions.

To learn more about sessions please see another post Sessions Tracking in Servlet



No comments:

Post a Comment