Définissez la valeur avec request.setAttribute (nom java.lang.String, java.lang.Object o) et récupérez la valeur avec getAttribute (nom java.lang.String).
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);
//Transition vers 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);
//Transition vers 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>
Recommended Posts