【Java】2つのJSPファイル間で画面遷移を行う

■実装するプログラムの仕様

2つのJSPファイルを使用して画面遷移を行う。

▼画面遷移イメージ図

「画面遷移はここをクリック!」ボタンを押下して、次の画面(JSPファイル)に遷移する。

▼URL

遷移元:
http://localhost:8080/JspServletSample/sample_jsp_from.jsp

遷移先:
http://localhost:8080/JspServletSample/hoge/sample_jsp_to.jsp

▼ディレクトリ階層イメージ

■画面仕様

▼sample_jsp_from.jsp

①「画面遷移はここをクリック!」を押下し、「sample_jsp_to」へ遷移する。

▼sample_jsp_to.jsp

①「画面遷移しました!」を表示する。

■サンプルソース

▼sample_jsp_from.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%-- 画面遷移先のURLを入力 --%>
<% String url_name = "http://localhost:8080/JspServletSample/hoge/sample_jsp_to.jsp"; %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>画面遷移前</title>
</head>
<body>

<a href=<%=url_name %>>画面遷移はここをクリック!</a>

</body>
</html>

▼sample_jsp_to.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<% String message = "画面遷移しました!"; %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>画面遷移後</title>
</head>
<body>

<p><%=message %></p>

</body>
</html>

■実行結果

▼sample_jsp_from.jsp

「画面遷移はここをクリック!」を押下

▼sample_jsp_to.jsp

タイトルとURLをコピーしました