PHPVSJSP
PHPVSJSP
PHPVSJSP
JSP
Hello World - JSP
<HTML>
<HEAD>
<TITLE>JSP -- Hello World!</TITLE>
</HEAD>
<BODY>
<% out.println(" Hello World"); %> !
</BODY>
</HTML>
Hello World - PHP
<HTML>
<HEAD>
<TITLE>PHP – Hello World!</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT COLOR=RED SIZE=5>
<?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>
<?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);
<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
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
<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">
</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">
</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"]);
<HTML>
<HEAD>
<TITLE>Employee List</TITLE>
</HEAD>
<BODY>
</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
$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");
$i++;
}
?>