Servlet/JSP

 JavaをつかってのWebアプリケーション開発は、いまでは特に難しいものではなく、情報も多くありますが、いざ個人で使用するとなると少し敷居が高いようです。ひとつはJavaをサポートしているレンタルサーバーが数少ないこと、もうひとつはPHPやPerlに比べて設定が多いことがあると思います。

 最近は、IDE、EclipseやNetBeansなどで、簡単にJavaのWEBアプリケーションの開発が簡単にできると思います。IDEを使うと、ServletやServletコンテナの知識がなくても、とっても簡単にアプリケーションが開発できてしまい、いざ実際のサーバーにデプロイする際につまずいているのを、Webなどの質問で見かけます。

 ここでは、Servletの基本的なところを説明していきたいと思います。ブラウザーからのリクエストの流れは、以下のようになります。
 

Web Browser -> HTTP Server -> Web Container -> Servlet

 Web Containerは、Servlet Containerともよばれ、簡単に言うとServletを実行させる環境です。Tomcatが有名ですが、ここでは、Jettyを使用します。Web ServerとWeb Containerがひとつになっているものも多く、Tomcat、Jettyもそうです。TomcatのHTTP Serverは貧弱なので、HTTP ServerとしてApacheを使用して、Apache JServ Protocol(AJP)でTomcatと連携しているサーバーが多いと思いま、JettyのHTTPは実運用に耐えうるそうですが、ここでは、HTTP ServerとしてLighttpd1.5を使います。

 まずは、HTTP Server、Lighttpd1.5からWeb Containerに受け渡す為の設定に以下を追加します。http://XXX/helloworld/とリクエストが来た際に、AJPでWeb Containerに受け渡す設定です。XXXはサイト名になります。ローカルで試す場合は。http://localhost/helloworld/になります。

server.modules += ( "mod_proxy_backend_ajp13" )
$HTTP["url"] =~ "^/helloworld/" {
   proxy-core.balancer = "round-robin"
   proxy-core.protocol = "ajp13"
   proxy-core.backends = ( "localhost:8009" )
   proxy-core.max-pool-size = 16
}

 Web Container、Jettyの方は、etc/jetty-ajp.xmlを読み込めば、初期設定で以下のように書かれていて8009のポートで受け取ります。

<Call name="addConnector">
   <Arg>
      <New>
         <Set name="port">8009</Set>
      </New>
   </Arg>
</Call>

 この設定で、Web Containerのところまでは、リクエストを導いて来ることができます。
 次は、Web ContainerからServletにリクエストを投げる設定です。それは、デプロイメント・ディスクリプタ、日本語だと配備記述子とよばれるweb.xmlに記述されます。Javaらしからぬファイル名からは推測しずらい呼び名です。Jettyでは、etc/webdefault.xmlに初期設定のweb.xmlがかありますので、これを利用してもいいですが、理解のために簡略したものを使います。

<?xml version="1.0" encoding="UTF-8"?>
<web-app
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   metadata-complete="true"
   version="2.5"
>
      <display-name>Hello World</display-name>
      <servlet>
         <servlet-name>helloworld</servlet-name>
         <servlet-class>HelloWorld</servlet-class>
      </servlet>
      <servlet-mapping>
         <servlet-name>helloworld</servlet-name>
         <url-pattern>/</url-pattern>
      </servlet-mapping>
