servletRequest+forward+リクエストスコープ+calcEX

中途半端初心者の忘備録

/CalcControllerからWEB-INF/jsp/firstView.jspへフォーワード

```CalcController.java
@WebServlet("/CalcController")
public class CalcController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dsp = request.getRequestDispatcher("WEB-INF/jsp/firstView.jsp");
		dsp.forward(request, response);
	}
```


WEB-INF/jsp/firstView.jsより/firstNum/secondNum/markをString mark = request.getParameterで受け取る


```calcController.java

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String firstNum = request.getParameter("firstNum");
		String secondNum = request.getParameter("secondNum");
		String mark = request.getParameter("mark");
```
 calcBeansインスタンス化しsetterにセット、first,secondはDobule型変換
request.setAttribute("cb", cb);でリクエストスコープに保存


```calcController.java

		calcBeans cb = new calcBeans();
		cb.setFirstNum(Double.parseDouble(firstNum));
		cb.setSecondNum(Double.parseDouble(secondNum));
		cb.setMark(mark);

		request.setAttribute("cb", cb);
```

secondViewへフォーワード

```calcController.java
		RequestDispatcher dsp = request.getRequestDispatcher("/WEB-INF/jsp/secondView.jsp");
		dsp.forward(request, response);
```

インポート
リクエストスコープ受け取りgetAttribute("cb")

```secondView.jsp
   <%@ page import="model.calcBeans" %>
   <%
   calcBeans cb = (calcBeans)request.getAttribute("cb");
   %>

```

```firstView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>試し</title>
</head>
<body>
    <h1>試し</h1>
    <form action="/calcEXEXEX/CalcController" method="post">
        <input type="text" name="firstNum">
        <select name="mark">
            <option value="0">+</option>
            <option value="1">-</option>
            <option value="2">/</option>
            <option value="3">*</option>
        </select>
        <input type="text" name="secondNum">
        <input type="submit" value="=">
    </form>
</body>
```


```calcBeans.java
package model;

import java.io.Serializable;

public class calcBeans implements Serializable{
	private double firstNum;
	private double secondNum;
	private String mark;

	public calcBeans() {
	}
	public calcBeans(double firstNum, double secondNum, String mark) {
		this.firstNum = firstNum;
		this.secondNum = secondNum;
		this.mark = mark;
	}
	public double getFirstNum() {
		return firstNum;
	}
	public void setFirstNum(double firstNum) {
		this.firstNum = firstNum;
	}
	public double getSecondNum() {
		return secondNum;
	}
	public void setSecondNum(double secondNum) {
		this.secondNum = secondNum;
	}
	public String getMark() {
		return mark;
	}
	public void setMark(String mark) {
		this.mark = mark;
	}
	public double getTasu() {
		return this.firstNum+this.getSecondNum();
	}

}

```

```secondView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ page import="model.calcBeans" %>
   <%
   calcBeans cb = (calcBeans)request.getAttribute("cb");
   %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>second</h1>
<%= cb.getTasu() %>

</body>
</html>
```

簡単なTasuだけのサーブレット&jsp
コンストラクタで受け取った値をnewする時に直接Beansに受け取らせることもできる