[JAVA] [For myself] Transfer of Servlet class processing

PROBLEM TO BE SOLVED: To generate a random number and forward when an even number is generated. If an odd number is generated, transfer the process to the redirect

[Servlet class]

package p185;

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("/P185")
public class P185 extends HttpServlet {
	private static final long serialVersionUID = 1L;
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int Ran=(int)(Math.random()*10);
		if(Ran%2==1) {
			response.sendRedirect("/example/redirected62.jsp");
		}else{
			RequestDispatcher dispatcher =request.getRequestDispatcher("/forwarded62.jsp");
			dispatcher.forward(request,response);
		}
	}
}


a

[Redirect (jsp file)]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Redirect sample</title>
</head>
<body>
<h1>Show redirect sample</h1>
</body>
</html>

[Forward (jsp file)]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Forward sample</title>
</head>
<body>
<h1>View forward sample</h1>
</body>
</html>

Recommended Posts

[For myself] Transfer of Servlet class processing
Nested class (for myself)
Summary of rails validation (for myself)
Summary of Java environment settings for myself [mac]
Implementation of asynchronous processing for single tenant in Tomcat
Use of Date class
[For myself] Browser display using JSP files and Servlet classes
Aiming for a basic understanding of the flow of recursive processing