close

portfolio


Javaで作った商品登録っぽいもの

コードの一部を下記に載せます。(PCのみ)


controller(Java)

package controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.Product;

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

	/**
		* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		*/
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd=request.getRequestDispatcher("WEB-INF/view/main.jsp");
		rd.forward(request, response);
	}

	/**
		* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
		*/
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application=this.getServletContext();
		List list=(List)application.getAttribute("list");
		if(list==null) {
			list=new ArrayList<>();
		}

		request.setCharacterEncoding("UTF-8");

		String name=request.getParameter("name");
		String price=request.getParameter("price");

		if(name.isEmpty() || price.isEmpty()) {
			request.setAttribute("err", "未記入の項目があります!");
		}else {
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String updated=sdf.format(date);
			Product product=new Product(name,price,updated);
			list.add(0,product);
			application.setAttribute("list", list);
			request.setAttribute("msg", "1件登録しました。");
		}

		doGet(request,response);
	}

}
						

↑TOP↑

model(Java)

package model;

import java.io.Serializable;

public class Product implements Serializable{
	private String name;
	private String price;
	private String updated;

	//コンストラクタ
	public Product() {

	}
	public Product(String name,String price,String updated) {
		this.name=name;
		this.price=price;
		this.updated=updated;
	}

	//getter&setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

}


↑TOP↑

viewについては下記のnoteにてご確認いただけます。



noteの記事はこちら
Javaで作ったねこあるきゲーム

アドベンチャー(Java)

小説イメージ画像

ややミステリー(ラノベ)

チーム開発実習で作ったミニゲーム集

チーム開発実習(Unity)

QRコード

note

↑TOP↑