</web-app>

 Jetty 7では、Servlet 2.5に対応しているので、Ver2.5のSchemaを指定しています。Servlet 2.4からweb.xmlがDTDベースからXMLSchema変更されています。Servlet 2.3のweb.xmlですとDOCTYPEで定義がなされています。設定の意味を簡単に説明しますと、<servlet>でServletの定義をしています。<servlet-name>でServletの呼び名を定義し、そのクラスを<servlet-class>に定義します。次に、<servlet-mapping>で、ServletとURLの対応づけを行います。<servlet-name>は<servlet>内で決めた名前を使い、<url-pattern>は、URLのパターンを書きます。ここでは、乱暴に/にしていますので、http://XXX/helloworld/で来たものはすべて、HelloWorld.classが呼び出されます。web.xmlには、もっと多くのことを記述できますが、今回はこれで十分です。

 ここでやっと呼び出されるServletの作ることにします。ファイル名は、HelloWorld.javaで内容は以下のようになります。

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

 Servletをつくる手初めとしてjavax.servlet.http.HttpServletを継承してHelloWorldというクラスを作ります。そのクラスにGETのリクエストに対応する、doGetというメソッドをオーバーライドします(POSTを処理する際は、doPostをオーバーライドします)。今回は、HttpServletRequest requestは、使用せずにリクエストされたら、HttpServletResponse responseにレスポンスを入れた返すだけのクラスにしてあります。htmlを返しますので、setContentTypeで、”text/html”を指定しています。次に、getWriter()で取得した、文字出力ストリームにどんどん出力していくだけです。出力内容がおわったら、close()で出力を閉じます。

ちなみに、HttpServletにはHTTPのリクエストに対応したメソッドが準備されています。

GET :
 protected void doGet(HttpServletRequest request, HttpServletResponse response)

POST:
 protected void doPost(HttpServletRequest request, HttpServletResponse response)

PUT:
 protected void doPut(HttpServletRequest request, HttpServletResponse response)

DELETE:
 protected void doDelete(HttpServletRequest request, HttpServletResponse response)

HEAD:
 protected void doHead(HttpServletRequest request, HttpServletResponse response)

TRACE:
 protected void doTrace(HttpServletRequest request, HttpServletResponse response)

OPTIONS:
 protected void doOptions(HttpServletRequest request, HttpServletResponse response)

 次にこれをコンパイルします。Servlet周りのライブラリーは、JDKに入っていませんので、Jettyのlibの中にあるservlet-api-2.5.jarをCLASS PATHに指定します。Jettyが、/usr/local/にあるのであれば、以下のようにコンパイルします。

$ javac -classpath /usr/local/jetty/lib/servlet-api-2.5.jar HelloWorld.java

 これででき上がった、HelloWorld.classと先に作ったweb.xmlをWeb Containerから呼び出せるところに置かなければなりません。通常は、webappsの下に置きます。Jettyが、/usr/local/にあるのであれば/usr/local/jetty/webappsです。具体的には、以下のように配備します。

webapps--+--helloworld--+--WEB-INF--+-- web.xml
                                    +--classes--+-- HelloWorld.class

 ファイルを再配置して、/usr/local/jetty/bin/jetty.sh startで、Jettyを起動し、http://XXX/helloworld/とリクエストすれば、”Hello World!”と表示されるはずです。Javaって結構、PHPなどに比べて設定が大変ですよね。PHPとくらべるとJavaは5倍速いというベンチマークを見たことがありますので、古いマシンを使っているものとして、そちらに期待したいですね。

Servlet/JSP への154件のフィードバック

  1. Hksfnjkh の発言:

    best place to buy careprost https://bimatoprostrx.com
    bimatoprost

  2. Nuiskdoik の発言:

    plaquenil 200 mg 60 tab https://plaquenils.com
    cost of generic plaquenil

  3. Nisdksdl の発言:

    ivermectin buy online https://stromectolr.com
    ivermectin 1 cream

  4. Ivermectin の発言:

    ivermectin otc stromectol tablet 3 mg – cost of ivermectin medicine
    ivermectin 1 cream generic https://stromectolr.com
    stromectol medicine

  5. Niusdkfj の発言:

    stromectol prices – ivermectin brand name ivermectin 250ml
    stromectol 3 mg tablet https://stromectolh.com
    ivermectin australia

  6. Buikdifud の発言:

    ivermectin 2ml – ivermectin cream canada cost ivermectin 1 cream generic
    ivermectin buy https://stromectolh.com
    stromectol usa

  7. Biisdlsfj の発言:

    ivermectin 2mg how much is ivermectin stromectol price in india
    ivermectin 3mg tablets https://stromectolh.com
    oral ivermectin cost

  8. Mkoslfk の発言:

    buy hydroxychloroquine 200 mg hydroxychloroquine tab 200mg plaquenil 150 mg
    plaquenil generic drug https://hydroxychloroquineq.com
    plaquenil 25 mg

  9. Zithromax の発言:

    zithromax drug zithromax online pharmacy canada can you buy zithromax over the counter
    zithromax over the counter uk https://zithromaxq.com
    generic zithromax azithromycin

  10. Loispodl の発言:

    buy zithromax online purchase zithromax z-pak zithromax 250
    zithromax 1000 mg pills zithromax drug zithromax cost canada
    how to get zithromax online http://www.molijnh.com/bbs/home.php?mod=space&uid=10376/#
    zithromax cost uk http://118.24.12.193/home.php?mod=space&uid=152942/#
    zithromax capsules australia http://bbs.net.yoyun.cn/home.php?mod=space&uid=96372/#

  11. Viagraju の発言:

    buy viagra online viagra canada female viagra
    viagra vs cialis https://vgrst.com
    viagra for women

  12. Clomidl の発言:

    clomid for sale clomid tablets clomid coupon
    clomid online cheap https://clomidt.com
    price of clomid

  13. Clomid の発言:

    clomid canada https://clomidt.com
    purchase clomiphene

  14. Viagra の発言:

    buy viagra pills viagra cost online viagra
    buy viagra professional https://vgrst.com
    viagra coupons

  15. Niolosik の発言:

    prednisone 20mg prednisone 20 mg tablets coupon prednisone 40mg
    prednisone over the counter cost https://prednisonet.com
    buy prednisone online uk

  16. Sildenafil の発言:

    best price for sildenafil medicine https://sildenafilg.com/
    sildenafil price in india

  17. GenericPil の発言:

    buy canadian drugs https://gpillswithoutprescription.com
    canadian online pharmacy

  18. Sildeanfil の発言:

    sildenafil where to buy https://sildenafilg.com/
    where to buy otc sildenafil

  19. CanadaPh の発言:

    canadian online pharmacy: canadian online drugs – dog antibiotics without vet prescription
    canadian pharmacy: ed meds online without doctor prescription pain medications without a prescription: canadian drug prices
    buy anti biotics without prescription http://www.xnfgxh.com/bbs/home.php?mod=space&uid=4268350/#
    best ed pills non prescription http://www.anjiaxz.com/home.php?mod=space&uid=108647/#
    carprofen without vet prescription https://www.baimi100.com/home.php?mod=space&uid=19641/#

  20. Sildenafil の発言:

    sildenafil citrate buy sildenafil 20 mg tablet coupon generic sildenafil 50 mg
    sildenafil 20 mg brand name sildenafil citrate online sildenafil online purchase
    generic sildenafil nz http://e.414500.cc/home.php?mod=space&uid=351226/#
    order sildenafil citrate online https://www.laosanzy.com/space-uid-27300.html/#
    sildenafil citrate australia https://bblt.me/home.php?mod=space&uid=26315/#

  21. Stromectol の発言:

    ivermectin cream 1% ivermectin nz ivermectin price
    stromectol 6 mg dosage https://stromectolg.com
    stromectol for sale

  22. Stromectol の発言:

    ivermectin cream 5% – https://stromectol1st.com/
    ivermectin cost

  23. Stromectol の発言:

    stromectol online: buy stromectol online ivermectin lotion cost
    stromectol tablets

  24. Kioplsodik の発言:

    pharmacy Dipyridamole Epivir
    Cipro https://pillfast24.com/
    health canada drug database

  25. AllPharm の発言:

    canadian prescriptions online canada pharmacy online orders hormones
    canadian drugs online pharmacy https://pillfast24.com/
    Brand Cialis

  26. Kioplsodik の発言:

    Cipro prescription drugs without doctor approval erection pills
    prescription drugs online without doctor https://pharmfast24.com/
    pharmacy online drugstore

  27. AllPharm の発言:

    canada pharmaceutical online ordering Trimox Zyrtec
    canada drugs https://pharmfastd.com/
    canadian pharmacy no prescription

  28. Pharmacy の発言:

    try our ed pack list of reputable canadian pharmacies Prilosec
    approved canadian online pharmacies https://canadarxfast.online/
    pharmacy in canada

  29. AllPharm の発言:

    prescription discount best pills for ed Biaxin
    Cefixime https://pillfast24.online/
    Super P-Force

  30. Pjiishffj の発言:

    canadian pharmacy generic viagra pharmacy canada discount Urispas
    canadian prescriptions https://canadarxfast.online/
    cialis canadian pharmacy

  31. Buisodkf の発言:

    Microzide buy prescription drugs online cheap canadian pharmacies online
    Aceon https://pharmfast24.online/
    Cefadroxil

  32. nanoo の発言:

    Great article. I will be going through many of these issues as well..|

  33. vesdjudeq の発言:

    oRGRgc jlwogmseczhw, [url=http://chqvfhkkzvkt.com/]chqvfhkkzvkt[/url], [link=http://eznnlhrmfhuu.com/]eznnlhrmfhuu[/link], http://zniwrpsdfkek.com/

  34. uxvgxgrwwtu の発言:

    9nkHmY dfeejnzniwrd, [url=http://oeahkvydalmu.com/]oeahkvydalmu[/url], [link=http://bnlxanqivfif.com/]bnlxanqivfif[/link], http://hhodzaluvzwn.com/

  35. Vudskdsj の発言:

    ivermectin 200mg stromectol xr ivermectin 4000 mcg
    ivermectin oral 0 8 http://stromectolabc.com/
    ivermectin 3mg

  36. Vudskdsj の発言:

    where to buy ivermectin pills ivermectin 12 stromectol tablet 3 mg
    buy stromectol pills http://stromectolabc.com/
    ivermectin otc

  37. Vudskdsj の発言:

    buy stromectol canada generic stromectol ivermectin tablets
    purchase oral ivermectin http://stromectolabc.com/
    stromectol 3mg cost

  38. Nudshdsdsj の発言:

    purchase ivermectin ivermectin generic price of ivermectin
    stromectol otc http://stromectolabc.com/
    ivermectin 4 tablets price

  39. Nudshdsdsj の発言:

    ivermectin tablets order ivermectin human ivermectin 5
    stromectol online canada http://stromectolabc.com/
    ivermectin australia

  40. Nudshdsdsj の発言:

    cost of stromectol medication stromectol tab 3mg ivermectin 3mg
    ivermectin human http://stromectolabc.com/
    stromectol 3mg cost

  41. Busdjdsh の発言:

    buy stromectol pills http://stromectolabc.com/
    ivermectin 2%

  42. Busdjdsh の発言:

    ivermectin eye drops http://stromectolabc.com/
    buy ivermectin for humans uk

  43. Busdjdsh の発言:

    stromectol pill http://stromectolabc.com/
    ivermectin oral

  44. Busdjdsh の発言:

    stromectol cost http://stromectolabc.com/
    where to buy stromectol online

  45. Busdjdsh の発言:

    cost of ivermectin 1% cream http://stromectolabc.com/
    buy liquid ivermectin

  46. Busdjdsh の発言:

    stromectol buy http://stromectolabc.com/
    stromectol 6 mg dosage

  47. Busdjdsh の発言:

    stromectol 3 mg tablets price http://stromectolabc.com/
    ivermectin syrup

  48. Busdjdsh の発言:

    stromectol 6 mg dosage http://stromectolabc.com/
    stromectol tab 3mg

  49. Busdjdsh の発言:

    ivermectin pill cost http://stromectolabc.com/
    stromectol online pharmacy

  50. Busdjdsh の発言:

    ivermectin 5 http://stromectolabc.com/
    ivermectin 10 mg

  51. Busjdhj の発言:

    stromectol order online http://stromectolabc.com/
    ivermectin generic

  52. Bhdsjsdk の発言:

    stromectol 12mg http://stromectolabc.com/
    buy stromectol pills

  53. Busdjdsk の発言:

    cost of ivermectin pill http://stromectolabc.com/
    stromectol 0.5 mg

  54. Busdjdsk の発言:

    ivermectin 3mg http://stromectolabc.com/
    buy stromectol

  55. Busdjdsk の発言:

    stromectol generic name http://stromectolabc.com/
    ivermectin 4

  56. Busdjdsk の発言:

    buy ivermectin cream http://stromectolabc.com/
    stromectol in canada

  57. Busdjdsk の発言:

    ivermectin generic cream http://stromectolabc.com/
    where to buy stromectol

  58. Busdjdsk の発言:

    ivermectin pill cost http://stromectolabc.com/
    ivermectin cost

  59. Busdjdsk の発言:

    ivermectin online http://stromectolabc.com/
    ivermectin 1% cream generic

  60. Busdjdsk の発言:

    stromectol price us http://stromectolabc.com/
    ivermectin cream canada cost

  61. Busdjdsk の発言:

    ivermectin rx http://stromectolabc.com/
    ivermectin tablets uk

  62. Busdjdsk の発言:

    cost of ivermectin lotion http://stromectolabc.com/
    stromectol for humans

  63. Busjdhj の発言:

    buy ivermectin pills http://stromectolabc.com/
    ivermectin cream 5%

  64. Busjdhj の発言:

    ivermectin 4000 mcg http://stromectolabc.com/
    ivermectin syrup

  65. Byusids の発言:

    zithromax online paypal buy zithromax without presc zithromax capsules australia
    zithromax price south africa https://azithromycin1st.com/
    where can you buy zithromax

  66. Buipsoldi の発言:

    zithromax 500mg price generic zithromax 500mg india buy zithromax without prescription online
    how much is zithromax 250 mg

  67. Nuisdksdl の発言:

    ivermectin lotion price https://stromectolis.com/
    ivermectin eye drops

  68. Clomid の発言:

    buy clomid online clomid alcohol buy clomid 50mg
    clomiphene for sale https://clomiphene1st.com/
    clomid tablet

  69. Tadaliko の発言:

    viagraorcialis cialis going generic cheap cialis canadian

  70. Ramonita の発言:

    I am actually thankful to the owner of this web page who
    has shared this enormous post at at this time.

  71. Twila の発言:

    Good day! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content.

    Please let me know. Many thanks

  72. Andy の発言:

    Hello, i read your blog from time to time and i own a similar one and i was just wondering if
    you get a lot of spam responses? If so how do you reduce
    it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any assistance is very much
    appreciated.

  73. Esther の発言:

    You actually make it appear really easy with your presentation but
    I find this matter to be actually one thing which I think I
    might by no means understand. It seems too complex and
    very wide for me. I am having a look ahead to your subsequent post, I’ll attempt to get the
    cling of it!

  74. Tami の発言:

    Wow, fantastic blog format! How long have you been blogging for?
    you made blogging look easy. The full look of your website is great, as well as the content!

  75. Kristi の発言:

    My brother suggested I might like this web site. He used to be totally
    right. This put up truly made my day. You cann’t imagine
    just how a lot time I had spent for this information! Thank you!

  76. Katherine の発言:

    Hello, i think that i saw you visited my website so i came
    to go back the desire?.I am trying to find issues to enhance my website!I assume
    its ok to make use of a few of your ideas!!

  77. Louanne の発言:

    Hello, this weekend is nice designed for me, as this time i am reading this fantastic educational article here at my home.

  78. Renee の発言:

    Thank you, I have just been searching for information approximately this subject for a long time and yours is the best I have
    came upon till now. But, what about the bottom line? Are you certain about the supply?

  79. child porn の発言:

    Seguri N.V firmasının güvencesi ile hizmet vermekte olan bu bahis platformunda kullanıcıların sadece kendi kimlik ve adres bilgilerini kullanarak kayıt yapmasına izin verilmektedir. Farklı kişilerin bilgilerini kullanarak kayıt yaptığı fark edilen kişilerin tespit edilmesi durumunda CIL lisans verisi ile korunan bu site hesabı kalıcı olarak askıya alacaktır. Sitede kullanıcıların hem spor bahisleri hem de casino oyunları konusunda sorunsuz şekilde deneyim elde etmesi için özel bir bahis market geliştirilmiştir. Anında kayıt olarak ve en güncel alan adını kullanarak bu bahis olanaklarının tadını çıkarabilirsiniz.

  80. Escort Kayseri の発言:

    Gerektiğinde gecelik görüşebilen sizin isteğinize göre saatlik ya da tek seans şeklinde de randevular veren esma yada sarışın güzeller kalite standartlarını çok yüksek tutuyor sağlıklı ilişkileri ile sizi güvenilir koşullarda misafir ederek mutlu ediyor. Elit kadınlara güvenin onlarla beraber harika ucuz girls ilişkileri yaşamak için harekete geçin. Enerji dolu seks ilişkileri paylaşan dinamik üniversiteli genç kızlarda Kayseri escort platformu içerisinde sizin için üyelik oluşturdu. Eve gelen ve otele gelen seçenekleri ile bu kızlardan randevu alabilir sınırsız seks ayrıcalığını onlarla beraber yaşayabilir hatta anal ve cimcif ilişkilerde de buluşabilirsiniz.

  81. Yanira の発言:

    Please let me know if you’re looking for a article writer for your blog.
    You have some really great posts and I believe I would be a good
    asset. If you ever want to take some of the load off, I’d
    love to write some articles for your blog in exchange for
    a link back to mine. Please blast me an e-mail if interested.

    Regards!

  82. child porn の発言:

    Nice article inspiring thanks. child porn , Google porn , sex izle , Baby porn

  83. Ciara の発言:

    Great article, exactly what I was looking for.

  84. Mamie の発言:

    Hi there very cool web site!! Guy .. Beautiful ..
    Amazing .. I’ll bookmark your blog and take the feeds additionally?
    I am glad to find a lot of helpful info here in the post, we’d like develop more strategies in this
    regard, thank you for sharing. . . . . .

  85. child porn の発言:

    Thank you for great information. child porn , Google porn , sex izle , Baby porn

  86. Dominique の発言:

    Whats up are using WordPress for your blog
    platform? I’m new to the blog world but I’m trying to get
    started and create my own. Do you require any coding knowledge to make your own blog?
    Any help would be really appreciated!

  87. Anna の発言:

    Hey There. I found your blog using msn. This is a very well written article.

    I’ll be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will certainly comeback.

  88. hotshot bald cop の発言:

    That’s a great point

  89. Bahis Forum の発言:

    Thank you great post. Bahis Forum – Canlı Spor Bahis Forumu, bedava bahis ve deneme bonusu casino forumu bahis forumu , deneme bonusu bahis forum ve en iyi bahis forumu, Forum Bahis

  90. Jetbahis の発言:

    Jetbahis giriş, bonus kampanyaları, üyelik ve kayıt işlemleri, para yatırma, çekme yöntemleri hakkında tüm detaylı bilgilere sayfamızdan …

  91. Mobilbahis; Bets10; Jetbahis; Discount Casino; Casino Metropol; Casino Maxi; Tipobet; Vdcasino; 1xbet; Piabet; Betvole; Betticket; Betlike; Supertotobet … Casino Siteleri

  92. Thank you great posting about essential oil. 国产线播放免费人成视频播放 国产线播放免费人成视频播放 b是不是越日越大 日本成本人av无码免费

コメントを残す

メールアドレスが公開されることはありません。