1. Hello, Java Servlet World!

    Posted on 2月 14th, 2012 by cx20

    Java Servlet

    Java Servlet は Java EE 周辺技術の一つで Web サーバーで Java プログラムを動作させる技術である。
    サーブレットの実行環境は Web コンテナまたはサーブレットコンテナと呼ばれ、Apache Tomcat が使われることが多い。Tomcat は雄猫を意味する。
    Java Servlet の類似技術としては、JSP(JavaServer Pages)、PHP、ASP、APS.NET 等がある。
    CGI が実行時にプロセスを起動するのに対して、Java Servlet はメモリに常駐して実行時に軽量スレッドとして動作する為、効率が良い。
    なお、Java EE 周辺技術のバージョンは以下のような対応になっている。

    J2EE Servlet JSP EL JSTL JSF EJB
    J2EE 1.2 2.2 1.1 1.1
    J2EE 1.3 2.3 1.2 1.0 2.0
    J2EE 1.4 2.4 2.0 2.0 1.1 2.1
    Java EE 5 2.5 2.1 2.1 1.2 1.2 3.0
    Java EE 6 3.0 2.2 2.2 1.2 2.0 3.1

    ソースコード(Java)

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    public class Hello extends HttpServlet {
        public void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException,IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println( "<html>" );
            out.println( "  <head>" );
            out.println( "    <title>Hello, World!</title>" );
            out.println( "  </head>" );
            out.println( "  <body>" );
            out.println( "    <p>Hello, Java Servlet World!</p>" );
            out.println( "  </body>" );
            out.println( "</html>" );
        }
    }

    実行方法

    ディレクトリ構成
    /hello               … 公開用フォルダ
        /WEB-INF         … アプリケーション設定ファイル配置場所
            /classes     … クラスファイル配置場所
     
    1. モジュールのコンパイル
        javac -cp $CATALINA_HOME/lib/servlet-api.jar:. Hello.java
    2. モジュールの配置
       WEB-INF/classes
    3. WEB-INF/web.xml の記載
    <?xml version="1.0"?>
    <web-app>
      <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>Hello</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/servlet/Hello</url-pattern>
      </servlet-mapping>
    </web-app>
    4. ブラウザで表示
       http://localhost/hello/servlet/Hello

    実行結果

    Hello, Java Servlet World!
  2. Hello, StringTemplate.NET World!

    Posted on 2月 13th, 2012 by cx20

    StringTemplate.NET

    StringTemplate.NET は Java 用のテンプレートエンジン「StringTemplate」の .NET 実装である。

    ソースコード(テンプレート)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, $message$ World!</p>
      </body>
    </html>

    ソースコード(C#)

    using System;
    using System.IO;
    using System.Diagnostics;
    using Antlr3.ST;
    using Antlr3.ST.Language;
     
    public partial class _Hello : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String template = Server.MapPath("templates");
                StringTemplateGroup group = new StringTemplateGroup("group", template);
                StringTemplate st = group.GetInstanceOf("hello");
                st.SetAttribute("message", "StringTemplate.NET");
                Response.Write(st);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }
    }

    実行方法

    ディレクトリ構成
    /Hello               … 公開用フォルダ
        /Hello.aspx
        /Hello.aspx.cs
        /Web.config      … ASP.NET 構成ファイル
        /bin
             /Antlr3.Runtime.dll
             /Antlr3.StringTemplate.dll
        /templates      … テンプレート配置場所
              /hello.st
     
    1. テンプレートの配置
       templates
    2. ブラウザで表示
       http://localhost/Hello/Hello.aspx

    実行結果

    Hello, StringTemplate.NET World!
    
  3. Hello, NVelocity World!

    Posted on 2月 12th, 2012 by cx20

    NVelocity

    NVelocity は Java 用のテンプレートエンジン「Velocity」の .NET 実装である。

    ソースコード(テンプレート)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, $message World!</p>
      </body>
    </html>

    ソースコード(C#)

    using System;
    using System.IO;
    using System.Diagnostics;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
     
    public partial class _Hello : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VelocityEngine velocity = new VelocityEngine();
            VelocityContext context = new VelocityContext();
            StringWriter sw = new StringWriter();
            try
            {
                String properties = Server.MapPath("nvelocity.properties");
                velocity.Init(properties);
                Template template = velocity.GetTemplate("hello.vm");
                context.Put("message", "NVelocity");
                template.Merge(context, sw);
                Response.Write(sw.GetStringBuilder().ToString());
            }
            catch( Exception ex )
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }
    }

    実行方法

    ディレクトリ構成
    /Hello               … 公開用フォルダ
        /Hello.aspx
        /Hello.aspx.cs
        /nvelocity.properties … NVelocity 設定ファイル
        /Web.config      … ASP.NET 構成ファイル
        /bin
             /NVelocity.dll
        /templates      … テンプレート配置場所
              /hello.vm
     
    1. テンプレートの配置
       templates
    2. nvelocity.properties の記載
    resource.loader = file
    file.resource.loader.class = NVelocity.Runtime.Resource.Loader.FileResourceLoader
    file.resource.loader.path = C:\home\edu\vs2010\vc10#\Hello\templates
    file.resource.loader.cache = true
    file.resource.loader.modificationCheckInterval = 2
    3. ブラウザで表示
       http://localhost/Hello/Hello.aspx

    実行結果

    Hello, NVelocity World!
    
  4. Hello, ASP.NET Razor World!

    Posted on 2月 11th, 2012 by cx20

    ASP.NET Razor

    Razor は ASP.NET の新しい Web ページ記述構文である。
    ASP や PHP のように HTML の中にスクリプトを埋め込むように記述できるという特徴がある。
    実際の記述コードの言語としては、C# や Visual Basic(VB.NET)が用いられる。
    実行環境として ASP.NET MVC 3 もしくは WebMatrix が必要。

    ソースコード(C# による Razor 記法)

    @{
        var msg = "ASP.NET Razor";
    }
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, @msg World!</p>
      </body>
    </html>

    ソースコード(Visual Basic による Razor 記法)

    @Code
        Dim msg = "ASP.NET Razor"
    End Code
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, @msg World!</p>
      </body>
    </html>

    上記 Razor 記法のコードは、以下の .NET のコードに相当する。実行時に .NET アセンブリにコンパイルされ実行される。

    ソースコード(C#)

    namespace ASP {
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Net;
        using System.Web;
        using System.Web.Helpers;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.WebPages;
        using System.Web.WebPages.Html;
        using WebMatrix.WebData;
        using WebMatrix.Data;
     
        public class _Page_hello_cshtml : System.Web.WebPages.WebPage {
            public _Page_hello_cshtml() {
            }
     
            protected System.Web.HttpApplication ApplicationInstance {
                get {
                    return ((System.Web.HttpApplication)(Context.ApplicationInstance));
                }
            }
     
            public override void Execute() {
                var msg = "ASP.NET Razor";
                WriteLiteral("<html>rn<head>rn<title>Hello, World!</title>rn</body>rn<body>rn<p>Hello, ");
                Write(msg);
                WriteLiteral(" World!</p>rn</body>rn</html> ");
            }
        }
    }

    実行方法

    1. IIS の公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.cshtml … C# の場合
       http://localhost/doc/hello.vbhtml … VB の場合

    実行結果

    Hello, ASP.NET Razor World!
  5. Hello, ASP.NET World!

    Posted on 2月 10th, 2012 by cx20

    ASP.NET

    ASP.NET は、ASP の後継でマイクロソフトの Web サーバー(IIS)でプログラムを動作させる技術の一つである。
    既定の言語として Visual Basic(VB.NET)や C# が用いられるが、実際にはコンパイルされた .NET アセンブリが使用される為、JScript.NET や C++/CLI など .NET に対応した他の言語でも記述が可能である。

    ソースコード(Visual Basic)

    <%@ Page Language="VB" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ) %></p>
      </body>
    </html>

    ソースコード(C#)

    <%@ Page Language="C#" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ); %></p>
      </body>
    </html>

    ソースコード(JScript.NET)

    <%@ Page Language="JScript" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ); %></p>
      </body>
    </html>

    上記コードは、以下の .NET のコードに相当する(以下の例は C# のケース)。実行時に .NET アセンブリにコンパイルされ実行される。

    ソースコード

     
    namespace ASP {
        using System.Web;
        using System.Text.RegularExpressions;
        using System.Web.Profile;
        using System.Web.UI.WebControls;
        using System.Web.Security;
        using System.Collections.Generic;
        using System.Collections.Specialized;
        using System;
        using System.Xml.Linq;
        using System.Collections;
        using System.Linq;
        using System.Web.UI;
        using System.Web.DynamicData;
        using System.Text;
        using System.Web.Caching;
        using System.Web.UI.HtmlControls;
        using System.Configuration;
        using System.Web.UI.WebControls.WebParts;
        using System.Web.SessionState;
        using System.ComponentModel.DataAnnotations;
     
        [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
        public class hello_aspx : global::System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
            private static bool @__initialized;
            private static object @__fileDependencies;
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            public hello_aspx() {
                string[] dependencies;
                ((global::System.Web.UI.Page)(this)).AppRelativeVirtualPath = "~/hello.aspx";
                if ((global::ASP.hello_aspx.@__initialized == false)) {
                    dependencies = new string[1];
                    dependencies[0] = "~/hello.aspx";
                    global::ASP.hello_aspx.@__fileDependencies = this.GetWrappedFileDependencies(dependencies);
                    global::ASP.hello_aspx.@__initialized = true;
                }
                this.Server.ScriptTimeout = 30000000;
            }
     
            protected System.Web.Profile.DefaultProfile Profile {
                get {
                    return ((System.Web.Profile.DefaultProfile)(this.Context.Profile));
                }
            }
     
            protected override bool SupportAutoEvents {
                get {
                    return false;
                }
            }
     
            protected System.Web.HttpApplication ApplicationInstance {
                get {
                    return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));
                }
            }
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            private void @__BuildControlTree(hello_aspx @__ctrl) {
                this.InitializeCulture();
                @__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(this.@__Render__control1));
            }
     
            private void @__Render__control1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
                @__w.Write("rn<html>rn<head>rn<title>Hello, World!</title>rn</head>rn<body>rn<p>");
                Response.Write( "Hello, ASP.NET World!" );
                @__w.Write("</p>rn</body>rn</html> ");
            }
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            protected override void FrameworkInitialize() {
                base.FrameworkInitialize();
                this.@__BuildControlTree(this);
                this.AddWrappedFileDependencies(global::ASP.hello_aspx.@__fileDependencies);
                this.Request.ValidateInput();
            }
        }
    }

    実行方法

    1. IIS の公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.aspx

    実行結果

    Hello, ASP.NET World!
  6. Hello, ASP World!

    Posted on 2月 9th, 2012 by cx20

    ASP

    ASP(Active Server Pages)は、マイクロソフトの Web サーバー(IIS)でプログラムを動作させる技術の一つである。既定の言語として VBScript が用いられるが、JScript や Active Scripting に対応した他の言語でも記述が可能である。現在は、後継技術の ASP.NET が登場しており、ASP(レガシー ASP)はあまり使われることは無くなってきている。
    ASP の類似技術としては、Java Servlet、JSP(JavaServer Pages)、PHP 等がある。

    OS IIS 4.0 IIS 5.0 IIS 5.1 IIS 6.0 IIS 7.0 IIS 7.5
    Windows NT 4.0
    Windows 2000
    Windows XP
    Windows 2003
    Windows Vista
    Windows 2008
    Windows 7
    Windows 2008 R2

    ソースコード(VBScript)

    <%@ Language="VBScript" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write "Hello, ASP World!" %></p>
      </body>
    </html>

    ソースコード(JScript)

    <%@ Language="JScript" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP World!" ); %></p>
      </body>
    </html>

    実行方法

    1. IIS の公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.asp

    実行結果

    Hello, ASP World!
  7. Hello, PHP World!

    Posted on 2月 8th, 2012 by cx20

    PHP

    PHP は Web サーバー上で動作するサーバーサイドスクリプト言語である。
    CGI 版およびモジュール版(Apache や IIS の拡張モジュールとして動作)が存在する。
    PHP の類似技術としては、ASP、ASP.NET、Java Servlet、JSP(JavaServer Pages) 等がある。

    ソースコード(CGI 版)

    #!/usr/bin/php
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, PHP World!"; ?></p>
      </body>
    </html>

    ソースコード(モジュール版)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, PHP World!"; ?></p>
      </body>
    </html>

    実行方法(CGI 版)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.php
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.php

    実行方法(モジュール版)

    1. Web 公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.php

    実行結果

    Hello, PHP World!
  8. Hello, CGI World!

    Posted on 2月 7th, 2012 by cx20

    CGI

    CGI(Common Gateway Interface)は、Web サーバー上でプログラムを動作させる為の仕様の一つである。
    言語としては、C言語 や Perl が主に使われるが、他の言語でも HTTP ヘッダ “Content-Type: text/html¥n¥n” を出力することで、CGI プログラムとして動作させることが可能である。
    CGI 以外の実行方式としてはモジュール形式(Apache や IIS の拡張モジュールとして動作)や FastCGI 形式がある。

    ソースコード(C言語)

    #include <stdio.h>
     
    int main( int argc, char* argv[] )
    {
        printf( "Content-type: text/htmlnn" );
        printf( "<html>n" );
        printf( "  <head>n" );
        printf( "    <title>Hello, World</title>n" );
        printf( "  </head>n" );
        printf( "  <body>n" );
        printf( "    <p>Hello, CGI World!</p>n" );
        printf( "  </body>n" );
        printf( "</html>n" );
        return 0;
    }

    ソースコード(Perl)

    #!/usr/bin/perl
    print "Content-Type: text/htmlnn";
    print "<html>n";
    print "  <head>n";
    print "    <title>Hello, World!</title>n";
    print "  </head>n";
    print "  <body>n";
    print "    <p>Hello, CGI World!</p>n";
    print "  </body>n";
    print "</html>n";

    ソースコード(PHP)

    #!/usr/bin/php
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, CGI World!"; ?></p>
      </body>
    </html>

    実行方法(C言語)

    1. コンパイル
       $ cc -o hello.cgi hello.c
    2. CGI用フォルダ(cgi-bin等)に配置
    3. 実行権限の付与
       $ chmod +x hello.cgi
    4. ブラウザで表示
       http://localhost/cgi-bin/hello.cgi

    実行方法(Perl)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.pl
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.pl

    実行方法(PHP)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.php
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.php

    実行結果

    Hello, CGI World!
  9. Hello, jQuery World!

    Posted on 2月 6th, 2012 by cx20

    jQuery

    jQuery は高い人気を誇る JavaScript ライブラリの1つである。マイクロソフトの ASP.NET のクライアントスクリプトライブラリにも採用されている。

    ソースコード

    <html>
      <head>
        <title>Hello, World!</title>
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript">
        <!--
          $(function() {
            $('#hello').html('<p>Hello, jQuery World!</p>');
          });
        //-->
        </script>
      </head>
      <body>
        <div id="hello"></div>
      </body>
    </html>

    実行方法

    1. Web サーバーの公開フォルダへ配置
    2. ブラウザで表示
       http://localhost/doc/hello.html

    実行結果

    Hello, jQuery World!
  10. Hello, prototype.js World!

    Posted on 2月 5th, 2012 by cx20

    prototype.js

    prototype.js は Ajax 対応の JavaScript ライブラリの一つ。Ruby on Rails に同梱されている。

    ソースコード

    <html>
      <head>
        <title>Hello, World!</title>
        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript">
        <!--
          Event.observe(window, 'load', function() {
            var elem = $('hello');
            elem.innerHTML = "<p>Hello, prototype.js World!</p>";
          });
        //-->
        </script>
      </head>
      <body>
        <div id="hello"></div>
      </body>
    </html>

    実行方法

    1. Web サーバーの公開フォルダへ配置
    2. ブラウザで表示
       http://localhost/doc/hello.html

    実行結果

    Hello, prototype.js World!