0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[サーブレット&JSP]子供の身長予測するwebアプリケーションを作成 JAVA

Posted at

サーブレット&jspで子供の身長を予測するwebアプリケーションを作成しました。

今回MVCを完全に無視しました。なのでサーブレット側にロジック部分も記述しております。

プルダウンから男子か女子を選択する

スクリーンショット 2022-07-03 0.14.05.png

お父さんの身長とお母さんの身長を入力

スクリーンショット 2022-07-03 0.14.41.png

form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%Integer childheight = (Integer)session.getAttribute("childheight");%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<!-- 子供の身長予測 -->
<form action="height" method="post">
	<div>
		<select name="gender">
		<option value="男子">男子</option>
		<option value="女子">女子</option>
	</select>
	</div>
	
	 <p>お父さんの身長<p/>
	 <input type="text" name="fatherheight"><br>
	 <p>お母さんの身長<p/>
	 <input type="text" name="motherheight"><br>
	 
	 <input type="submit" name="submit" value="送信" />
	
</form>

<% if(childheight != null) { %>
  <%=("お子様の予想身長は") %>
  <%=(childheight) %>
  <%=("cmです") %>
<% }%>

</body>
</html>
User.java
package beans;

public class User {
	
	private String gender;
	private String name;
	private int fatherheight;
	

	private int motherheight;

	public User(String name) {
			super();
			this.name = name;
		}

	public User() {
		
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	  }
	
	public int getFatherheight() {
		return fatherheight;
	}

	public void setFatherheight(int fatherheight) {
		this.fatherheight = fatherheight;
	}

	public int getMotherheight() {
		return motherheight;
	}

	public void setMotherheight(int motherheight) {
		this.motherheight = motherheight;
	}
	
	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}


}

height.java
package サーブレット;

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;
import javax.servlet.http.HttpSession;

import beans.User;

/**
 * Servlet implementation class height
 */
@WebServlet("/height")
public class height extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public height() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String gender= request.getParameter("gender");
		String fatherheight= request.getParameter("fatherheight");
		String motherheight= request.getParameter("motherheight");
		
		int number1 = Integer.parseInt(fatherheight);
		int number2 = Integer.parseInt(motherheight);
		
		User u = new User();
		u.setGender(gender);
		u.setFatherheight(number1);
		u.setMotherheight(number2);
		int total = 0;
		if(u.getGender().equals("男子")) {
			total = (u.getFatherheight() + u.getMotherheight() + 13) / 2 + 2;
			
			HttpSession session = request.getSession();
			session.setAttribute("childheight",total);
			
			RequestDispatcher rd = request.getRequestDispatcher("form.jsp");
			rd.forward(request, response);
			
		} else {
			total = (u.getFatherheight() + u.getMotherheight() - 13) / 2 + 2;
			HttpSession session = request.getSession();
			session.setAttribute("childheight",total);
			
			RequestDispatcher rd = request.getRequestDispatcher("form.jsp");
			rd.forward(request, response);
			
		}
		
	}

}


0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?