The document compares and contrasts JSP and PHP for common web development tasks like displaying "Hello World", printing the date, handling forms, sessions, mail, file uploads, and connecting to databases. For each task, code examples are provided in both JSP and PHP with explanations of the key differences in their implementations and approaches.
6. JSP – Handling Forms The form: <html> <body> <form action="welcome.jsp" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> Handler: <html> <body> Welcome <%request.getParameter("name”)%> .<br /> You are <%request.getParameter("age”)%> years old. </body> </html>
7. PHP – Handling Forms The form: <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> Handler: <html> <body> Welcome <?php echo $_REQUEST["name"]; ?> .<br /> You are <?php echo $_REQUEST["age"]; ?> years old. </body> </html>
8. JSP - Session <html> <body> <% //Get current session or create a new session HtppSession session = request.getSession(true); //Add information to the session session.setAttribute(“name”, “Pramati”); //Print the information out.println(session.getAttribute(“name”); %> </body> </html>
9. PHP - Session //Getting or starting a new session <?php session_start(); ?> <html> <body> <?php //Add information to the session $_SESSION['name']= “Pramati”; //Print the information echo $_SESSION['name']; ?> </body> </html>
12. JSP – Mail sender bean – Page 1 import javax.mail.*; //JavaMail packages import javax.mail.internet.*; //JavaMail Internet packages import java.util.*; //Java Util packages public class SendMailBean { public String send(String p_from, String p_to, String p_subject, String p_message, String p_smtpServer) { // Gets the System properties Properties l_props = System.getProperties(); // Puts the SMTP server name to properties object l_props.put("mail.smtp.host",p_smtpServer); // Get the default Session using Properties Object Session l_session = Session.getDefaultInstance(l_props, null); l_session.setDebug(true); // Enable the debug mode
13. JSP – Mail sender bean – Page 2 try { MimeMessage l_msg = new MimeMessage(l_session); // Create a New message l_msg.setFrom(new InternetAddress(p_from)); // Set the From address l_msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(p_to, false)); l_msg.setSubject(p_subject); // Sets the Subject MimeBodyPart l_mbp = new MimeBodyPart(); l_mbp.setText(p_message); // Create the Multipart and its parts to it Multipart l_mp = new MimeMultipart(); l_mp.addBodyPart(l_mbp); // Add the Multipart to the message l_msg.setContent(l_mp); // Set the Date: header l_msg.setSentDate(new Date());
14. JSP – Mail sender bean – Page 3 // Send the message Transport.send(l_msg); // If here, then message is successfully sent. // Display Success message return “Mail sent”; } catch (Exception e) { return “Mail failed”; }
22. JSP and Database - II try { // Class.forName("com.mysql.jdbc.Driver").newInstance(); Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = DriverManager.getConnection( "jdbc:mysql://localhost/jsp?user=xxx&password=xxx"); st = conn.createStatement(); rs = st.executeQuery("select * from employees"); while(rs.next()) { %> <TR><TD><%= rs.getString("lname_txt") %></TD> <TD><%= rs.getString("fname_txt") %></TD></TR> <% } %> </TABLE>
23. JSP and Database - III <% } catch (Exception ex) { ex.printStackTrace(); %> </TABLE> Ooops, something bad happened: <% } finally { if (rs != null) rs.close(); if (st != null) st.close(); if (conn != null) conn.close(); } %> </BODY> </HTML>
24. PHP and Database - I <? $username="username"; $password="password"; $database="your_database"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM contacts"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close();