**JSP 내장객체
: jsp 문서내에서 특별히 사용자가 객체를 생성하지 않고도 사용할 수 있는 객체를 말한다.
==> jsp를 실행하면 서버에서 내부적으로 서블릿 클래스가 생성된다.
그 서블릿 클래스의 실제 서비스를 하는 메소드의 지역변수들이 바로 내장객체 !!!
--------------------------------------------------------------------------
** 생성된 서블릿의 실제 서비스를 하는 메소드의 매개변수와 지역변수들은 다음과 같다.
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
** 내장객체 종류
request
responce
session
application
pageContext
config
out
page
** 가장 많이 사용하는 객체 (밑줄 친 것)
request, session, application 은 상태유지를 위해서 필요하다 "상태유지"
** request
: 요청한 사용자의 정보를 싣고 가는 객체
많이 사용하는 메소드 getParamater
파라메터 이름 html문서의 이름과 동일해야한다
getParameter는 문자열을 반환한다.
형태) request.getParameter
여기서 많이 쓰는 메소드(자세한 설명과 예제는 밑에)
getParameter
getRealPath
** 서비스를 요청하는 방식
<form action = "a.jsp" method = "여기">
get : 사용자가 요청한 정보가 주시표시줄에 노출된다.
post : 사용자가 요청한 정보가 주시표시줄에 노출되지 않는다.
--------------------------------------------------------------------------
** 1부터 밑에 입력한 수까지의 합 구하기 프로그램 만들어 보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<h2>request Test</h2>
<hr>
<form action="requestTest01.jsp" method="post">
1부터 밑에 입력한 수까지의 합 구하기
<hr>
수를 입력하세요 : <input type="text" name="userInput"><br>
<input type="submit" value="합 구하기"><br>
</form>
<hr>
<%
if(request.getParameter("userInput") != null){
int user = Integer.parseInt(request.getParameter("userInput"));
int result=0;
for(int i = 1; i <= user; i++){
result = result + i;
}
out.println(user + " 까지의 합은 : "+result );
}
%>
</body>
</html>
------------------------------------------------------------------------------------------------------------
** getParameter 관련
String getParameter(String name)
입력값이 하나일때
<input type = "text">
String[] getParameterValues(String name)
입력값이 여러개 일때
value 값은 달라야 한다.
<input type = "checkbox" name="a" value="홍길동">
<input type = "checkbox" name="a" value="강감찬">
<input type = "checkbox" name="a" value="이순신">
-------------------------------------------------------------------------------------------------------------
** 연습) 사용자한테 취미를 입력받아 출력하는 웹어플리케이션을 작성합니다.
문서명이나 화면구성은 자유롭게 한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<h2>getParameter 연습하기</h2>
<hr>
<form action="requestTest02.jsp" method="post">
이중에서 취미 고르기<br>
축구<input type="checkbox" name="hobby" value="축구"><br>
농구<input type="checkbox" name="hobby" value="농구"><br>
배구<input type="checkbox" name="hobby" value="배구"><br>
야구<input type="checkbox" name="hobby" value="야구"><br>
족구<input type="checkbox" name="hobby" value="족구"><br>
<input type="submit" name="check" value="확인하기">
</form>
<hr>
<%
/*
request.setCharacterEncoding("UTF-8");
if(request.getParameterValues("a") != null){
String []a = request.getParameterValues("a");
for(int i=0; i<a.length ; i++)
{
out.print(a[i]);
}
}
*/
request.setCharacterEncoding("UTF-8");
if(request.getParameterValues("hobby") != null){
String hobby[] = request.getParameterValues("hobby");
String str = "";
for(String s : hobby){
str = str + s + ",";
}
str = str.substring(0, str.length()-1);
out.println("너의 취미는 : " + str + " 입니다." );
}
%>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------
** String getRealPath(String path)
: 웹어플리케이션의 특정 문서나 폴더의 실제경로를 반환한다.
예를 들어) 파일업로드가 가능한 웹어플리케이션을 개발할 경우에는 업로드 할 폴더를 현재 어플리케이션에 만들어 두고 개발한다. 이때 파일처리를 위해서는 실제(물리적인) 경로를 알아야만 한다. 그때 사용한다.
예제)
<%
String path = request.getRealPath("requestTest02.jsp");
%>
<%= path %>
결과)
C:\jspStudy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\day0407\requestTest02.jsp
** String getRemoteAddr()
: 요청한 클라이언트의 ip주소를 반환한다.
예제)
<%
String ip = request.getRemoteAddr();
%>
클라이언트의 ip : <%=ip %>
결과)
http://localhost:8088/day0407/ex04.jsp
localhost 일때 ==> 클라이언트의 ip : 0:0:0:0:0:0:0:1
여기에서 localhost 자리에 다른사람의 아이피를 친다면
자신의 ip주소가 출력이 된다.
다른사람ip 일때 ==> 클라이언트 ip: 203.236.209.142
** setCharacterEncoding()
: 요청한 사용자의 문자셋을 설정
** 상태유지를 위하여 사용하는 메소드
void setAttribute(String name, Object o)
: 값을 설정
Object getAttribute(String name)
: 값을 읽어오기
Attribute 예제 및 형태)
: ex06에 출력값은 다 동일하지만 출력방법이 다르다. 가장 선호하는것 사용. 왠만하면 간단한거사용
ex05
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<%
String name = "홍길동";
int age = 24;
request.setAttribute("name", name);
request.setAttribute("age", age);
// 상태유지가 되면서 ex06.jsp로 보내고자 한다.
RequestDispatcher dispatcher = request.getRequestDispatcher("ex06.jsp");
dispatcher.forward(request, response);
%>
</body>
</html>
ex06
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<%
Object o1 = request.getAttribute("name");
Object o2 = request.getAttribute("age");
%>
<%=o1 %>
<%=o2 %>
<br>
<%=request.getAttribute("name") %>
<%=request.getAttribute("age") %>
<br>
${name}
${age}
</body>
</html>
'JAVA > JSP' 카테고리의 다른 글
JSP 에서의 오류처리 방법 및 예제 (0) | 2016.06.18 |
---|---|
JSP 내장객체 - Cookie, Session (0) | 2016.06.18 |
JSP - 테이블 수정이 안되고 이전것이 자꾸 나올 때 하는 방법 (0) | 2016.06.18 |
JSP 구성요소 중 지시자에 대한 설명 page, include (0) | 2016.06.18 |
JSP - Dao 와 Vo 관련 설명 (0) | 2016.06.18 |