Friday 14 December 2012

dynamic feedback page in jsp

hi friends m vikram parihar going to telling you that how we can add a feedback page in our dynamic website
and also we can store feedback in our database....  thanks..


first create a page name is feedback.jsp (use for view)

and write the code...



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
                    <style>
#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
width:71%;
margin-left:15%;
border-collapse:collapse;
}
#customers td, #customers th
{
font-size:1em;
border:1px solid #98bf21;
padding:3px 8px 2px 8px;
}
#customers th
{
font-size:1.1em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#ffffff;
}
#customers tr.alt td
{
color:#000000;
background-color:#EAF2D3;
}
</style>
    <script>
            function mail()
            {
                var uid = document.getElementById("email").value;
            var atpos = uid.indexOf("@");
            var dotpos = uid.lastIndexOf(".");
            if(atpos<1 || dotpos<atpos+2 || dotpos+2>=uid.length || uid =="" || uid == null)
                {
                    alert("not a valid email address")
                    return false;
                }
            }
        </script>
    <div style="position:absolute;width:1px;height:800px;top:10px;left:207px;background-color:black"></div>
        <div style="position:absolute;width:1px;height:800px;top:10px;left:1152px;background-color:black"></div>
        <img style="position:absolute;top:100px;left:1200px;" src="image/door.gif">


        <%
        try
                {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            }
        catch(ClassNotFoundException e)
                               {
        out.print("not connected to database"+e);
        }
     
        try
                {
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select name, message from userfeedback ");
            while(rs.next())
                {
                String name = rs.getString(1);
                String message = rs.getString(2);
     
               out.print("<table id= \"customers\">");%>
               <tr class =alt><td><%=name%></td><td><%=message%></td></tr>
                </table><%
}
            }
        catch(SQLException e)
                {
            out.print("sql query exception"+e);
        }
     
        %>
        <br>
        <%
        out.print("<form action = \"Handlefeedback.jsp\"  method = \"post\" onsubmit=\"return mail()\">");
        out.print("<table align = center>");
        out.print("<tr><td><u>Pass Comment</u></td></tr>");
        out.print("<tr><td>Name</td><td><input type = textbox name = \"name\" id = \"name\" size = 31>  </td></tr>");
        out.print("<tr><td>Email</td><td><input type = textbox name = \"email\" id = \"email\" size = 31>  </td></tr>");
        out.print("<tr><td>Message</td><td><textarea cols =25 rows =3  name = \"message\" id = \"message\"></textarea>  </td></tr>");
        out.print("<tr><td><button type = \"submit\">Post</button></td></tr>");
        out.print("</table>");
        out.print("</form>");
        %>





now write the code for handle the user feedback... n u can also store this feed back in  your database..
create jsp new page name is  feedbackHandle.jsp

and write the following code.. in this page


<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
    </head>
    <body>
        <%
        String name = request.getParameter("name").trim();
        String message = request.getParameter("message").trim();
        String email = request.getParameter("email").trim();
        out.print(name);
        out.print("<br>");
        out.print(message);
        out.print("<br>");
        out.print(email);


        try
                {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            }
        catch(ClassNotFoundException e)
                {
            out.print("not connect to database"+e);
        }
        %>
<%
        try
                {
             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");

            Statement stm = con.createStatement();
          stm.executeUpdate("insert into userfeedback values('"+email+"','"+name+"', '"+message+"')");
          }
       catch(SQLException e)
                {
            out.print("sorry database out of order"+e);
                }
     response.sendRedirect("feedback.jsp");
        %>
    </body>
</html>


at last when you will run your file u will see like this..








1 comment: