動作テスト用ファイル,PHP,Perl,Java,PostgreSQL,MySQL,DB,JSP,サーブレットのサンプル。
■はじめに ・このページは、サーバ上でPHP、Perl、データベースなどの表示のテストをするための簡単なサンプルファイルです。 ・Linux Fedora、RedHat、Apache、tomcatなどのシステム上での動作確認を行えます。 ・新しい環境でウェブアプリケーションを動作させる場合、 まず簡単なサンプルが動作するか試すことをオススメします。 ・HTML ・Perl、Perl + PostgreSQL、Perl + MySQL ・PHP、PHP + PostgreSQL、PHP + MySQL ・Java、Java + PostgreSQL、Java + MySQL ==================== 動作テスト用ファイル ==================== ■HTML / html.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTML TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> <body> <h2>HTMLテスト</h2> </body> </html> ■CGI(Perl) / perl.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=shift_jis'>\n"; print "</head><body><h2>CGI-Perlテスト</h2>\n"; print "</body></html>\n"; exit; ■CGI(Perl + PostgreSQL、use Pg) / perl_pg_postgres.cgi #!/usr/bin/perl use Pg; $conn = &ConnectDB; print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl usePg TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=shift_jis'>\n"; print "</head><body><h2>DB接続テスト(CGI-Perl usePg版)</h2>\n"; $sql = "select * from testtab;"; $select = $conn ->exec($sql); $check = $select->ntuples; if($check != 0){ for ($i=0;$i<$check;$i++){ $id = $select->getvalue($i,0); $name = $select->getvalue($i,1); print "$id / $name<br>\n"; } } print "$err</body></html>\n"; exit; sub ConnectDB{ $pghost = "localhost"; $pgport = "5432"; $pgoptions = ""; $pgtty = ""; $pgdbname = "testdb"; $pguser = "ユーザ名"; $pgpwd = "パスワード"; my($conn); $conn = Pg::setdbLogin($pghost, $pgport, $pgoptions, $pgtty, $pgdbname, $pguser, $pgpwd); if( $conn->status == PGRES_CONNECTION_BAD){ $err = "接続失敗"; } #$setencoding = $conn->exec("set client_encoding='SJIS';"); return($conn); } ■CGI(Perl + PostgreSQL、DBI:Pg) / perl_dbi_postgres.cgi #!/usr/bin/perl use DBI; $conn = &ConnectDB; print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl DBI:Pg TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=shift_jis'>\n"; print "</head><body><h2>DB接続テスト(CGI-Perl DBI:Pg版)</h2>\n"; #$sql = "insert into testtable values('4','4いいい')"; #$sql = "update testtable set name='4あああ' where id=4"; #$sql = "delete from testtable where id=4"; #$sth = $conn->prepare($sql); #$ref = $sth ->execute; $sql = "select * from testtable"; $sth = $conn->prepare($sql); $ref = $sth ->execute; for($i=0;$i<$ref;$i++){ ($id,$name) = $sth->fetchrow_array(); print "$id / $name<br>\n"; } $sth ->finish; $conn->disconnect; print "$err</body></html>\n"; exit; sub ConnectDB{ $db_user = 'ユーザ名'; $db_name = 'testdb'; $db_pwd = ''; $conn = DBI->connect("dbi:Pg:dbname=$db_name","$db_user","$db_pwd"); if($conn->errstr != ""){ $err = $conn->errstr; } #$setencoding = $conn->do("set client_encoding='SJIS';"); return($conn); } ■CGI(Perl + MySQL、DBI:MySQL) / perl_dbi_mysql.cgi #!/usr/bin/perl use DBI; $conn = &ConnectDB; print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl DBI:MySQL TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=shift_jis'>\n"; print "</head><body><h2>DB接続テスト(CGI-Perl DBI:MySQL版)</h2>\n"; #$sql = "insert into testtable values('4','4いいい')"; #$sql = "update testtable set name='4あああ' where id=4"; #$sql = "delete from testtable where id=4"; #$sth = $conn->prepare($sql); #$ref = $sth ->execute; $sql = "select * from testtable"; $sth = $conn->prepare($sql); $ref = $sth ->execute; for($i=0;$i<$ref;$i++){ ($id,$name) = $sth->fetchrow_array(); print "$id / $name<br>\n"; } $sth ->finish; $conn->disconnect; print "$err</body></html>\n"; exit; sub ConnectDB{ $db_user = 'ユーザ名'; $db_name = 'testdb'; $db_pwd = 'パスワード'; #$conn = DBI->connect("DBI:mysql:".$db_name.":localhost:3306", $db_user, $db_pwd); $conn = DBI->connect("DBI:mysql:".$db_name, $db_user, $db_pwd); if($conn->errstr != ""){ $err = $conn->errstr; } return($conn); } ■PHP / php.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>PHP TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> <body> <h2>PHPテスト</h2> <?php print Date("y/m/d H:i:s"); ?> <hr> <?php phpinfo(); ?> </body> </html> ■PHP + PostgreSQL / php_postgres.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>PHP PostgreSQL TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> <body> <h2>DB接続テスト(PHP PostgreSQL版)</h2> <?php $hostname = "localhost"; $dbport = "5432"; $username = "ユーザ名"; $password = "パスワード"; $dbname = "testdb"; //$connect = pg_connect("host=$hostname port=$dbport dbname=$dbname user=$username password=$password"); $connect = pg_connect("dbname=$dbname user=$username"); //$sql = "insert into testtable values('4','4いいい')"; //$sql = "update testtable set name='4あああ' where id=4"; //$sql = "delete from testtable where id=4"; //$rs = pg_exec($sql); $sql = "select * from testtable"; $rs = pg_exec($sql); $ct = pg_numrows($rs); for($i=0; $i<$ct;$i++){ $item = pg_fetch_array($rs, $i); print "${item['id']} / "; print "${item['name']}<br>"; } pg_close($connect); ?> </body> </html> ■PHP + MySQL / php_mysql.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>PHP MySQL TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> <body> <h2>DB接続テスト(PHP MySQL版)</h2> <?php $hostname = "localhost"; $username = "ユーザ名"; $password = "パスワード"; $dbname = "testdb"; $connect = mysql_connect($hostname, $username, $password); mysql_select_db($dbname); //$sql = "insert into testtable values('4','4いいい')"; //$sql = "update testtable set name='4あああ' where id=4"; //$sql = "delete from testtable where id=4;"; //$sqlq = mysql_query($sql, $connect); $sql = "select * from testtable"; $sqlq = mysql_query($sql, $connect); while($row = mysql_fetch_array($sqlq)){ echo $row["id"] . " / "; echo $row["name"] . "<br>"; } mysql_free_result($sqlq); mysql_close($connect); ?> </body> </html> ■Java JSP / java.jsp <%@ page language="java" contentType="text/html; charset=shift_jis" %> <% String jpString = "あかさたな"; String enString = "ABCDEFG"; %> <html> <head> <title>Java JSP TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> <body> <h2>Java JSPテスト</h2> jpString : <%=jpString%> <br> enString : <%=enString%> </body> </html> ■Java JSP + PostgreSQL / java_postgres.jsp <%@ page import="java.sql.*" language="java" contentType="text/html; charset=shift_jis" %> <% Class.forName("org.postgresql.Driver"); String jdbcUrl = "jdbc:postgresql:testdb?charSet=SJIS"; String user = "ユーザ名"; String password = "パスワード"; Connection con = null; Statement st = null; ResultSet rs = null; String sql = ""; int cnt; String test = ""; try{ con = DriverManager.getConnection(jdbcUrl,user,password); st = con.createStatement(); //sql = "set client_encoding='SJIS'"; //st.execute(sql); //sql = "insert into testtable values('4','4いいい')"; //sql = "update testtable set name='4あああ' where id=4"; //sql = "delete from testtable where id=4"; //cnt = st.executeUpdate(sql); sql = "select * from testtable"; rs = null; rs = st.executeQuery(sql); while(rs.next()){ test += rs.getInt("id") + " / "; test += rs.getString("name") + "<br>"; } }catch(Exception e){ System.out.println(e); }finally{ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(con != null){ con.close(); } }catch(Exception e){ } } %> <html> <head> <title>Java JSP PostgreSQL TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> </body> <h2>DB接続テスト(Java JSP PostgreSQL版)</h2> <%=test%> </body> </html> ■Java JSP + MySQL / java_mysql.jsp <%@ page import="java.sql.*" language="java" contentType="text/html; charset=shift_jis" %> <% Class.forName("com.mysql.jdbc.Driver"); String jdbcUrl = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=SJIS"; String user = "ユーザ名"; String password = "パスワード"; Connection con = null; Statement st = null; ResultSet rs = null; String sql = ""; int cnt; String test = ""; try{ con = DriverManager.getConnection(jdbcUrl,user,password); st = con.createStatement(); //sql = "insert into testtable values('4','4いいい')"; //sql = "update testtable set name='4あああ' where id=4"; //sql = "delete from testtable where id=4"; //cnt = st.executeUpdate(sql); sql = "select * from testtable"; rs = null; rs = st.executeQuery(sql); while(rs.next()){ test += rs.getInt("id") + " / "; //test += rs.getString("name") + "<br>"; test += new String(rs.getBytes("name"), "SJIS") + "<br>"; } }catch(Exception e){ System.out.println(e); }finally{ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(con != null){ con.close(); } }catch(Exception e){ } } %> <html> <head> <title>Java JSP MySQL TEST</title> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> </head> </body> <h2>DB接続テスト(Java JSP MySQL版)</h2> <%=test%> </body> </html> ■Java Servlet / JavaServlet.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class JavaServlet extends HttpServlet { public void init() throws ServletException { } //HTTP Get リクエストの処理 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=shift_jis"); PrintWriter out = response.getWriter(); String jpString = "あかさたな"; String enString = "ABCDEFG"; out.println("<html><head><title>JavaServlet TEST</title>"); out.println("<meta http-equiv='content-type' content='text/html; charset=shift_jis'>"); out.println("</head><body>"); out.println("<h2>JavaServletテスト</h2>"); out.println("jpString : " + jpString); out.println("<br>"); out.println("enString : " + enString); out.println("</body></html>"); out.close(); } //HTTP Post リクエストの処理 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ■Java Servlet + PostgreSQL / JavaServletPostgres.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class JavaServletPostgres extends HttpServlet { public void init() throws ServletException { } //HTTP Get リクエストの処理 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=shift_jis"); PrintWriter out = response.getWriter(); Connection con = null; try{ Class.forName("org.postgresql.Driver"); String jdbcUrl = "jdbc:postgresql:testdb?charSet=SJIS"; String user = "ユーザ名"; String password = "パスワード"; con = DriverManager.getConnection(jdbcUrl,user,password); }catch(ClassNotFoundException e){ }catch(SQLException e){ } Statement st = null; ResultSet rs = null; String sql = ""; int cnt; String test = ""; try{ st = con.createStatement(); //sql = "set client_encoding='SJIS'"; //st.execute(sql); //sql = "insert into testtable values('4','4いいい')"; //sql = "update testtable set name='4あああ' where id=4"; //sql = "delete from testtable where id=4"; //cnt = st.executeUpdate(sql); sql = "select * from testtable"; rs = null; rs = st.executeQuery(sql); while(rs.next()){ test += rs.getInt("id") + " / "; test += rs.getString("name") + "<br>"; } }catch(Exception e){ System.out.println(e); }finally{ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(con != null){ con.close(); } }catch(Exception e){ } } out.println("<html><head><title>JavaServlet PostgreSQL TEST</title>"); out.println("<meta http-equiv='content-type' content='text/html; charset=shift_jis'>"); out.println("</head><body>"); out.println("<h2>DB接続テスト(JavaServlet PostgreSQL版)</h2>"); out.println(test); out.println("</body></html>"); out.close(); } //HTTP Post リクエストの処理 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ■Java Servlet + MySQL / JavaServletMysql.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class JavaServletMysql extends HttpServlet { public void init() throws ServletException { } //HTTP Get リクエストの処理 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=shift_jis"); PrintWriter out = response.getWriter(); Connection con = null; try{ Class.forName("com.mysql.jdbc.Driver"); String jdbcUrl = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=SJIS"; String user = "ユーザ名"; String password = "パスワード"; con = DriverManager.getConnection(jdbcUrl,user,password); }catch(ClassNotFoundException e){ }catch(SQLException e){ } Statement st = null; ResultSet rs = null; String sql = ""; int cnt; String test = ""; try{ st = con.createStatement(); //sql = "insert into testtable values('4','4いいい')"; //sql = "update testtable set name='4あああ' where id=4"; //sql = "delete from testtable where id=4"; //cnt = st.executeUpdate(sql); sql = "select * from testtable"; rs = null; rs = st.executeQuery(sql); while(rs.next()){ test += rs.getInt("id") + " / "; //test += rs.getString("name") + "<br>"; test += new String(rs.getBytes("name"), "SJIS") + "<br>"; } }catch(Exception e){ System.out.println(e); }finally{ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(con != null){ con.close(); } }catch(Exception e){ } } out.println("<html><head><title>JavaServlet MySQL TEST</title>"); out.println("<meta http-equiv='content-type' content='text/html; charset=shift_jis'>"); out.println("</head><body>"); out.println("<h2>DB接続テスト(JavaServlet MySQL版)</h2>"); out.println(test); out.println("</body></html>"); out.close(); } //HTTP Post リクエストの処理 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }