- PR -

Applet-Servlet通信について

1
投稿者投稿内容
n.w
大ベテラン
会議室デビュー日: 2003/07/15
投稿数: 126
お住まい・勤務地: 神奈川
投稿日時: 2004-06-30 15:56
お世話になってます。何度も質問ばかりで申し訳ないですが、
現在AppletからServletを呼び出す処理を作成していまして
その際Connectionがうまくいかず困ってます
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=7116&forum=12&3
を参考にしているのですが
コード:

URL url =new URL("http://localhost:8080/testProject/GenServlet");
URLConnection ucon = url.openConnection();
ucon.setDoOutput(true);
ucon.setDoInput(true);
// ucon.setUseCaches(false);
ucon.setRequestProperty("Content-type", "text/html");
ObjectOutputStream dos =new ObjectOutputStream(ucon.getOutputStream());


HTTP/1.1 404 /testProject/GenServlet
と404エラーが出てしまいます。

フォルダ構成としては
コード:

testProject --- Appletクラスのパッケージ/Appletクラス


|−−−− WEB-INF
|−−−classes - 直下にGenServlet配置


とし、Applet自体はhtmlはこちらでは作成せずeclipseにて実行しています。

現在作成しているものとは別にAppletクラス側で作成した文字列を
Servlet側で作成した文字列と連結してApplet画面に表示するとう
処理をテスト用に作成した際にはApplet−Servletのデータのやり取り
は確認できたのですが、

コード:

String str = "From Applet to Servlet"; //サーブレットに送るメッセージ
// Servletクラスを直に指定
URL url = new URL("http://localhost:8080/testProject/test/ServletTest");

URLConnection c = url.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.connect();
out = c.getOutputStream();
PrintWriter pout = new PrintWriter(out);
pout.print(str); //メッセージを送ったところ。
pout.close();
out.close();

in = c.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
//サーブレットからの返信を受け取る
in.close();
br.close();

tarea.setText(line);
//受け取った返信をテキストエリアに表示



今回とくに文字を送るなど必要ないのでPrintWriterなどは作成していませんが
getOutputメソッドやgetInputメソッドを使用しないと通信できないのでしょうか?
個人的にはServletの現在のエラーとしてはServletクラスをうまく見にいけてない
からだとは思うのですが、web.xmlに
コード:

<servlet>
<servlet-name>genTest</servlet-name>
<servlet-class>GenServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>genTest</servlet-name>
<url-pattern>/testProject/GenServlet</url-pattern>
</servlet-mapping>


とし URL url =new URL("http://localhost:8080/testProject/genTest");
としてもだめでした。長くなってしまいましたがご教授頂けると幸いです。
よろしくお願いします

<追記>
Tomcat 4.1
eclipse 2.1.2



[ メッセージ編集済み 編集者: n.w 編集日時 2004-06-30 16:03 ]

[ メッセージ編集済み 編集者: n.w 編集日時 2004-06-30 16:17 ]
n.w
大ベテラン
会議室デビュー日: 2003/07/15
投稿数: 126
お住まい・勤務地: 神奈川
投稿日時: 2004-06-30 17:09
現状以下の様な作りでアプレットから、Servletを呼び出していますが、
in = c.getInputStream();
を行った際に
IOException: Server returned HTTP response code:
500 for URL: http://localhost:8080/testProject/GenServlet
と落ちてしまいます。
変数cのURLConnectionがNullというわけでもなくc自体は値がとれているのですが
getInputStream時に落ちてしまいます。
デバッグをとってみたところサーバ側のログは出ていない状態なんですが、
入出力の値がうまくいってないのでしょうか?

--Applet----
コード:

URL url =new URL("http://localhost:8080/testProject/GenServlet");

URLConnection c = url.openConnection();
c.setDoOutput(true);

c.setDoInput(true);

c.connect();

out = c.getOutputStream();

PrintWriter pout = new PrintWriter(out);

pout.print("hogehoeg"); //メッセージを送ったところ。
pout.close();
out.close();

in = c.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//サーブレットからの返信を受け取る
String line = br.readLine();
in.close();
br.close();

--Servlet----

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();

try {
InputStream in = req.getInputStream();
//アプレットからの送信をキャッチ
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
out.print("Returned Message ======> " + line);

in.close();
br.close();

} catch (IOException ex) {

ex.printStackTrace();

} catch (Exception ex) {
ex.printStackTrace();
}

out.close();
}


java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/testProject/GenServlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:709)
at example.client.eReportForm.ConnectServlet(eReportForm.java:238)
at example.client.eReportForm.button1ActionPerformed(eReportForm.java:177)
at example.client.eReportForm.access[1code]
URL url =new URL("http://localhost:8080/testProject/GenServlet");

