Phpvsjsp
Upcoming SlideShare
Loading in...5
×
 

Phpvsjsp

on

  • 2,243 views

 

Statistics

Views

Total Views
2,243
Views on SlideShare
2,243
Embed Views
0

Actions

Likes
0
Downloads
23
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Phpvsjsp Phpvsjsp Presentation Transcript

  • PHP vs. JSP
  • Hello World - JSP <HTML> <HEAD> <TITLE>JSP -- Hello World!</TITLE> </HEAD> <BODY> &nbsp; <% out.println(&quot; Hello World&quot;); %> ! </BODY> </HTML>
  • Hello World - PHP <HTML> <HEAD> <TITLE>PHP – Hello World!</TITLE> </HEAD> <BODY> <CENTER> <FONT COLOR=RED SIZE=5> &nbsp; <?php echo &quot;Hello world!&quot;;?> ! </FONT> </CENTER> </BODY> </HTML>
  • JSP – Print date as dd/MM/yyyy <HTML> <HEAD> <TITLE>JSP -- Hello World!</TITLE> </HEAD> <BODY> <% java.util.Calendar cal = java.util.Calendar.getInstance(); out.println( new SimpleDateFormat(“dd/MM/yyyy).format (cal.getTime()‏) ); %> </BODY> </HTML>
  • PHP – Print Date as dd/MM/yyyy <HTML> <HEAD> <TITLE>PHP – Hello World!</TITLE> </HEAD> <BODY> <CENTER> <FONT COLOR=RED SIZE=5> &nbsp; <?php echo date(“d/m/Y”)?> ! </FONT> </CENTER> </BODY> </HTML>
  • JSP – Handling Forms The form: <html> <body> <form action=&quot;welcome.jsp&quot; method=&quot;post&quot;> Name: <input type=&quot;text&quot; name=&quot;name&quot; /> Age: <input type=&quot;text&quot; name=&quot;age&quot; /> <input type=&quot;submit&quot; /> </form> </body> </html> Handler: <html> <body> Welcome <%request.getParameter(&quot;name”)%> .<br /> You are <%request.getParameter(&quot;age”)%> years old. </body> </html>
  • PHP – Handling Forms The form: <html> <body> <form action=&quot;welcome.php&quot; method=&quot;post&quot;> Name: <input type=&quot;text&quot; name=&quot;name&quot; /> Age: <input type=&quot;text&quot; name=&quot;age&quot; /> <input type=&quot;submit&quot; /> </form> </body> </html> Handler: <html> <body> Welcome <?php echo $_REQUEST[&quot;name&quot;]; ?> .<br /> You are <?php echo $_REQUEST[&quot;age&quot;]; ?> years old. </body> </html>
  • 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>
  • 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>
  • JSP – Mail Form <HTML> <FORM METHOD=POST ACTION=”mailform.jsp”> SMTP SERVER: <INPUT TYPE=&quot;text&quot; NAME=&quot;p_smtpServer&quot; SIZE=60 VALUE=&quot;<%= l_svr!=null ? l_svr : &quot;&quot; %>&quot; > TO: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=200 NAME=&quot;p_to&quot;> FROM: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=200 NAME=&quot;p_bcc&quot;> SUBJECT: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=100 NAME=&quot;p_subject&quot;> MESSAGE:<br> <TEXTAREA NAME=&quot;p_message&quot; ROWS=10 COLS=66 SIZE=2000 WRAP=HARD> </TEXTAREA> <INPUT TYPE='SUBMIT'>   </FORM> </HTML>
  • JSP – Mail Sender <html> <jsp:useBean id=&quot;sendMail&quot; class=&quot;SendMailBean&quot; scope=&quot;page&quot; /> <% String l_from = request.getParameter (&quot;p_from&quot;); String l_to = request.getParameter (&quot;p_to&quot;); String l_cc = request.getParameter (&quot;p_cc&quot;); String l_subject = request.getParameter (&quot;p_subject&quot;); String l_message = request.getParameter (&quot;p_message&quot;); %> <%= sendMail.send(l_from, l_to, l_cc, l_subject, l_message) %> </html>
  • 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(&quot;mail.smtp.host&quot;,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
  • 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());
  • 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”; }
  • PHP – Mail form <HTML> <FORM METHOD=POST ACTION=”mailform.php”> TO: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=200 NAME=&quot;p_to&quot;> SUBJECT: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=100 NAME=&quot;p_subject&quot;> MESSAGE:<br> <TEXTAREA NAME=&quot;p_message&quot; ROWS=10 COLS=66 SIZE=2000 WRAP=HARD> </TEXTAREA> <INPUT TYPE='SUBMIT'>   </FORM> </HTML>
  • PHP – Mail Sender <?php //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( &quot;someone@example.com&quot;, &quot;Subject: $subject&quot;, $message, &quot;From: $email&quot; ); echo &quot;Thank you for using our mail form&quot;; ?>
  • JSP – File Upload Form Note: No in-built support. Should rely on libraries. This example uses Apache's commons-upload lib. <HTML> <HEAD> <META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=windows-1252&quot;/> <TITLE>File Upload Page</TITLE> </HEAD> <BODY>Upload Files <FORM name=&quot;filesForm&quot; action=&quot;ProcessFileUpload.jsp&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;> File 1:<input type=&quot;file&quot; name=&quot;file1&quot;/><br/> <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Upload File&quot;/> </FORM> </BODY> </HTML>
  • JSP – File Upload <html> <body> <% if(ServletFileUpload.isMultipartContent(request))‏ { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); Iterator itemsIter = items.getIterator(); if(iter.hasNext())‏ { File uploadedFile = new File(item.getName()); item.write(uploadedFile); } } %> </body> </html>
  • PHP File Upload Form <HTML> <HEAD> <META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=windows-1252&quot;/> <TITLE>File Upload Page</TITLE> </HEAD> <BODY>Upload Files <FORM name=&quot;filesForm&quot; action=&quot;ProcessFileUpload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;> File 1:<input type=&quot;file&quot; name=&quot;file1&quot;/><br/> <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Upload File&quot;/> </FORM> </BODY> </HTML>
  • PHP – File Upload <?php if ($_FILES[&quot;file&quot;][&quot;error&quot;] > 0)‏ { echo &quot;Return Code: &quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;<br />&quot;; } else { move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;], &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]); echo &quot;Stored in: &quot; . &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]; } } ?>
  • JSP and Database - I <%@ page import=&quot;java.sql.*&quot; %> <HTML> <HEAD> <TITLE>Employee List</TITLE> </HEAD> <BODY> <TABLE BORDER=1 width=&quot;75%&quot;> <TR> <TH>Last Name</TH><TH>First Name</TH> </TR> <% //Connection conn = null; java.sql.Connection conn= null; Statement st = null; ResultSet rs = null;
  • JSP and Database - II try { // Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance(); Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance(); conn = DriverManager.getConnection( &quot;jdbc:mysql://localhost/jsp?user=xxx&password=xxx&quot;); st = conn.createStatement(); rs = st.executeQuery(&quot;select * from employees&quot;); while(rs.next()) { %> <TR><TD><%= rs.getString(&quot;lname_txt&quot;) %></TD> <TD><%= rs.getString(&quot;fname_txt&quot;) %></TD></TR> <% } %> </TABLE>
  • 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>
  • PHP and Database - I <? $username=&quot;username&quot;; $password=&quot;password&quot;; $database=&quot;your_database&quot;; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( &quot;Unable to select database&quot;); $query=&quot;SELECT * FROM contacts&quot;; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close();
  • PHP and Database - II echo &quot;<b><center>Database Output</center></b><br><br>&quot;; $i=0; while ($i < $num) { $first=mysql_result($result,$i,&quot;first&quot;); $last=mysql_result($result,$i,&quot;last&quot;); $phone=mysql_result($result,$i,&quot;phone&quot;); $mobile=mysql_result($result,$i,&quot;mobile&quot;); $fax=mysql_result($result,$i,&quot;fax&quot;); $email=mysql_result($result,$i,&quot;email&quot;); $web=mysql_result($result,$i,&quot;web&quot;); echo &quot;<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>&quot;; $i++; } ?>