2009年8月10日月曜日

GAE/Jの間違いやすい?部分

参考ページ
参考ページ2
ここで詰まるのは少数派かもしれないがHttpURLConnectionクラスでリクエストを行うと日本語が文字化けを起こす。具体的にはPostRequest.java からのリクエストです→PostRequest.java???????????となってしまう。私は
package org.test;

import java.net.*;
import java.io.*;
import javax.servlet.http.*;

public class PostRequest extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
PrintWriter out = resp.getWriter();

URL url = new URL("http://localhost:8080/html/index.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.close();

if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
resp.setContentType("text/html;charset=UTF-8");
String line;
while( (line = reader.readLine()) != null ) {
out.println(line);
}
reader.close();
} else {
resp.setContentType("text/html;charset=UTF-8");
out.println("error");
}
}
}
のように書いていたが太字の部分をPrintWriter out = resp.getWriter();の上に書かなければならない。
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
//以下ほとんど同文


/html/index.html
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>PostRequest.javaからのリクエスト</title>

</head>
<body>
<p>PostRequest.java からのリクエストです</p>
</body>
</html>

2009年8月4日火曜日

Protocol Buffersについて勉強をする

参考ページ
JavaでProtocolBuffersを扱うにはソースコードからビルドする必要がある。基本的な手順は参考ページに書いてあるが細かいところでトラブルが起き、意外と手間がかかる。

作業ディレクトリについて
「ソースコードのルートディレクトリ/java」に移動と書いてあるがどれがそれなのか分かりにくい。ソースコードのルートディレクトリってどれ?ってことになる。
protobuf-*/javaディレクトリで作業を行う必要がある。

Java用ランタイムライブラリをビルドするときの注意点
OpenJDKではビルドエラーが発生してしまうため、環境変数JAVA_HOMEを適切なものにするために~/.bash_profileを

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.14/jre
export PAHT=$PATH:$JAVA_HOME
と編集し、
$ source ~/.bash_profile

とコマンドを実行

2009年8月3日月曜日

Protocol Buffersについて勉強

参考ページ
主にJavaについて参考にさせていただきました。あと、付属のサンプルも。

現時点で正直あんまり理解できていないがサンプルなどから推測するにあるデータがどのような構成になっているかを表現しているんじゃないかと思う。

//多分、違うのだろうから正解が分かったらちゃんと削除するようにしてください。

2009年8月1日土曜日

GAE for Javaをターミナルで開発する(AntによるJDOの初期設定について)

参考ページ
これによるとJDOを使用するのにantを用いる必要がある。
build.xmlファイルに
  <target name="datanucleusenhance" depends="compile"
description="Performs JDO enhancement on compiled data classes.">
<enhance_war war="war" />
</target>
を追加する必要があるがnew_project_templateからコピーし、作成したプロジェクトではこれだけではいけない。
$ ant datanucleusenhance
 
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-6-openjdk/lib/tools.jar
Buildfile: build.xml

compile:

datanucleusenhance:

BUILD FAILED
/home/something/gcode/appengine-java-sdk-1.2.0/demos/New_Project/build.xml:83: The following error occurred while executing this line:
/home/something/gcode/appengine-java-sdk-1.2.0/config/user/ant-macros.xml:94: /home/something/gcode/appengine-java-sdk-1.2.0/demos/New_Project/war/WEB-INF/classes not found.

Total time: 0 seconds
というエラーが出る
そのため
  <target name="datanucleusenhance" depends="compile"
description="Performs JDO enhancement on compiled data classes.">
<enhance_war war="${war.dir}" />
</target>
と太字の部分を変更する必要がある。

2009年7月29日水曜日

Google map apiのローカルドライブでの開発

file:///home/directory/gmap.htmlにアクセス
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>

//下のsrc属性にkeyパラメータがなくてもローカルドライブでは開発できる
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false"
type="text/javascript"></script>
<script type="text/javascript">

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
}
}

</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>
</html>

2009年7月26日日曜日

GAE for Javaをターミナルで開発する (Antでコンパイル)

参考ページ 厳密にはfork属性を指定する意味を理解するのに参考にした。

build.xmlファイルがあるディレクトリに移動

デフォルトでは
$ ant compile

BUILD FAILED
/path/appengine-java-sdk-1.2.0/demos/guestbook/build.xml:39: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"

Total time: 0 seconds

とエラーが出る。

  <target name="compile" depends="copyjars"
description="Compiles Java source and copies other source files to the WAR.">
<mkdir dir="war/WEB-INF/classes" />
<copy todir="war/WEB-INF/classes">
<fileset dir="src">
<exclude name="**/*.java" />
</fileset>
</copy>
<javac
srcdir="src"
destdir="war/WEB-INF/classes"
classpathref="project.classpath"
debug="on"
fork="true" />
</target>
※太字の部分を追加fork属性を追加することでコンパイルが可能になった。開発サーバは毎回停止しなければいけないらしい。

2009年7月25日土曜日

環境変数のいろいろ

環境変数を追加する
例:
$ export ECLIPSE_PATH=/usr/eclipse
$ export PATH=$PATH:$ECLIPSE_PATH
$ source .bash_profile

環境変数を削除するにはunsetコマンドを使う
例:$ unset ECLIPSE_PATH

環境変数を表示するにはenv,printenvを使う。この2つがどう違うのかはまだ不明。