//Google AdSense

① 리턴 X 입력 X

packagekr.or.ksmart class AA01

package kr.or.ksmart;

public class AA01 {
	public void aaa() {
		System.out.println("01 aaa 실행");
	}
}

aa01_call.jsp

<%@page import="com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="kr.or.ksmart.AA01"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
AA01 a = new AA01();
a.aaa();
%>

aa01_call.jsp 실행

 


<console>

01 aaa 실행

② 리턴 X 입력 O

package kr.or.ksmart class BB01

package kr.or.ksmart;

public class BB01 {
	public void sum(int one, int two) {
		System.out.println("one 변수에 담긴 값 : "+one);
		System.out.println("two 변수에 담긴 값 : "+two);
		int hap = one + two;
		System.out.println(hap + "<- hap");
	}
}

bb01_call.jsp

<%@page import="kr.or.ksmart.BB01"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%
BB01 b = new BB01();
b.sum(5, 3);

%>

bb01_call.jsp 실행


<console>

one 변수에 담긴 값 : 5
two 변수에 담긴 값 : 3
8<- hap

③ 리턴 O 입력 X

package kr.or.ksmart class CC01

package kr.or.ksmart;

public class CC01 {
	public String cc() {
		System.out.println("cc메서드 실행");		
		return "김또깡";
	}
}

cc01_call.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "kr.or.ksmart.CC01" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
CC01 c =new CC01();
String re = c.cc();
out.println(re + "<--re <br/><br/><br/>");
%>

cc01_call.jsp 실행


<console>

cc메서드 실행

<브라우저>

김또깡<--re

④ 리턴 O 입력 O

package kr.or.ksmart class DD01

package kr.or.ksmart;

public class DD01 {
	public int sum(int one, int two) {
		System.out.println("one 변수에 담긴 값 : "+one);
		System.out.println("two 변수에 담긴 값 : "+two);
		int hap = one + two;
		System.out.println(hap + "<- hap");
		return hap;
	}
}

dd01_call.jsp

<%@page import="kr.or.ksmart.DD01"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
DD01 d = new DD01();
int result = d.sum(5, 15);
out.println(result + "<- result <br/><br/><br/>");
%>

dd01_call.jsp 실행

 


<console>

one 변수에 담긴 값 : 5
two 변수에 담긴 값 : 15
20<- hap

<브라우저>

20<- result

 


실습하기

form으로 입력받은 String data type의 두 수를 int data type으로 변환하여
package kr.or.ksmart class DD01 method sum 을 이용해 합산하여 화면에 출력하기

 

 

 

package kr.or.ksmart class DD01 (위의 ④에서 생성함.)
package kr.or.ksmart;

public class DD01 {
	public int sum(int one, int two) {
		System.out.println("one 변수에 담긴 값 : "+one);
		System.out.println("two 변수에 담긴 값 : "+two);
		int hap = one + two;
		System.out.println(hap + "<- hap");
		return hap;
	}
}

 

 

 

input_form.jsp >> 두 수를 입력받을 form 생성
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>


<form action="<%=request.getContextPath()%>/cal/sum_call.jsp" method="post">
1 : <input type = "text" name = "one">	<br/>
2 : <input type = "text" name = "two">	<br/>
	<input type ="submit" value = "전송">
</form>
</body>
</html>

 

 

 

sum_call.jsp >> form으로 입력받은 String data type의 두 수를 int data type으로 변환 ( Integer.parseInt() )
kr.or.ksmart.DD01 import 후 
method sum 호출
<%@page import="kr.or.ksmart.DD01"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
String one = request.getParameter("one");
String two = request.getParameter("two");

int a = Integer.parseInt(one);
int b = Integer.parseInt(two);

DD01 dd = new DD01();
dd.sum(a,b);

out.println("입력한 수 1 : " + a + "<br/>");
out.println("입력한 수 2 : " + b + "<br/><br/>");
out.println("두 수의 합 : " + dd.sum(a,b));
%>

input_form.jsp 실행


 

<브라우저 - 입력화면>

 

 

<console>

one 변수에 담긴 값 : 1234
two 변수에 담긴 값 : 4321
5555<- hap

 

 

<브라우저 - 결과화면>

 

'JSP' 카테고리의 다른 글

[JSP+MySQL] JSP + MySQL / JDBC  (0) 2020.03.19
[JSP] JSP_session응용  (0) 2020.03.13
[JSP] JSP 문법 구조 / include를 활용해 layout 나누기  (0) 2020.03.13
[JSP] 데이터와 화면 연결하기  (1) 2020.03.12

+ Recent posts