STUDY/JSP

Jsp 6일차 - 전송방식

Anne of Green Galbes 2019. 2. 19. 09:29

1. 데이터 전송방식

① form양식 : getParameter

② session : 여러 jsp에서 공통으로 사용 : session.setAttribute / session.getAttribute

③ hidden : getParameter로 넘어온 데이터를 겉모습을 숨겨서 받음

④ 액션태그


2. life cycle

① application : 서버가 유지되고 있는동안 상태유지(ex. 모든 어플리케이션이 공유할 자원)

★ JSP : Context클래스

★ Servlet : ServletContext클래스

② session : 세션이 설정되서 세션종료때까지 상태유지(ex. 로그인정보, 장바구니)

★ JSP : session 내장객체 사용

★ Servlet : HttpSession 클래스를 이용 세션객체얻기

HttpSession session=request.getSession();                              

③ request : 다음 페이지까지 상태유지 ( 1:1의 관계) (ex. 게시판, 방명록)

request에는 무조건 ip정보가 들어있다.

★ Servlet

RequestDispatcher rd

=request.getRequestDispatcher("상대경로/파일명");

request.setAttribute("세션명",객체명);

rd.forward(request, response);

page  : 현재페이지에서만 상태유지


3. 예제 - 계산기

① calc.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%

request.setCharacterEncoding("UTF-8");

%>

<!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=UTF-8">

<title>Insert title here</title>

</head>


<body>

<form action="calc_ok.jsp" method="post" name="myForm">

<input type="text" name="su1">

<select name="oper">

<option value="+">더하기</option>

<option value="-">빼기</option>

<option value="X">곱하기</option>

<option value="÷">나누기</option>

</select>

<input type="text" name="su2">

<input type="submit" value="=">

</form>

</body>


</html>

submit은 action을 찾아간다

② calc_ok.jsp

<%@page import="com.calc.CalcVO"%>

<%@ page contentType="text/html; charset=UTF-8"%>

<%

request.setCharacterEncoding("UTF-8");

int su1 = Integer.parseInt(request.getParameter("su1"));

int su2 = Integer.parseInt(request.getParameter("su2"));

String oper = request.getParameter("oper");

CalcVO vo = new CalcVO();

vo.setSu1(su1);

vo.setSu2(su2);

vo.setOper(oper);

String str = vo.toString();

%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%=str %>

</body>

</html>

form양식 getParameter로 받는다.

○ su1, su2, oper 에 calc.jsp에서 form양식으로 넘어온 값을 받는다.

○ 받은 값을 vo에 넣는다.

vo에서 계산한 str만 출력 

<%=str %>

③ CalcVO

package com.calc;

public class CalcVO {

private int su1;

private int su2;

private String oper;

public int getSu1() {

return su1;

}

public void setSu1(int su1) {

this.su1 = su1;

}

public int getSu2() {

return su2;

}

public void setSu2(int su2) {

this.su2 = su2;

}

public String getOper() {

return oper;

}

public void setOper(String oper) {

this.oper = oper;

}

public String toString(){

String str = "";

int result=0;

if(oper!=null){

if(oper.equals("+"))

result = su1+su2;

else if(oper.equals("-"))

result = su1-su2;

else if(oper.equals("X"))

result = su1*su2;

else

result = su1/su2;

str = String.format("%d %s %d = %d", su1,oper,su2,result);

}

return str;

}

}

★ VO의 변수 이름은 input의 name에서 지정한 이름과 같아야 한다.

결과

① calc.jsp

② calc_ok.jsp