Passing values from servlet to jsp

Step 1: ServletCall.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Javadomain.in</title>
</head>
<body>
Click the below button to call the servlet, then servlet will print the value in the display.jsp file
<br>
<br>
<form action="ServletValues">
<input type="submit">
</form>
</body>
</html>

 

Step 2: ServletValues.java

 

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;

@WebServlet("/ServletValues")
public class ServletValues extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public ServletValues() {
        super();

    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String input = "Welcome to Javadomain.in";
		request.setAttribute("servlet_value",input);
		RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
		rd.forward(request, response);
	}

}

 

Step 3: display.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Javadomain.in</title>
</head>
<body>
The values from the servlet (ServletValues) is,
<br>
<br>
${servlet_value}
<!-- servlet_value is set in the ServletValues servlet.
Read more.....