URLConnection c = url.openConnection();
c.setDoOutput(true);

c.setDoInput(true);

c.connect();

out = c.getOutputStream();

PrintWriter pout = new PrintWriter(out);

pout.print("hogehoeg"); //メッセージを送ったところ。
pout.close();
out.close();

in = c.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//サーブレットからの返信を受け取る
String line = br.readLine();
in.close();
br.close();

--Servlet----

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();

try {
InputStream in = req.getInputStream();
//アプレットからの送信をキャッチ
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
out.print("Returned Message ======> " + line);

in.close();
br.close();

} catch (IOException ex) {

ex.printStackTrace();

} catch (Exception ex) {
ex.printStackTrace();
}

out.close();
}


java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/testProject/GenServlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:709)
at example.client.eReportForm.ConnectServlet(eReportForm.java:238)
at example.client.eReportForm.button1ActionPerformed(eReportForm.java:177)
at example.client.eReportForm.access$0(eReportForm.java:138)
at example.client.eReportForm$1.actionPerformed(eReportForm.java:88)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(eReportForm.java:138)
at example.client.eReportForm.actionPerformed(eReportForm.java:88)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents
[/code1](eReportForm.java:138)
at example.client.eReportForm.actionPerformed(eReportForm.java:88)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(eReportForm.java:138)
at example.client.eReportForm.actionPerformed(eReportForm.java:88)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents




[ メッセージ編集済み 編集者: n.w 編集日時 2004-09-30 22:28 ]

[ メッセージ編集済み 編集者: n.w 編集日時 2004-09-30 22:30 ]
いっきゅう
大ベテラン
会議室デビュー日: 2004/04/04
投稿数: 153
お住まい・勤務地: 兵庫
投稿日時: 2004-07-01 09:57
> IOException: Server returned HTTP response code:
> 500 for URL: http://localhost:8080/testProject/GenServlet
> と落ちてしまいます。
> 変数cのURLConnectionがNullというわけでもなくc自体は値がとれているのですが
> getInputStream時に落ちてしまいます。
> デバッグをとってみたところサーバ側のログは出ていない状態なんですが、
> 入出力の値がうまくいってないのでしょうか?

ここまで分かっているならばサーバ側がdoGetで処理を受けているか
確認したのでしょうか?
受けていないような気がしますがどうなのでしょう?
n.w
大ベテラン
会議室デビュー日: 2003/07/15
投稿数: 126
お住まい・勤務地: 神奈川
投稿日時: 2004-07-01 11:59
引用:

ここまで分かっているならばサーバ側がdoGetで処理を受けているか
確認したのでしょうか?
受けていないような気がしますがどうなのでしょう?


サーブレット側ではSystem.out.printlnをひたすら
定義しているすべてのメソッドの先頭に入れてみたんですが
ログがでないというところまで確認してました。

結論をいうと、なんとか解決はできたのですが、今回色々
ツールが絡み合っている為、原因がまだはっきりしてなくて
気持ち悪い状況ですが、時間できて理由判明しましたら、今後
同じような状況に陥ってしまった人の為にも報告します。
(現状忙しすぎて目処たってませんが(^^;

いっきゅう様、アドバイスありがとうございました。
n.w
大ベテラン
会議室デビュー日: 2003/07/15
投稿数: 126
お住まい・勤務地: 神奈川
投稿日時: 2004-09-30 22:34
はるか昔の投稿になりますが、ある程度理由が判明したので
追記します。

上記原因として
1.webサーバとして使用しているIntramartの設定がおかしかったこと
(カレントディレクトリの認識がうまくいっていなかった)

2.Servlet mappingの設定がうまく認識されていなかったこと
が主な原因でした。

別処理をしていてわかったことですが、
コード:
    StringBuffer buf = new StringBuffer();
    buf.append("http://localhsot:8080/testProject/hogeServlet");
    buf.append("?subtitle=hoge"); // 第1引数 


とし、URLクラスをもちいて
コード:
    URL url = new URL(buf.toString());
    AppletContext#showDocument(url, "_blank");


とウィンドウを新たにサーブレット指定のURLから開いたと
サーブレット側にて
HttpServletRequest#getParameter("subtitle");
でjsp画面などから飛ばした時と同じ用にhogeという値がとれ
今スレッドのなぞだったところは解決しました。

Applet−Servet連携で値渡しを散々検索した際には
URLConnectionなどを使いprintWriterに文字列を渡す方法しか
見つけることができなかったので追記してみました。

実は周知なことだったり?
1

スキルアップ/キャリアアップ(JOB@IT)