【JSP】【Servlet】ページ遷移およびパラメータの受け取り ~クエリ文字列編~

構文

ページ遷移

RequestDispatcher dispatcher = request.getRequestDispatcher("/【ページ(クラス名)】");
dispatcher.forward(request, response);

パラメータの受け渡し

 * <form>タグ 内の <input name="【キー名】" value="【値】">の値を取得する方法。

単一のパラメータ

String 【値】 = request.getParameter("【キー名】");

同名で複数のパラメータ

String[] 【値】 = request.getParameterValues("【キー名】");

サンプル

JSP

Sample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
// ★ここに注目★
String param = request.getParameter("parameter1");
String[] params = request.getParameterValues("parameter2");

if (param == null) {
	param = "This is Null.";
}
if (params == null || params.length == 0) {
	params = new String[] {"These are Nulls."};
}
%>
param : <%=param %>
params : 
<% for (String value : params) { %>
	<%=value %>
<% } %>
</body>
</html>

出力

出力例1

[http://localhost:8080/SampleWeb/Sample.jsp]
param : This is Null. params : These are Nulls.

出力例2

[http://localhost:8080/SampleWeb/Sample.jsp?parameter1=test&parameter2=test2&parameter2=test3]
param : test params : test2 test3

Servlet

FirstPageServlet.java

package com.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstPageServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
       
    public FirstPageServlet() {
        super();
    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           request.setAttribute("Key001", "Value001");
           RequestDispatcher dispatcher = request.getRequestDispatcher("/OtherPageServlet");
           dispatcher.forward(request, response);
     }
}

OtherPageServlet.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OtherPageServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
       
    public OtherPageServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html; charset=UTF-8");

         PrintWriter out = response.getWriter();
         
         Object value = request.getAttribute("Key001");
         if (value == null) {
              value = "";
         }
         /* HTML出力 */
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Hello World</title>");
         out.println("</head>"); 
         out.println("<body>");
         out.println("<H1>");
         out.println("Result : " + value);
         out.println("<H1>");
         out.println("</body>");
         out.println("</html>");
         out.close();
     }
}

出力

出力例

[http://localhost:8080/SampleWebAppli/FirstPageServlet]
Result : Value001 

トラブルシューティング

例外「JasperException: An exception occurred processing JSP page」が発生する

例外内容

org.apache.jasper.JasperException: An exception occurred processing JSP page /Sample.jsp at line 17

原因

受け取った変数が、nullだったため。

解決策

nullチェックをする

■ 例
if (param == null) {
	param = "This is Null.";
}
if (params == null || params.length == 0) {
	params = new String[] {"These are Nulls."};
}

変数について

 * リクエスト以外に他にどんな変数があるのか?
http://struts.wasureppoi.com/jsp/01_scope.html
http://ash.jp/java/webapp_scope.htm
より

 * セッション(session) : 複数のリクエストをまたいで、情報共有
 * リクエスト(request) : 1回のリクエスト内で、情報共有
 * アプリケーション(application) : Webプリケーション全体で、情報共有
 * ページ(page) : 同一のJSPファイル内で、情報共有


関連記事

ページ遷移およびパラメータの受け取り ~クエリ文字列編~

http://blogs.yahoo.co.jp/dk521123/32776759.html

ページ遷移およびパラメータの受け取り ~セッション編~

http://blogs.yahoo.co.jp/dk521123/33118263.html

Servlet/JSP/useBeanタグ を使って、MVCモデル

http://blogs.yahoo.co.jp/dk521123/33118025.html

Servlet + JSP でログイン機能を実装する

http://blogs.yahoo.co.jp/dk521123/33567439.html

ページ遷移の種類

http://blogs.yahoo.co.jp/dk521123/33601438.html