request.setAttribute("input", param);
request.setAttribute("result", result);<%=request.getAttribute("result") %>
<%=request.getAttribute("input") %>《HttpServletRequest》.setAttribute( 名前 , 値 );
・値の取得変数 = 《HttpServletRequest》.getAttribute( 名前 );
ServletContext app = this.getServletContext();ServletContextというのは、現在動いているWebアプリケーションを管理するためのものです。このインスタンス内に、Webアプリケーションに関する重要な機能がまとめられています。
RequestDispatcher dispatcher = app.getRequestDispatcher("/helo.jsp");ServletContextから「RequestDispatcher」というインスタンスを取得します。これは、リクエストをディスパッチ(別のものに掛け替える)するためのものです。まぁ、とりあえずは「この中に、フォワードがメソッドとして用意されているんだ」という点だけわかればOKでしょう。try {
dispatcher.forward(request, response);
} catch (ServletException e) {
out.println(e);
}取得したRequestDispatcherに用意されている「forward」でフォワードが実行されます。引数にはHttpServletRequestとHttpServletResponseを指定します。これらは、いずれもdoGet/doPostで引数に渡されるインスタンスをそのまま設定しておけばよいでしょう。※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
※helo.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample jsp</title>
<style>
h1{
font-size: 16pt;
background: #AAFFAA;
padding: 5px;
}
</style>
</head>
<body>
<h1>Hello App Engine!</h1>
<p>Result: <%=request.getAttribute("result") %></p>
<hr>
<p id="msg">※整数を入力:</p>
<form method="post" action="/mygaeapp">
<table>
<tr>
<td>入力</td>
<td><input type="text" id="input" name="text1"
value="<%=request.getAttribute("input") %>"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="送信する">
</td>
</tr>
</table>
</form>
</body>
</html>
※MyGaeAppServlet.java
package com.tuyano.libro.mygaeapp;
import java.io.*;
import java.net.URLDecoder;
import javax.servlet.*;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MyGaeAppServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/plain");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
out.println("Hello, world!");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
String param = URLDecoder.decode(request.getParameter("text1"),"utf8");
PrintWriter out = response.getWriter();
int result = 0;
try {
int n = Integer.parseInt(param);
for(int i = 1;i <= n;i++){
result += i;
}
} catch (NumberFormatException e) {
out.println(e);
}
request.setAttribute("input", param);
request.setAttribute("result", result);
ServletContext app = this.getServletContext();
RequestDispatcher dispatcher = app.getRequestDispatcher("/helo.jsp");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
out.println(e);
}
}
}
| << 前へ | 次へ >> |