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
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
<%@ 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>
<%@ 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