Java: How to send values from Servlet to Servlet

How to send values from Servlet to Servlet

Set the value with request.setAttribute (java.lang.String name, java.lang.Object o) and retrieve the value with getAttribute (java.lang.String name).

sample

--Folder structure - webTest - Hoge.java - Test1.java - Test2.java - webTest2 - Moge.java - WebContent - test.jsp --Run Test1.java and it will work

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 to 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 to 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

Java: How to send values from Servlet to Servlet
Try passing values from Java Servlet to iPhone app using JSON
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
[ruby] How to receive values from standard input?
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
How to use java Optional
New features from Java7 to Java8
[Java] How to extract the file name from the path
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to change from Oracle Java 8 to Adopt Open JDK 9
How to set Java constants
[Java] How to erase a specific character from a character string
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to migrate from JUnit4 to JUnit5
How to initialize Java array
How to deploy a simple Java Servlet app on Heroku
Periodically send accelerometer values from Android to PC via UDP
[Java] How to convert a character string from String type to byte type
How to write and notes when migrating from VB to JAVA
How to study Java Silver SE 8
How to use Java HttpClient (Get)
How to use scope (JSP & Servlet)
How to push from Tarminal to GitHub
Studying Java # 6 (How to write blocks)
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
Kotlin Class to send to Java developers
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to create a folder
How to decompile java class files
[Java] How to use LinkedHashMap class
[Swift] How to send a notification
From Java to VB.NET-Writing Contrast Memo-
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration
Java, interface to start from beginner