LoginSignup
7
10

More than 3 years have passed since last update.

Javaでいいね機能の実装

Posted at

iineko.PNG

アウトプットはどんどんした方がいいらしいので、初めて作ったいいねアプリの公開をします。
MVCモデルで作りました。

概要

画像をクリックするといいねこ!が増えます。
値はアプリケーションスコープに保存しています。
https://iineko.herokuapp.com

苦労したところ

1、HTMLから画面遷移せずに値を送る方法がわからずに四苦八苦しました。
画面遷移せずにHTMLで値を送る方法

2、いいねを押しても1以上にならない。
いいねされる度に毎回newしていたので初回起動判定を入れた。

3、クラスをまたいでインスタンスを渡す方法がわからなかった。
YoineServelet.javaでYoine y = new Yoine();して
YoineLogic.javaでYoine yにいいねを1加算して
Yoine.javaにsetしたかったが
YoineServeletでnewしてYoineLogicでもnewしたら別のインスタンスになっちゃうしどうするんだと悩んでしまった。

YoineLogicでYoine型を引数として受け取るようにした。

ソースコード

model

Yoine.java
package model;

import java.io.Serializable;

public class Yoine implements Serializable {
    private int yoineCount = 0;

    public void setYoineCount(int yoineCount) {
        this.yoineCount = yoineCount;
    }

    public int getYoineCount() {
        return yoineCount;
    }
}
YoineLogic.java
package model;

public class YoineLogic {

    public void yoinePlus(Yoine y) {
        int count = y.getYoineCount();
        count++;
        y.setYoineCount(count);
    }
}

View

yoineView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
<!--
   div.ta0 {text-align: left;}
   div.ta1 {text-align: center;}
   div.ta2 {text-align: right;}
   div.ta3 {text-align: justify;}
-->
</style>
<title>いいねこ!!!</title>
</head>
<body>
<div class="ta1">
<p><a href="/iine/YoineServlet?action=yoine">
<img src="/iine/finger-163689_640.jpg" alt="いいね!" width="300" height="200"></a>
<a href="/iine/YoineServlet?action=yoine">
<img src="/iine/2cat-323262_1280.jpg" alt="いいね!" width="300" height="200"></a>
</p>
<p><font size="5"><b>いいねこ!:${yoine.yoineCount}</b></font></p>
</div>
</body>
</html>

Controller

YoineServlet.java
package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Yoine;
import model.YoineLogic;

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public YoineServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 初回起動を判定するための処理
        // アプリケーションスコープから値を取得
        ServletContext sc = this.getServletContext();
        Yoine y = (Yoine) sc.getAttribute("yoine");

        // 初回起動判定の続き
        // アプリケーションスコープに値がなければnewする
        if(y == null) {
            y = new Yoine();
            sc.setAttribute("yoine", y);
        }

        // リクエストパラメーターの取得
            request.setCharacterEncoding("UTF-8");
            String yoine = request.getParameter("action");


        // いいねボタン押されたら
        if (yoine != null) {

            // YoineLogicでいいねを加算
            YoineLogic yl = new YoineLogic();
            yl.yoinePlus(y);

            // いいねの数をアプリケーションスコープに保存
            sc.setAttribute("yoine", y);
        }

        // フォワード
        RequestDispatcher rd = request.getRequestDispatcher("/yoineView.jsp");
        rd.forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

}

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10