Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active September 13, 2019 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cartman0/c2e76a36a87fa88988ee to your computer and use it in GitHub Desktop.
Save Cartman0/c2e76a36a87fa88988ee to your computer and use it in GitHub Desktop.
Beanクラスを使ったSample
package beansample;
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;
/**
* MVCモデルクラス.
* C(コントローラ)
*/
@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* コンストラクタ.
*/
public ControllerServlet() {
super();
}
/**
* POSTメソッドでリクエストされた場合の処理.
* @param request
* @param response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ID入力画面で入力されたIDを取得
String id = request.getParameter("id");
// IDに紐づくユーザ情報をモデルに格納するために指示
// ID処理クラスをインスタンス化
IdProcessing ip = new IdProcessing();
// ID処理クラスにIDを渡してユーザ情報をモデルに格納
UserBean bean = ip.getUserData(id);
// ビューに画面を表示させるための準備
RequestDispatcher rd;
// モデルの情報を判定
if(bean != null) {
// モデルの情報が存在する場合
// setAttributeメソッドを使ってモデルの情報をセット
request.setAttribute("user", bean);
// つぎに表示させる画面(ビュー)を指定
rd = request.getRequestDispatcher("./userResponse.jsp");
} else {
// モデルの情報が存在しない(IDに紐づくユーザ情報がない)場合
// エラー画面へ
rd = request.getRequestDispatcher("./userError.jsp");
}
// ③-2 つぎの画面を表示
rd.forward(request, response);
}
}
package beansample;
/**
* ID処理クラス.
*/
public class IdProcessing {
/**
* 指定されたIDに紐づくユーザ情報を返却します.
* @param id ID
* @return ユーザ情報
*/
public UserBean getUserData(String id) {
UserBean user = null;
// 引数のIDを判定
if("web01".equals(id)) {
// IDがweb01の場合
// Beanに名前を設定
String name = "Cartman Taro";
// Beanに年齢を設定
int age = 17;
// 管理者権限を設定
int auth = 1;
user = new UserBean(id, name, age, auth);
} else if ("web02".equals(id)) {
// IDがweb02の場合
// Beanに名前を設定
String name = "Cartman Jiro";
// Beanに年齢を設定
int age = 10;
// 一般権限を設定
int auth = 0;
user = new UserBean(id, name, age, auth);
}
return user;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package beansample;
import java.io.Serializable;
/**
* MVCモデル
* M (モデル)
*/
public class UserBean implements Serializable{
private static final long serialVersionUID = 1L;
private String id_; // ID
private String name_; // 名前
private int age_; // 年齢
private int auth_; // 権限(管理者権限:1 一般権限:0)
// constructor
public UserBean(){
this.id_ = "";
this.id_ = "";
this.age_ = -1;
this.auth_ = -1;
}
public UserBean(String id, String name, int age, int auth){
this.id_ = id;
this.name_ = name;
this.age_ = age;
this.auth_ = auth;
}
// id
public void setId(String id){
this.id_ = id;
}
public String getId(){
return this.id_;
}
// name
public void setName(String name){
this.name_ = name;
}
public String getName(){
return this.name_;
}
// age
public void setAge(int age){
this.age_ = age;
}
public int getAge(){
return this.age_;
}
// auth
public void setAuth(int auth){
this.auth_ = auth;
}
public int getAuth(){
return this.auth_;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Bean Sample Error</title>
</head>
<body>
<p>入力されたユーザは存在しません.</p>
<form>
<input type="button" onclick="location.href = './userRequest.jsp'" value="戻る">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%-- MVCモデル V(ビュー) --%>
<%-- ID入力画面 --%>
<!DOCTYPE html>
<html>
<head>
<title>Bean Sample</title>
</head>
<body>
<h1>インプットフォーム</h1>
<%-- POSTメソッドでテキストを送信 --%>
<form action="./ControllerServlet" method="post">
<p>IDを入力してください:<input type="text" name="id"></p>
<p>IDはweb01またはweb02です</p>
<input type="submit" value="リクエスト">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<jsp:useBean id="user" scope="request" class="beansample.UserBean" />
<!DOCTYPE html>
<html>
<head>
<title>Sample Bean</title>
</head>
<body>
<h1>リクエスト結果</h1>
<%--一般権限の場合はIDと年齢を非表示にする--%>
<% if (user.getAuth() == 1) {%>
<p>ID:<%=user.getId()%></p>
<% }%>
<p>名前:<%=user.getName()%></p>
<% if (user.getAuth() == 1) {%>
<p>年齢:<%=user.getAge()%></p>
<% }%>
<br>
<% if (user.getAuth() == 1) { %>
<p>ID:<jsp:getProperty property="id" name="user"/></p>
<% } %>
<p>名前:<jsp:getProperty property="name" name="user"/></p>
<% if (user.getAuth() == 1) { %>
<p>年齢:<jsp:getProperty property="age" name="user"/></p>
<% }%>
<form>
<input type="button" onclick="location.href = './userRequest.jsp'" value="戻る">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment