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.
Click on the image to see it! Will increase. The value is stored in the application scope. https://iineko.herokuapp.com
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
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.
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.
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