Web 2.0 Training
 
 

Random Header :: Unpredictably Exciting

PHP vs JSP
Bookmark and Share

In my opinion, developing a Web based application in JSP (Java) is more or less like killing a housefly with a bazooka. Of course you will hit your target, no one is denying that, but you will also have collateral damage along with it. This damage ranges from Cost of hardware, hosting, servers and development. Not to mention the time taken to develop, edit or modify the application.

But, that’s just me, I’ll let you to decide and let me know after going through these factors.

SNO

Function / Feature

JSP

PHP

1.

Programming Approach

1. Completely Object Oriented

Advantage:

· Clean code

Disadvantage:

· Way too descriptive

1. Mainly a Scripting Language

2. It can also be used in OOPS from PHP 5 and above.

Advantage:

· Functional and quick coding, you can use OOP practices at your convenience

Disadvantage:

· May get clumsy

2.

String and data manipulation

Rich library, too much descriptive and object oriented code

Rich functionality. Functional and easy coding.

Has inbuilt support to use third party libraries from other programming languages like Java, C and Dot.net

3.

Web Oriented features

  1. Includes
  2. Mails
  3. File Uploads
  4. Form Handling
  5. Sessions

Advantage:

· Almost everything is built in or supported by libraries.

Disadvantage:

· Complicated and way too much of code.

Advantages:

· Inbuilt functionality and easy to use functions, written for the specific tasks

· Reduces the lines of code and time taken to write an application.

4.

Database Access features

Standard JDBC structure/ Use EJB/ Struts framework built over JDBC. Descriptive and too much overhead or boiler plate code involved. Uses the same API for all databases using JDBC drivers

Dedicated inbuilt libraries for most of the commonly used databases. Very tight integration with Oracle, MySQL and PostGRE SQL. Very minimal boiler plate code required. The libraries and results are straight forward, robust and easy to use.

5.

XML/XSL/XPATH

Use standard SAX/DOM parsers. Too much boiler plate code involved. Well defined APIs and stable implementations are available for XSL and XPATH

SAX and DOM parsers available are easy to use and to the point. Another library, Simple XML provides very easy OO approach to handling XML data. XSL and XPATH functionality is also built in.

6.

Extensibility

Java Classes and Libraries. Run’s in sandbox and hard JNI approach needed to integrate with server programs.

PHP/C/Any thing executable by the underlying OS platform. Can very easily interact with programs on the server. Very good support for native code.

7.

Dynamic Graphics/PDF and bells and whistles

Almost everything has a readymade library

Supported internally or though libraries.

8.

Web Services/SOAP

Addon Libraries like Axis, JAX-WS, etc.

In Built

9.

Portals

Spec JSR-168 and 286

Many different Portal frameworks

10.

Learning curve

It helps if you have an decent understanding of JAVA and its architecture.

If you know C/C++, then you are halfway through to learn PHP.

11.

Support

It has support and backing of Industry giants like Sun and IBM.

It enjoys the backing of Oracle, Sun, IBM, Microsoft and Zend.

12.

Hardware cost and requirement.

Well this is one factor which makes PHP a favourite. The cost of procuring a server is exponentially higher than that of getting PHP server up and running.

Cost of hosting a small and medium scale PHP application start from as low as $9 per month.

For a decent enterprise level application server with an enterprise PHP framework, the cost would come out to be around $50 per month.

Sample Codes for comparison

1. Hello World

JSP

PHP

<html>

<head>
<title>JSP — Hello World!</title>

</head>
<body>

<% out.println(” Hello World”); %> !

</body>
</html>

<html>

<head>
<title>PHP — Hello World!</title>

</head>
<body>

<?php echo ”Hello World”; ?>

</body>
</html>

2. Print Date as DD/MM/YYYY

JSP

PHP

<html>

<head>
<title>JSP – Print date</title>

</head>
<body>

<%

java.util.Calendar cal =java.util.Calendar.getInstance();

out.println(

new SimpleDateFormat(“dd/MM/yyyy).format(cal.getTime()?)

);

%>
</body>
</html>

<html>

<head>
<title>PHP – Print Date</title>

</head>
<body>

<?php echo date(“d/m/Y”); ?>

</body>
</html>

3. Read / Write session variable

JSP

PHP

<html>

<head>
<title>JSP – Session Read/Write</title>

</head>
<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>

<html>

<head>
<title>PHP – Session Read/Write</title>

</head>
<body>

<?php

//Get current session or create a new session

session_start();

//Add information to the session

$_SESSION['name']= “Pramati”;

//Print the information

echo $_SESSION['name'];

?>

</body>
</html>

4.

a. File Uploading - Form

JSP

PHP

<html>

<head>
<title>JSP – File upload form</title>

</head>
<body>

<form 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>

Please Note: No in-built support for file uploading in JSP. You have to rely on external libraries. This example uses Apache’s commons-upload lib.

<html>

<head>
<title>PHP – File upload form</title>

</head>
<body>

<form 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>

4.

b. File Uploading – Backend Code

JSP

PHP

Code for ProcessFileUpload.jsp

<%

if(ServletFileUpload.isMultipartContent(request))?

{

FileItemFactory f = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(f);

List items = upload.parseRequest(request);

Iterator itemsIter = items.getIterator();

if(iter.hasNext())?

{

File uploadedFile = new File(item.getName());

item.write(uploadedFile);

}

}

%>

Please Note: No in-built support for file uploading in JSP. You have to rely on external libraries. This example uses Apache’s commons-upload lib.

Code for ProcessFileUpload.php

<?php

if ($_FILES["file"]["error"] > 0)?

{

echo “Error: ” . $_FILES["file"]["error"];

}

else

{

move_uploaded_file(

$_FILES["file"]["tmp_name"],

“upload/” . $_FILES["file"]["name"]);

}

?>

5. Connect to the Database (MySql)

JSP

PHP

<%@ page import=”java.sql.*” %>

<%

Connetion conn = null;

java.sql.Connection conn= null;

Statement st = null;

ResultSet rs = null;

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();

// connection made….
}

<?php

$dbserver=”locations”;

$username=”username”;
$password=”password”;
$database=”your_database”;

mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

//connection made…

?>

If you liked my post, feel free to subscribe to my rss feeds

3 Comments so far (Add 1 more)

  1. Differece btween jsp and php

    1. Divyesh INDIA Windows XP Mozilla Firefox 2.0.0.17 on November 1st, 2008 at 11:24 AM
  2. Hi Josh,

    Thanks for sharing your views. Well, I am just trying to compare JSP and PHP head to head. Logic being separate from presentation (MVC) holds true for PHP too. The standard way is to use a framework (Zend ,Cake etc ) to make sure your business logic is handled by different set of files.

    Having said that, the point I am trying to make is that for the same task, the lines of code in Pure PHP will always be less that that of JSP/Servelets.
    No Offence :)

    2. rochakchauhan INDIA Windows Vista Mozilla Firefox 2.0.0.17 on October 30th, 2008 at 6:39 AM
  3. Using JSP tags is not standard practice. All business logic should be handled server side, and JSTL tags should be used to display dynamic values in the web page.

    3. Josh UNITED STATES Windows XP Mozilla Firefox 3.0.3 on October 29th, 2008 at 8:58 PM

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*