PHPVSJSP

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

PHP vs.

JSP
Hello World - JSP

<HTML>
<HEAD>
<TITLE>JSP -- Hello World!</TITLE>
</HEAD>
<BODY>
&nbsp;<% out.println(" Hello World"); %> !
</BODY>
</HTML>
Hello World - PHP

<HTML>
<HEAD>
<TITLE>PHP – Hello World!</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT COLOR=RED SIZE=5>
&nbsp;<?php echo "Hello world!";?> !
</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="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>
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>
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="text" NAME="p_smtpServer" SIZE=60
VALUE="<%= l_svr!=null ? l_svr : "" %>" >
TO: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=200 NAME="p_to">
FROM: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=200 NAME="p_bcc">
SUBJECT: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=100 NAME="p_subject">
MESSAGE:<br>
<TEXTAREA NAME="p_message" ROWS=10 COLS=66 SIZE=2000 WRAP=HARD>
</TEXTAREA>
<INPUT TYPE='SUBMIT'>
</FORM>
</HTML>
JSP – Mail Sender

<html>
<jsp:useBean id="sendMail" class="SendMailBean" scope="page" />
<%
String l_from = request.getParameter("p_from");
String l_to = request.getParameter("p_to");
String l_cc = request.getParameter("p_cc");
String l_subject = request.getParameter("p_subject");
String l_message = request.getParameter("p_message");
%>
<%= 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("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
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="p_to">
SUBJECT: <INPUT TYPE=TEXT SIZE=60 MAXLENGTH=100 NAME="p_subject">
MESSAGE:<br>
<TEXTAREA NAME="p_message" 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( "[email protected]", "Subject: $subject", $message, "From: $email" );
echo "Thank you for using our mail form";
?>
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="Content-Type" CONTENT="text/html; charset=windows-1252"/>
<TITLE>File Upload Page</TITLE>
</HEAD>
<BODY>Upload Files
<FORM name="filesForm" action="ProcessFileUpload.jsp"
method="post" enctype="multipart/form-data">

File 1:<input type="file" name="file1"/><br/>


<input type="submit" name="Submit" value="Upload File"/>

</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="Content-Type" CONTENT="text/html; charset=windows-1252"/>
<TITLE>File Upload Page</TITLE>
</HEAD>
<BODY>Upload Files
<FORM name="filesForm" action="ProcessFileUpload.php"
method="post" enctype="multipart/form-data">

File 1:<input type="file" name="file1"/><br/>


<input type="submit" name="Submit" value="Upload File"/>

</FORM>
</BODY>
</HTML>
PHP – File Upload

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];


}
}
?>
JSP and Database - I
<%@ page import="java.sql.*" %>

<HTML>
<HEAD>

<TITLE>Employee List</TITLE>

</HEAD>
<BODY>

<TABLE BORDER=1 width="75%">


<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("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>
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="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();
PHP and Database - II

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail:


$email<br>Web: $web<br><hr><br>";

$i++;
}

?>

You might also like