chap03 JSP 기본 구조

3. JSP 페이지에 파라미터 값 전달

3.1.1 GET 요청

GET 요청 파라미터 값 받기

get.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>request.getParmeter</h1>
<%
String sA = request.getParameter("a");
String sB = request.getParameter("b");
%>
A = <%=sA%>, B = <%=sB%><br>
<h2>산술연산</h2>
<%
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
%>
a + b = <%=a+b %><br>
a - b = <%=a-b %><br>
a * b = <%=a*b %><br>
a / b = <%=a/b %><br>
a % b = <%=a%b %><br>

<h2>비교연산</h2>
a == b = <%=a==b %><br>
a >= b = <%=a>=b %><br>
a > b = <%=a>b %><br>
a <= b = <%=a<=b %><br>
a < b = <%=a<b %><br>
a != b = <%=a!=b %><br>

<h2>논리연산</h2>
a == b &&  a <= b = <%=a == b &&  a <= b %><br>
a == b ||  a <= b = <%=a == b ||  a <= b %><br>
!(a == b) = <%=!(a == b) %><br>
</body>
</html>

3.1.2 POST 요청

POST 요청 파라미터 값 받기

input.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
<form action="post.jsp" method="post">
A : <input type="text" name="a"> <br>
B : <input type="text" name="b"> <br>
Submit : <input type="submit" value="Submit">

</form>
</h1>
</body>
</html>

post.jsp

3.2 여러 페이지에 파라미터 값 전달

value01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
String sN = request.getParameter("n");
int n = 0;
if(sN != null){
    n = Integer.parseInt(sN);
}
%>

<h1>최초 값 전달 페이지</h1>
<h1>최초 값 : <%=n %></h1><hr>
<form action="value10.jsp" method="post">
값 <input type="text" name="n">
<input type="submit" value="값전달">
</form>


</body>
</html>

value10.jsp

<form action="value100.jsp" method="post">

value100.jsp

<form action="value1000.jsp" method="post">

value1000.jsp

<form action="value01.jsp" method="post">

3.3 계산기 사이트 개발

cal_input.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
4칙연산 계산기<br>
<form action="cal_result.jsp" method="post">
<input type="text" name="a">
<select name="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="b"> <br>
<input type="submit" value="계산하기">

</form>
</h1>
</body>
</html>

cal_result.php

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String sA = request.getParameter("a");
String sel = request.getParameter("sel");
String sB = request.getParameter("b");

int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int result = 0;
if(sel.equals("+")) result = a + b;
else if(sel.equals("+")) result = a + b;
else if(sel.equals("+")) result = a + b;
else if(sel.equals("+")) result = a + b;
else result = 0;
%>
<h1>계산결과</h1>
<hr>
<h2>
<%=a %> <%=sel %> <%=b %> = <%=result %>
</h2>
<a href="cal_input">다시계산</a>
</body>
</html>

3.4 계산기 사이트 개발

cal.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String sA = request.getParameter("a");
String sel = request.getParameter("sel");
String sB = request.getParameter("b");

int a = 0, b=0, result = 0;
if(sA != null || sB != null || sel != null){
    a = Integer.parseInt(sA);
    b = Integer.parseInt(sB);

    if(sel.equals("+")) result = a + b;
    else if(sel.equals("-")) result = a - b;
    else if(sel.equals("*")) result = a * b;
    else if(sel.equals("/")) result = a / b;
    else result = 0;
}else {
    sel = "+";
}


%>
<h1>
4칙연산 계산기<br>
<form action="cal.jsp" method="post">
<input type="text" name="a">
<select name="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="b"> <br>
<input type="submit" value="계산하기">
<hr>
<h1>계산결과</h1>
<hr>
<h2>
<%=a %> <%=sel %> <%=b %> = <%=result %>
</h2>
</body>
</html>

3.5 VO(DTO) 사용하기

CalVO.java

package chap03;

public class CalVO {

    private int a, b, result;
    String sel;

    public CalVO() {
        this(0,0,0,"+");
    }

    public CalVO(int a, int b, int result, String sel) {
        super();
        this.a = a;
        this.b = b;
        this.result = result;
        this.sel = sel;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getSel() {
        return sel;
    }

    public void setSel(String sel) {
        this.sel = sel;
    }

    @Override
    public String toString() {
        return "CalVO [a=" + a + ", b=" + b + ", result=" + result + ", sel=" + sel + "]";
    }

}

CalService.java

package chap03;

public class CalService {

    CalVO cal(CalVO vo){
        int result = 0;
        if(vo.getSel().equals("+")) {
            result = vo.getA() + vo.getB();
        }else if(vo.getSel().equals("-")) {
            result = vo.getA() - vo.getB();
        }
        else if(vo.getSel().equals("*")) {
            result = vo.getA() *+* vo.getB();
        }
        else if(vo.getSel().equals("/")) {
            result = vo.getA() / vo.getB();
        }
        vo.setResult(result);

        return vo;
    }

}

cal.jsp

<%@page import="chap03.CalService"%>
<%@page import="chap03.CalVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
CalVO vo = null;
CalService cs = null;
String sA = request.getParameter("a");
String sB = request.getParameter("b");
String sel = request.getParameter("sel");
int a = 0, b=0, result = 0;

if(sA != null || sel != null){
    a = Integer.parseInt(sA);
    b = Integer.parseInt(sB);

    vo = new CalVO(a,b,result,sel);
    cs = new CalService();
    vo = cs.cal(vo);
}else {
    vo = new CalVO();
}

%>
<h1>
4칙연산 계산기<br>
<form action="cal.jsp" method="post">
<input type="text" name="a">
<select name="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="b"> <br>
<input type="submit" value="계산하기">
<hr>
<h1>계산결과</h1>
<hr>
<h2>
<%=vo.getA() %> <%=vo.getSel() %> <%=vo.getB() %> = <%=vo.getResult() %>
</h2>
</body>
</html>