선언부(Declaration)


스크립트릿이나 표현식에서 사용할 수 있는 함수를 작성할 때 사용

선언부 형식


<%!
    public 리턴타입 메서드이름(파라미터목록) {
        자바코드1;                                              메소드, 변수, 클래스 선언 가능
        자바코드2;
        ...
        자바코드n;
        return 값;
    }
%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%! 
	private String createGugu(int dan){
		String str ="";
		for(int i=1;i<=9;i++){
			str +=dan + " * " + i + " = " + dan*i + "<br/>"; 
		}
		return str;
	}

%>
<% int dan= Integer.valueOf(request.getParameter("dan")); %>
<!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>Declaration Test</title>
</head>
<body>

	<%=createGugu(dan)%>
</body>
</html>





<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%! 
	private ArrayList<String> createGugu(int dan){
		ArrayList<String> list= new ArrayList<String>();
		for(int i=1;i<=9;i++){
			list.add(dan + " * " + i + " = " + dan*i); 
		}
		return list;
	}

%>
<% int dan= Integer.valueOf(request.getParameter("dan")); %>
<!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>Declaration Test</title>
</head>
<body>

	<%=createGugu(dan)%>
</body>
</html>



↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓



<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%! 
	private ArrayList<String> createGugu(int dan){
		ArrayList<String> list= new ArrayList<String>();
		for(int i=1;i<=9;i++){
			list.add(dan + " * " + i + " = " + dan*i); 
		}
		return list;
	}

%>
<% int dan= Integer.valueOf(request.getParameter("dan")); %>
<!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>Declaration Test</title>
</head>
<body>
<%
	ArrayList<String> list = createGugu(dan);
	for(int i=0;i<list.size();i++){
		String line = list.get(i);%>
		<%=line %><br/>

<%}%>
</body>
</html>





jsp 모델1 방식(JSP, JAVA) 


Gugudan.java

package web.business;

import java.util.ArrayList;

public class Gugudan {
	private int num;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	public ArrayList<String> createGugu(int dan){
		ArrayList<String> list= new ArrayList<String>();
		for(int i=1;i<=9;i++){
			list.add(dan + " * " + i + " = " + dan*i); 
		}
		return list;
	}
}

gugudan.jsp
<%@page import="web.business.Gugudan"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<% 
	int dan= Integer.valueOf(request.getParameter("dan")); 
	Gugudan gugu = new Gugudan();
	ArrayList<String> list = gugu.createGugu(dan);
%>
<!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>Declaration Test</title>
</head>
<body>
<%
	for(int i=0;i<list.size();i++){
		String line = list.get(i);%>
		<%=line %><br/>

<%}%>
</body>
</html>






request 기본 객체


form 처리예

form.jsp

<%@ 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>Servlet POST 방식으로 요청하기</title>
</head>
<body>
<form  action="formProc.jsp" method="post">
	취미를 선택해주세요(다수개 가능)<p/>
	여행<input type="checkbox" name="hobby" value="여행"/>
	등산<input type="checkbox" name="hobby" value="등산"/>
	게임<input type="checkbox" name="hobby" value="게임"/>
	영화<input type="checkbox" name="hobby" value="영화"/>
	놀기<input type="checkbox" name="hobby" value="놀기"/>
	<input type="submit" value="저장"/>
</form>

</body>
</html>


formProc.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	request.setCharacterEncoding("euc-kr");
	String[] hobby = request.getParameterValues("hobby");
%>    
<!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>이용자가 선택한 취미</title>
</head>
<body>
<h2>이용자가 선택한 취미</h2>
<ol>
<%
	for(int i=0;i<hobby.length;i++){%>
		<li><%=hobby[i] %><br/>
<%}
%>
</ol>
</body>
</html>







리다이렉트(Redirect)
특정 페이지로 이동하라고 웹 브라우저에 응답

response.sendRedirect(String location)로 구현















+ Recent posts