LoginSignup
1
0

More than 3 years have passed since last update.

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

Posted at

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>

1
0
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
0