比較PHP和JSP這兩個Web開發技術,在目前的情況是其實是比較PHP和Java的Web開發。以下是我就幾個主要方面進行的比較:

呼中網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、成都響應式網站建設公司等網站項目制作,到程序開發,運營維護。創新互聯成立于2013年到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
一、 語言比較
PHP是解釋執行的服務器腳本語言,首先php有簡單容易上手的特點。語法和c語言比較象,所以學過c語言的程序員可以很快的熟悉php的開發。而java需要先學好java的語法和熟悉一些核心的類庫,懂得面向對象的程序設計方法。所以java不如php好學。
Java首先要編譯成字節碼.class文件,然后在java虛擬機上解釋執行。Java的Web開發首先最容易想到的就是JSP(現在已經到JSP2.0),原來的java的Web開發都是用servlet來實現的,用servlet來開發需要程序員在java的源文件中嵌入大量的html代碼。所以后來就出現了JSP,JSP可以方便的嵌入到html文件當中,其實jsp文件在服務器上執行的時候首先會被應用服務器轉換成servlet,然后再編譯執行。Jsp可以通過servlet和JavaBean的支持產生強大的功能。JavaBean 是一種可復用的、跨平臺的軟件組件。使用javabean可以方便的實現java代碼和html的分離,能夠增強系統的功能和軟件的復用性。
Java的Web開發屬于SUN公司定義的J2EE其中的規范。而且在J2EE中包括了java的Web開發的所有方面,如:JSP、Servlet、JDBC、JNDI、JAVABEAN、EJB等等。J2EE就特別適合于做大型的企業級的應用。
二、 數據庫訪問比較
Java通過JDBC來訪問數據庫,通過不同的數據庫廠商提供的數據庫驅動方便地訪問數據庫。訪問數據庫的接口比較統一。
PHP對于不同的數據庫采用不同的數據庫訪問接口,所以數據庫訪問代碼的通用性不強。例如:用Java開發的Web應用從MySQL數據庫轉到Oracle數據庫只需要做很少的修改。而PHP則需要做大量的修改工作。
三、 系統設計架構比較
采用Java的Web開發技術,需要使用的是面向對象的系統設計方法,而PHP還是采用面向過程的開發方法。所以用Java進行開發前期需要做大量的系統分析和設計的工作。
四、 跨平臺性
Java和PHP都有很好的跨平臺的特性。幾乎都可以在不作任何修改的情況下運行在Linux或者Windows等不同的操作系統上。
五、 開發成本比較
PHP最經典的組合就是:PHP + MySQL + Apache。非常適合開發中小型的Web應用,開發的速度比較快。而且所有的軟件都是開源免費的,可以減少投入。
Java的Web應用服務器有免費Tomcat、JBoss等,如果需要更好的商業化的服務有:Web Sphere和 Web logic。
六、 分布式多層架構比較
PHP只能實現簡單的分布式兩層或三層的架構,而JAVA在這方面就比較強大,可以實現多層的網絡架構。數據庫層(持久化層)、應用(業務)邏輯層、表示邏輯層彼此分開,而且現在不同的層都已經有一些成熟的開發框架的支持。例如Struts就是利用java的Web開發技術實現了MVC的設計模式,而在業務邏輯層也有Spring框架,數據庫持久化層有Hibernate等框架。這些框架可以方便開發者高效、合理、科學得架構多層的商業應用。
下面簡要的說一下Struts,它實質上是在JSP Model2的基礎上實現的一個MVC(Model、View、Controler)框架。JSP Model2體系結構是一種聯合使用JSP 與Servlet 來提供動態內容的方法。在Struts框架中,模型由實現業務邏輯的JavaBean或EJB組件構成,控制器由Servlet實現的,視圖由一組JSP文件組成。采用Struts可以明確角色的定義和開發者與網頁設計者的分工。而且項目越復雜,其優勢越明顯。
七、 源代碼安全
PHP開發的程序的源代碼都是公開的,他人拿到php開發的程序后都可以進行修改。
Java開發的程序,最后用戶拿到的是只是一些編譯好的class類,無法看到完整的源代碼,安全性高。
八、性能比較
有人做過試驗,對這兩種種語言分別做回圈性能測試及存取Oracle數據庫測試。
在循環性能測試中,JSP只用了令人吃驚的四秒鐘就結束了20000*20000的回圈。而PHP測試的是2000*2000循環(少一個數量級),卻分別用了63秒。
數據庫測試中,二者分別對 Oracle 8 進行 1000 次 Insert,Update,Select和Delete: JSP 需要 13 秒,PHP 需要 69 秒。
綜上所述,我個人認為,PHP適合于快速開發,中小型應用系統,開發成本低,能夠對變動的需求作出快速的反應。而Java適合于開發大型的應用系統,應用的前景比較廣闊,系統易維護、可復用性較好。還有,同樣功能的系統用Java開發的系統要比PHP開發的系統的價格要高。
用json 格式返回是正確做法,但你說的訪問這個方法不確切,應該是向php提交get或post請求
我自己封裝的一個
?php
class AppConfig{
public static $dbParam = array(
'dbHost' = 'localhost',
'dbUser' = 'root',
'dbPassword' ='',
'dbName' = '數據庫名',
'dbCharset' = 'utf8',
'dbPort' = 3306,
'dbPrefix' = 'test_',
'dbPconnect' = 0,
'dbDebug' = true,
);
}
class Model {
private $version = ''; //mysql版本
private $config = array(); //數據庫配置數組
private $class; //當前類名
public $tablepre = 'ts_'; //表前綴
public $db = ''; //庫名
public $table = ''; //表名
private static $link; //數據庫鏈接句柄
private $data = array(); //中間數據容器
private $condition = ''; //查詢條件
private $fields = array(); //字段信息
private $sql = array(); //sql集合,調試用
public $primaryKey = 'id'; //表主鍵
//構造函數初始化
public function __construct($dbParam = array()) {
$this-config = (is_array($dbParam) !empty($dbParam)) ? $dbParam : AppConfig::$dbParam;
$this-connect();
$this-init();
}
//鏈接數據庫
private function connect() {
if($this-config['dbPconnect']) {
self::$link = @mysql_pconnect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword']);
}else{
self::$link = @mysql_connect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword'], true);
}
mysql_errno(self::$link) != 0 $this-errdie('Could not connect Mysql: ');
$this-db= !empty($this-db) ? $this-db : $this-config['dbName'];
$serverinfo = $this-version();
if ($serverinfo '4.1' $this-config['dbCharset']) {
mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);
}
if ($serverinfo '5.0') {
mysql_query("SET sql_mode=''", self::$link);
}
@mysql_select_db($this-db, self::$link) or $this-errdie('Cannot use database');
return self::$link;
}
//表基本信息初始化
protected function init() {
$this-class = get_class($this);
$this-table = !empty($this-table) ? $this-table : strtolower($this-class);
$this-table = $this-tablepre . $this-table;
return $this;
}
//設置屬性值
public function __set($name, $value) {
//exit($value);
$this-data['fields'][$name] = $value;
}
//獲取屬性值
public function __get($name) {
if(isset($this-data['fields'][$name])) {
return($this-data['fields'][$name]);
}else {
return NULL;
}
}
//字段信息處理
private function implodefields($data) {
if (!is_array($data)) {
$data = array();
}
$this-fields = !empty($this-data['fields']) ? array_merge($this-data['fields'], $data) : $data;
foreach($this-fields as $key = $value) {
$fieldsNameValueStr[] = "`$key`='$value'";
$fieldsNameStr[] = "`$key`";
$fieldsValueStr[] = "'$value'";
}
return array($fieldsNameValueStr, $fieldsNameStr, $fieldsValueStr);
}
//條件判斷組裝
private function condition($where = NULL) {
if (is_numeric($where)) {
$where = "WHERE `{$this-primaryKey}`='{$where}' LIMIT 1";
}elseif (is_array($where)){
$where = "WHERE `{$this-primaryKey}` in (".implode(',',$where).")";
}elseif(!empty($this-data['condition'])){
//'預留WHERE', 'order', 'group', 'limit' …………等條件關鍵詞處理接口
$where = $where ? "WHERE {$where}" : "WHERE 1";
isset($this-data['condition']['where']) $where .= ' AND '.$this-data['condition']['where'];
isset($this-data['condition']['group']) $where .= ' GROUP BY '.$this-data['condition']['group'];
isset($this-data['condition']['order']) $where .= ' ORDER BY '.$this-data['condition']['order'];
isset($this-data['condition']['limit']) $where .= ' LIMIT '.$this-data['condition']['limit'];
}else{
$where = "WHERE {$where}";
}
$this-condition = $where;
return $this;
}
//插入數據
public function insert($data = array(), $replace = false) {
$fields = $this-implodefields($data);
$insert = $replace ? 'REPLACE' : 'INSERT';
$sql = "{$insert} INTO `{$this-db}`.`{$this-table}` (".implode(', ',$fields[1]).") values (".implode(', ',$fields[2]).")";
$this-query($sql);
return $this-getInsertId();
}
//更新數據
public function update($data = array() ,$where = '') {
$numargs = func_num_args();
if ($numargs == 1) {
$where = $data;
$data = array();
}
$fields = $this-implodefields($data);
$this-condition($where);
$sql = "UPDATE `{$this-db}`.`{$this-table}` SET ".implode(', ',$fields[0])." {$this-condition}";
$this-query($sql);
return $this-getAffectedRows();
}
//刪除數據
public function delete($where = NULL) {
if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'delete'){
$sql = $where;
}else{
$this-condition($where);
$sql = "DELETE FROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
$this-query($sql);
return $this-getAffectedRows();
}
//查詢數據
public function select($where = NULL, $fields = '*') {
if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'select'){
$sql = $where;
}else{
$this-condition($where);
$sql = "SELECT {$fields} FROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
return $this-fetch($this-query($sql));
}
//查詢一條數據
public function getOne($where, $fields = '*') {
$data = $this-select($where, $fields = '*');
if($data) {
return $data[0];
}
return array();
}
//查詢多條數據
public function getAll($where, $fields = '*') {
$data = $this-select($where, $fields = '*');
return $data;
}
//結果數量
public function getCount($where = '', $fields = '*') {
$this-condition($where);
$sql = "SELECT count({$fields}) as count FROM `{$this-db}`.`{$this-table}` {$this-condition}";
$data = $this-query($sql);
if($data){
return @mysql_result($data,0);
}
return 0;
}
//執行sql語句(flag為0返回mysql_query查詢后的結果,為1返回lastid,其他返回影響行數,默認為2返回影響行數)
public function query($sql, $flag = '0', $type = '') {
if ($this-config['dbDebug']) {
$startime = $this-microtime_float();
}
//查詢
if ($type == 'UNBUFFERED' function_exists('mysql_unbuffered_query')) {
$result = @mysql_unbuffered_query($sql, self::$link);
} else {
//exit($sql);
$result = @mysql_query($sql, self::$link);
}
//重試
if (in_array(mysql_errno(self::$link), array(2006,2013)) empty($result) $this-config['dbPconnect']==0 !defined('RETRY')) {
define('RETRY',true); @mysql_close(self::$link); sleep(2);
$this-connect();
$result = $this-query($sql);
}
if ($result === false) {
$this-errdie($sql);
}
if ($this-config['dbDebug']) {
$endtime = $this-microtime_float();
$this-sql[] = array($sql,$endtime-$startime);
}
//清空操作數據
$this-data = array();
return $flag == '0' ? $result : ($flag == '1' ? $this-getInsertId() : $this-getAffectedRows());
}
//返回結果$onlyone為true返回一條否則返回所有,$type有MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH
public function fetch($result, $onlyone = false, $type = MYSQL_ASSOC) {
if($result){
if ($onlyone) {
$row = @mysql_fetch_array($result, $type);
return $row;
}else{
$rowsRs = array();
while($row=@mysql_fetch_array($result, $type)) {
$rowsRs[] = $row;
}
return $rowsRs;
}
}
return array();
}
//可以運行SELECT,SHOW,EXPLAIN 或 DESCRIBE 等返回一個資源標識符的語句得到返回結果數組
public function show($sql, $onlyone = false) {
return $this-fetch($this-query($sql), $onlyone);
}
// 使用call函數處理同類型函數
private function __call($name, $arguments) {
$callArr = array('on', 'where', 'order', 'between', 'group', 'limit');
if (in_array($name, $callArr)) {
$this-data['condition'][$name] = $arguments[0];
}else{
$this-errdie("function error: function {$name} is not in ($this-class) class exist");
}
return $this;
}
//返回最后一次插入ID
public function getInsertId() {
return @mysql_insert_id(self::$link);
}
//返回受影響行數
public function getAffectedRows() {
return @mysql_affected_rows(self::$link);
}
//獲取錯誤信息
private function error() {
return ((self::$link) ? @mysql_error(self::$link) : @mysql_error());
}
//獲取錯誤信息ID
private function errno() {
return ((self::$link) ? @mysql_errno(self::$link) : @mysql_errno());
}
//獲取版本信息
function version() {
if(empty($this-version)) {
$this-version = mysql_get_server_info(self::$link);
}
return $this-version;
}
//打印錯誤信息
private function errdie($sql = '') {
if ($this-config['dbDebug']) {
die('/BRBMySQL ERROR/B/BR
SQL:'.$sql.'/BR
ERRNO:'.$this-errno().'/BR
ERROR:'.$this-error().'/BR');
}
die('DB ERROR!!!');
}
//獲取時間微妙數
private function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//析構函數
public function __destruct() {
echo 'hr';
$this-config['dbDebug'] print_r($this-sql);
//unset($this-result);
//unset($this-condition);
//unset($this-data);
}
}
class user extends Model {
//public $db = 'qsf_mvc';
//public $table = 'user';
public $primaryKey = 'uid';
}
$userObj = new user();
//---------------------------------------插入數據方法一-----------------------------------------
//模擬ActiveRecord模式 插入數據
$userObj-username = 'hoho';
$userObj-passwd = '1478522';
$userObj-email = 'qsf.z11@163.com';
$userObj-sex = 1;
$userObj-desc = '清潔工';
$insetId = $userObj-insert();
if ($insetId 0) {
echo "插入ID為:{$insetId}BR";
}
//---------------------------------------插入數據方法二-----------------------------------------
//直接數組做參數插入數據
$userArr = array(
'username' = 'hoho',
'passwd' = '1478522',
'email' = 'qsf.z2121ia@163.com',
'sex' = '1',
'desc' = '廚師',
);
$insetId = $userObj-insert($userArr);
if ($insetId 0) {
echo "插入ID為:{$insetId}BR";
}
//---------------------------------------更新數據方法一----------------------------------------
$userObj-username = 'h111oho';
$userObj-passwd = '1478511122';
$userObj-email = 'qsf111ia@163.com';
$userObj-sex = 1;
$userObj-desc = '清潔工';
$affectedRows1 = $userObj-update(89);
if ($affectedRows1 0) {
echo "影響行數為:{$affectedRows1}BR";
}
//---------------------------------------更新數據方法二----------------------------------------
//更新記錄(傳遞參數的方式和insert操作一樣)
$userArr = array(
'username' = 'hohoho',
'passwd' = '1474rr4448522',
'email' = 'qsf.rrza@165.com',
'sex' = '0',
'desc' = '廚師qq',
);
$affectedRows = $userObj-update($userArr, $insetId);
if ($affectedRows 0) {
echo "影響行數為:{$affectedRows}BR";
}
//----------------------------------------查詢數據----------------------------------------------
$userRs0 = $userObj-select(8); //單個主鍵值
//print_r($userRs0);
$userRs1 = $userObj-select(array(1,5,8)); //多個主鍵值的數組
//print_r($userRs1);
$userRs2 = $userObj-select('select count(*) as count from user where uid 20'); //直接完整sql語句
//print_r($userRs2);
$userRs3 = $userObj-select("`uid` 0"); //where條件
//print_r($userRs3);
$userRs4 = $userObj-getOne("`uid` 0"); //獲取單條記錄
//print_r($userRs4);
$usersRs5 = $userObj-getAll("`uid` 0"); ////獲取所有記錄
//print_r($usersRs5);
$usersRs6 = $userObj-limit('0,10')-where('uid 100')-order('uid DESC')-group('username')-select();
//print_r($usersRs6);
//----------------------------------------刪除數據-----------------------------------------------
//刪除操作傳遞參數的方式和select操作一樣
$userObj-delete(60); //單個主鍵值
$userObj-delete(array(1,5,8)); //多個主鍵值的數組
$userObj-delete('delete from user where uid 100'); //直接完整sql語句
$userObj-delete("`uid` 100"); //where條件
$userObj-limit('5')-where('uid 80')-delete();
//----------------------------------------特殊查詢-----------------------------------------------
$userShowRs = $userObj-show('show create table user', true); //獲取特殊查詢的結果,第二個參數代表返回一條結果還是所有的結果
分享題目:php開發數據庫接口 php開發接口的框架
網站路徑:http://www.yijiale78.com/article18/doddpgp.html
成都網站建設公司_創新互聯,為您提供網站排名、自適應網站、定制網站、建站公司、電子商務、企業網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