Implementation of like function in Java

iineko.PNG

It seems that it is better to output more and more, so I will publish the like application I made for the first time. I made it with MVC model.

Overview

Click on the image to see it! Will increase. The value is stored in the application scope. https://iineko.herokuapp.com

Where I had a hard time

  1. I struggled because I didn't know how to send the value from HTML without screen transition. How to send values in HTML without screen transition

  2. Even if you press Like, it will not be more than 1. Every time I liked it, I made a new one, so I made the first start judgment.

  3. I didn't know how to pass an instance across classes. In YoineServelet.java, Yoine y = new Yoine (); Add 1 like to Yoine y in YoineLogic.java I wanted to set it in Yoine.java I was worried about what to do if I made a new instance with Yoine Servelet and made a new instance with Yoine Logic. ↓ Changed to receive Yoine type as an argument in Yoine Logic.

Source code

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>Good cat! !! !!</title>
</head>
<body>
<div class="ta1">
<p><a href="/iine/YoineServlet?action=yoine">
<img src="/iine/finger-163689_640.jpg " alt="How nice!" width="300" height="200"></a>
<a href="/iine/YoineServlet?action=yoine">
<img src="/iine/2cat-323262_1280.jpg " alt="How nice!" width="300" height="200"></a>
</p>
<p><font size="5"><b>Good cat! :${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 {

		//Processing to determine the first startup
		//Get value from application scope
		ServletContext sc = this.getServletContext();
		Yoine y = (Yoine) sc.getAttribute("yoine");

		//Continuation of initial start judgment
		//New if the application scope has no value
		if(y == null) {
			y = new Yoine();
			sc.setAttribute("yoine", y);
		}

		//Get request parameters
			request.setCharacterEncoding("UTF-8");
			String yoine = request.getParameter("action");


		//If the like button is pressed
		if (yoine != null) {

			//Add likes with Yoine Logic
			YoineLogic yl = new YoineLogic();
			yl.yoinePlus(y);

			//Save the number of likes in the application scope
			sc.setAttribute("yoine", y);
		}

		//forward
		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 {

	}

}

Recommended Posts

Implementation of like function in Java
Implementation of like function (Ajax)
Implementation of gzip in java
[Rails] Implementation of like function
Implementation of tri-tree in Java
[Rails] Asynchronous implementation of like function
Rails sorting function implementation (displayed in order of number of like)
Implementation of DBlayer in Java (RDB, MySQL)
[Ruby on rails] Implementation of like function
Interpreter implementation in Java
Boyer-Moore implementation in Java
Heapsort implementation (in java)
Implementation of pagination function
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 8 Implementation of editing function
[Rails] Implementation of retweet function in SNS application
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Implementation of sequential search function
[Rails 6] Implementation of search function
Implementation of image preview function
[Java] Implementation of Faistel Network
Implementation of category pull-down function
[Rails] Implementation of tutorial function
Implementation of HashMap in kotlin
[Rails] Implementation of CSV import function
[Rails] Implementation of image preview function
Implementation of ls command in Ruby
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implement PHP implode function in Java
List of members added in Java 9
[Rails 6] Like function (synchronous → asynchronous) implementation
List of types added in Java 9
Error encountered in tagging function implementation
[Rails] Implementation of new registration function in wizard format using devise
Mechanism and characteristics of Collection implementation class often used in Java
Implementation of clone method for Java Record
[Java] I participated in ABC-188 of Atcorder.
Get the result of POST in Java
Implementation of user authentication function using devise (2)
Implementation of multi-tenant asynchronous processing in Tomcat
Various things like bit flags in Java
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
Immutable (immutable) List object conversion function in Java8
Implement something like a stack in Java
[Vue.js] Implementation of menu function Implementation version rails6
The story of writing Java in Emacs
[Vue.js] Implementation of menu function Vue.js introduction rails6
Role of JSP in Web application [Java]
Discrimination of Enums in Java 7 and above
DM function implementation
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
This and that of the implementation of date judgment within the period in Java
Implementation of GKAccessPoint
Comparison of thread implementation methods in Java and lambda expression description method
Pi in Java