1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

サーブレットのrequestメソッドのgetAttributeとgetParameterの違い

Last updated at Posted at 2021-02-12

サーブレットについて勉強しているときに
getParameterと間違ってgetAttributeを使っていて、
よくわからなかったのでメモ。

まずはフォームに入力した値を標準出力するjspを作ります。

Test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="TestServlet1">
text<input type="text" name="str">
<button type="submit" value="send">send</button>
</form>
</body>

入力して実行します。
この例ではhelloと入力をします。

20210212192227.png

formの値を受け取ったTestServlet1のdoGetメソッドが
下記コードであるとすると

TestServlet1.java
		String str = (String)request.getParameter("str");
		System.out.println(str);

コンソールには

console.
hello

と出力されます。

ところがgetParameterをgetAttributeとしてしまうと
String型にキャストしたとしても

TestServlet1_2.java
		String str = (String)request.getAttribute("str");
		System.out.println(str);

値はうけとれずnullになってしまいます

console.
null

#じゃあgetAttributeはいつ使えるのか・・・?

という疑問がありました。
さきほどのTestServletのdoGetメソッドを下記のように書き換えてみます。

TestServlet1.java
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = request.getParameter("str");
		System.out.println(str);
		request.setAttribute("str2", str);
		request.setAttribute("str3", str);
		RequestDispatcher rd = request.getRequestDispatcher("TestServlet2");
		rd.forward(request, response);
		rd = request.getRequestDispatcher("TestServlet3");
		rd.forward(request, response);
	}

これはフォームから受け取った値を
リクエストスコープにstr2,str3をセットするようにして
TestServlet2,TestServlet3にフォワード処理します。

このときリクエストスコープを通して
をサーブレットTestServlet2でstr2を,
サーブレットTestServlet3はstr3を受け取るようにします。

ただし、違うのは
TestServlet2ではgetAttributeで
TestServlet3ではgetParameterで
受け取るようにしているのがポイントです。

TestServlet2.java
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = (String)request.getAttribute("str2");
		System.out.println(str+"2");
	}
TestServlet3.java
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = (String)request.getParameter("str3");
		System.out.println(str+"3");
	}

これを実行すると実行すると
20210212193727.png

TestServlet2のgetAttributeではスコープの値を受け取れましたが、
getParameterでは下記のような500エラーがでました。

20210212194048.png

#メモまとめ

まとめると、

jsp → servlet:getParameter
servlet → servlet:getAttribute

という動作ですね。

とはいえ、私はtrl+spaceでコードしているとgetParameterのつもりが
いつのまにかgetAttributeしていることがあったりします・・・(--;

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?