Java Servlet / JSP View drawing

Differences depending on the transfer method

- ** [Forward](#Drawing view by forward) ** </ font> ... Can be used only when the transfer source and transfer destination are the same application. .. - ** [Redirect](#Redirect to draw view) ** </ font> ... Use when the transfer source and transfer destination are different applications. It has the disadvantage of slow transfer speed.

<!-Explanation of transfer method using forward from here! ->

Drawing View by forward

Use ** Forward ** </ font> to request the output processing to the JSP file. Use the forward () method of the `@ RequestDispatcher instance. ``

foward()syntax


RequestDispatcher dispatcher = request.getRequestDispatcher("Forward destination");
dispatcher.foward(request, response);

Sample program

Use forward to output the JSP file from the Servlet class.

ForwardServlet.java


package servlet;

import java.io.IOException;

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

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

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    //forward
    RequestDispatcher dispatcher = request.getRecestDispatcher("/WEB-INF/jsp/forward.jsp");
    dispatcher.forward(request, response);
  }
}

forward.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample page</title>
</head>
<body>
<h1>This is a sample</h1>
<p>Sample page</p>
</body>
</html>

result

This is a sample
Sample page

<!-Explanation of transfer method by redirect from here! ->

Drawing View by redirect

Redirect </ font> is a method to transfer the process by changing the request destination of the browser. It is used when the transfer source and transfer destination applications are different. Use the sendRedirect () method of the `@ HttpServletResponse instance. ``

Redirect syntax


response.sendRedirect("Redirect URL");

You can specify the redirect destination using a URL or, within the application, a relative path.

Sample program

RedirectServlet.java


package servlet;

import java.io.IOException;

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("/RedirectServlet")
public class RedirectServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

protected void doGet (HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    
    //redirect
    response.sendRedirect("/sample/SampleServlet");
  }
}


Next: What is Request Scope?

Recommended Posts