Search This Blog

Friday 7 January 2022

9. JSP Program to read registration details and write into text file & JSP program to read details from text file and show in tabular format.

 <!-- 9. JSP program to read registration details and write into text file. -->

<html>

<head>

<%@page import="java.io.*,java.lang.*,java.util.Scanner;" %>

<%! int flag=0; %>

<title>Program9 to Write File</title>

 

</head>

<body bgcolor=gray text=yellow>

<h3>Workshop Registration Form</h3>

<form action="Prog9.jsp" name="form1">

<table border=3>

<tr><td>Sl.No.:</td><td><input type=text name=sno></td></tr>

<tr><td>Delegate Name:</td><td><input type=text name=dname></td></tr>

<tr><td>College Name:</td><td><input type=text name=cname></td></tr>

<tr><td>Delegate Type:</td><td><select name=dtype><option>Staff</option><option>Student</option></select></td></tr>

<tr><td>Address:</td><td><textarea name=address></textarea></td></tr>

<tr><td>Phone Number:</td><td><input type=text name=phone></td></tr>

</table>

<input type=submit value=SAVE>

</form>

<form action="Prog91.jsp" name="form2">

<input type=submit value=DISPLAY>

</form>

<%

    

          String sno=request.getParameter("sno");

          String dname=request.getParameter("dname");

          String cname=request.getParameter("cname");

          String dtype=request.getParameter("dtype");

          String address=request.getParameter("address");

          String phone=request.getParameter("phone");

         

          try

          {

     BufferedReader reader = new BufferedReader(new FileReader("D:\\example.txt"));

   

               File fileObj = new File("D:\\example.txt");

               FileWriter fstream1 = new FileWriter(fileObj, true);

               BufferedWriter br = new BufferedWriter(fstream1);

                   

               if(flag==0) //Check for first time entry in the file.

               {

                  br.write("Sl.No"+"#"+"DelegateName"+"#"+"College"+"#"+"DelegateType"+"#"+"Address"+"#"+"Phone"+"#");

                   flag=1;  //File heading is written.

                   br.newLine();

               }

               if(dname!=null)

               {

                   br.write(sno+"#"+dname+"#"+cname+"#"+dtype+"#"+address+"#"+phone+"#");

                   br.newLine();

               }

              br.close();   //close buffer writer

       }

      catch(Exception e){out.println("Enter Valid Input!");}

  %>

</body>

</html>

<!-- 91. JSP program to read details from text file and show in tabular format. -->

<html>

<head>

<%@page import="java.io.*,java.lang.*,java.util.Scanner;" %>

<title>Read File</title>

</head>

<body  bgcolor=gray text=yellow>

<%

     try

     {

         File fileObj = new File("D:\\example.txt");

         Scanner scanObj = new Scanner(fileObj);

         out.println("<h3>Details of Delegates:</h3>");

         out.println("<table cellpadding=2 border=3>");

         while (scanObj.hasNextLine())

         {

                String line = scanObj.nextLine();

                String str[]=line.split("#",7);

                out.println("<tr>");

                for (String s :str)

                {

                     out.println("<td>"+s+"</td>");

                }

                   out.println("</tr>");

         }

         out.println("</table>");

         scanObj.close();

     }

     catch (Exception e)

     {

         out.println("Enter Valid Input!");

         //out.println(e);

      }

    %>

</body>

</html>

No comments :

Post a Comment

FREE Hit Counters