Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Java : ServletからServletへ値を送る方法

More than 1 year has passed since last update.

ServletからServletへ値を送る方法

request.setAttribute(java.lang.String name, java.lang.Object o)で値をセットし、getAttribute(java.lang.String name) で値を取り出す。

サンプル

  • フォルダ構成
    • webTest
      • Hoge.java
      • Test1.java
      • Test2.java
    • webTest2
      • Moge.java
    • WebContent
      • test.jsp
  • Test1.javaを実行すれば動きます
Test1.java
package webTest;


import java.io.IOException;

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

import webTest2.Moge;

/**
 * Servlet implementation class Test1
 */
@WebServlet("/Test1")
public class Test1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());

        Moge m = new Moge(1,2);

        request.setAttribute("bean", m);

        //JSPに遷移する
        RequestDispatcher disp = request.getRequestDispatcher("/Test2");
        disp.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Test2.java
package webTest;


import java.io.IOException;

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

import webTest2.Moge;

/**
 * Servlet implementation class Test1
 */
@WebServlet("/Test2")
public class Test2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());

        Moge m = (Moge)request.getAttribute("bean");
        Hoge h = new Hoge(3,4, m);

        request.setAttribute("bean", h);

        //JSPに遷移する
        RequestDispatcher disp = request.getRequestDispatcher("/test.jsp");
        disp.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
Moge.java
package webTest2;

public class Moge {
    private int a;
    private int b;

    public Moge(){

    }

    public Moge(int a, int b){
        this.a = a;
        this.b = b;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "Hoge [a=" + a + ", b=" + b + "]";
    }
}

Hoge.java
package webTest;

import webTest2.Moge;

public class Hoge {
    private int a;
    private int b;
    private Moge m;

    public Moge getM() {
        return m;
    }

    public void setM(Moge m) {
        this.m = m;
    }

    public Hoge(){

    }

    public Hoge(int a, int b, Moge m) {
        super();
        this.a = a;
        this.b = b;
        this.m = m;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "Hoge [a=" + a + ", b=" + b + ", m=" + m + "]";
    }


}

test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<jsp:useBean
        id="bean"
        class="webTest.Hoge"
        scope="request" />

<%@ page import="webTest2.Moge"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <%= bean.getM() %>
</body>
</html>

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away