출처:http://javai.tistory.com/463

'JSP > 기본(Oracle)' 카테고리의 다른 글

JSP 기본 객체와 영역  (0) 2012.05.30
JSP 기본 객체 사용하여 자원 읽기  (0) 2012.05.30
JSP 서버정보 출력, 로그 메세지 기록  (0) 2012.05.30
배포 구조  (0) 2012.05.30
autoFlush 설정(true,false)  (0) 2012.05.30
<%@ 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>서버 정보 출력</title>
</head>
<body>
서버 정보:<%=application.getServerInfo() %><br>
서블릿 규약 메이저 버젼:<%=application.getMajorVersion() %><br>
서블릿 규약 마이너 버젼:<%=application.getMinorVersion() %><br>

<%
	application.log("로그 메세지 기록");
%>
로그 메세지를 기록합니다.
</body>
</html>



'JSP > 기본(Oracle)' 카테고리의 다른 글

JSP 기본 객체 사용하여 자원 읽기  (0) 2012.05.30
Servlet Context  (0) 2012.05.30
배포 구조  (0) 2012.05.30
autoFlush 설정(true,false)  (0) 2012.05.30
JSP 리다이렉트(Redirect)를 이용해서 페이지 이동하기2  (0) 2012.05.30










출처: http://javai.tistory.com/461

autoFlushFalse.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page buffer="1kb" autoFlush="false"%>
<!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>autoFlush 속성값 false 예제</title>
</head>
<body>

	<%
		for (int i = 0; i < 1000; i++) {
	%>
	1234
	<%
		}
	%>
</body>
</html>





autoFlushTrue.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page buffer="1kb" autoFlush="true"%>
<!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>autoFlush 속성값 true 예제</title>
</head>
<body>
	<%
		for (int i = 0; i < 1000; i++) {
	%>
	1234
	<%
		}
	%>
</body>
</html>



login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String id = request.getParameter("memberId");
	if (id != null && id.equals("era13")) {
		response.sendRedirect("/01.HelloJsp/index.jsp");
	} else {
%>
<!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>로그인 실패</title>
</head>
<body>
아이디가 era13이 아닙니다.
</body>
</html>
<%
	}
%>


index.jsp
<%@ 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>login</title>
</head>
<body>
아이디가 era13 입니다.
</body>
</html>





redirectEncodingTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import ="java.net.URLEncoder" %>

<%
	String value = "I LIKE JAVA ";
	String encodedValue = URLEncoder.encode(value, "UTF-8");
	response.sendRedirect("/01.HelloJsp/main.jsp?name=" + encodedValue);
%>


main.jsp
<%@ 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>main.jsp</title>
</head>
<body>
	<%
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
	%>

	<%=name%>
</body>
</html>

redirectEncodingTest.jsp실행 화면


responseA.jsp
<%@ 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>responseA</title>
</head>
<body>
현재 페이지는 responseA입니다. 화면에 보여지지 않습니다.
</body>
</html>
<%
	response.sendRedirect("/01.HelloJsp/responseB.jsp");
%>


responseB.jsp
<%@ 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>responseB</title>
</head>
<body>
responseB.jsp 입니다.
</body>
</html>


responseA.jsp 실행화면


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.Enumeration"%>
<!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>헤더 목록 출력</title>
</head>
<body>
	<%
		Enumeration headerEnum = request.getHeaderNames();
		while (headerEnum.hasMoreElements()) {
			String headerName = (String) headerEnum.nextElement();
			String headerValue = request.getHeader(headerName);
	%>
	<%=headerName%>
	<%=headerValue%><br />
	<%
		}
	%>
</body>
</html>






한번 새로고침 하면 쿠키정보가 나옴 


Enumeration 복수의 데이터를 갖는 객체


 getParameter(String name) String 이름이 name인 피라미터의 값을 구한다. 존재하지 않을 경우 null을 리턴한다.
 getParameterValues(String name)  String[] 이름이 name인 모든 파라미터의 값을 배열로 구한다. 존재 하지 않을 경우 null을 리텉한다.
 getParameterNames() java.util.Enumeration 웹 브라우저가 전송한 피라미터의 이름을 구한다.
 getParameterMap() java.util.Map 웹 브라우저가 전송한 피라미터의 맵을 구한다.맵은<피라미터 이름, 값>쌍으로 구성된다






출처:http://javai.tistory.com/455

Request 기본 객체의 파라미터 읽기 메서드


<input type="text">  ==> 입력 값이 없이 전송 되면  name=""  빈문자열로 인식

<input type="checkbox">     ==> 선택하지 않으면 null값임 parametername 전송 못함  
<input type="radio">
<select>


get 방식으로 바꿔서 테스트

데이터를 입력해서 전송할 경우
http://localhost:8080/chap03/viewParameter.jsp?
name=%ED%99%8D%EA%B8%B8%EB%8F%99&
address=%EC%84%9C%EC%9A%B8%EC%8B%9C&
pet=dog&
pet=pig

데이터 입력없이 전송할경우 text 인경우에만 빈문자열로 전달
http://localhost:8080/chap03/viewParameter.jsp?
name=&    
address=




makeTestForm.jsp
<%@ 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>폼 생성</title>
</head>
<body>
폼에 데이터를 입력한 후[전송] 버튼을 클릭하세요
<form action="/chap03/viewParameter.jsp" method="post">
	이름 : <input type="text" name="name" size="10"><br/>
	주소 : <input type="text" name="address" size="30"><br/>
	좋아하는 동물
			<input type="checkbox" name="pet" value="dog">강아지
			<input type="checkbox" name="pet" value="cat">고양이
			<input type="checkbox" name="pet" value="pig">돼지
			<br/>
			<input type="submit" value="전송">
</form>
</body>
</html>



viewParameter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.Enumeration" %>
    <%@ page import="java.util.Map" %>
    <% 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>요청 피라미터 출력</title>
</head>
<body>
<b>request.getParameeter() 메서도 사용</b><br/>
name 피라미터 = <%= request.getParameter("name") %> <br/>
address 피라미터 = <%= request.getParameter("address") %> 
<p>
<b>request.getParameterValues() 메서드 사용</b><br/>

<%
	String[] values=request.getParameterValues("pet");
if(values != null){
	for (int i=0;i<values.length; i++){
%>
<%= values[i] %>
<%
	}
}
%>
<p>
<b>request.getParameterNames() 메서드 사용</b><br/>
<%
	Enumeration paramEnum = request.getParameterNames();
while(paramEnum.hasMoreElements()){
	String name= (String)paramEnum.nextElement();
%>
<%= name %>
<%} %>
<p>
<b>request.getParameterMap()메서드 사용</b><br/>
<%
	Map parameterMap = request.getParameterMap();
String[] nameParam = (String[])parameterMap.get("name");
if(nameParam != null){
%>
name = <%= nameParam[0] %>
<%}%>
</body>
</html>





'JSP > 기본(Oracle)' 카테고리의 다른 글

request 기본 객체의 파라미터 읽기 메서드  (0) 2012.05.30
parameter  (0) 2012.05.30
JSP 클라이언트 및 서버 정보  (0) 2012.05.30
JSP Calendar 클래스 사용, URL/URI의 구분  (0) 2012.05.30
JSP 배열의 내용 출력  (0) 2012.05.30
<%@ 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>클라이언트 및 서버 정보</title>
</head>
<body>
	클라이언트 IP = <%=request.getRemoteAddr() %> <br/>
	요청 정보 길이 = <%= request.getContentLength() %> <br/>
	요청정보 인코딩 = <%= request.getCharacterEncoding() %> <br/>
	요청정보 컨텐트 타입= <%=request.getContentType() %> <br/>
	요청정보 프로토콜 = <%= request.getProtocol() %> <br/>
	요청정보 전송방식 = <%= request.getMethod() %> <br/>
	요청 URI = <%= request.getRequestURI() %><br/>
	요청 URL = <%= request.getRequestURL() %> <br/>
	컨텍스트 경로 = <%= request.getContextPath() %> <br/>
	서버 이름 = <%= request.getServerName() %> <br/>
	서버 포트 =  <%= request.getServerPort() %> <br/>

</body>
</html>



※상대 편 컴퓨터로 들어가면 내가 클라이언트가 됨

page드렉티브

contentType : JSP가 생성할 문서의 타입을 지정한다. "text/html or "text/xml or "text/plain"
errorPage: JSP페이지를 실행하는 도중에 에러가 발생할 때 보여줄 페이지를 지정한다.

useImportCalendar.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import = "java.util.Calendar" %>
<!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>Calendar 클래스 사용</title>
</head>
<body>
	<%
		Calendar cal = Calendar.getInstance();

	%>
	
	오늘은
		<%= cal.get(Calendar.YEAR) %>년
		<%= cal.get(Calendar.MONTH)+1 %>월
		<%= cal.get(Calendar.DATE) %>일
	입니다.
</body>
</html>





URL/URI의 구분




<%@ 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>스크립트 연습</title>
</head>
<body>
	<h2>표현식 예제1 - 배열의 내용 출력</h2>

	<%!
		//선언부 : 변수 선언, 메소드 선언
		String str[] = { "JSP가", "정말", "재미", "있다" };
	%>
	<table border="1" width="250">
		<tr>
			<td>배열의 첨자</td>
			<td>배열의 내용</td>
		</tr>

		<%
			//스크립트 릿  : 변수선언 , 연산 , 제어문 , 출력
			for(int i=0;i<str.length;i++){
				out.println("<tr><td>");
				out.println(i);
				out.println("</td><td>");
				out.println(str[i]);
				out.println("</td>");
				out.println("</tr>");
			}
		%>
		</table>
		<br/>
		스크립트릿과 표현식 사용
		<table border="1" width="250">
		<tr>
			<td>배열의 첨자</td>
			<td>배열의 내용</td>
		</tr>
		
		<%
			for (int i=0;i<str.length;i++){	
		%>
		<tr>
		<!-- 표현식 : 변수의 값 출력, 메소드의 결과값 출력, 연산 -->
		<%-- JSP주석 소스보기할때 보여지지 않음 --%>
		<td><%=i%></td>
		<td><%= str[i]%></td>
	<%} %>
	</table>
	<br/>
	확장 for문 사용
	<table border="1" width="250">
	<tr>
	<td>배열의 내용</td>
	</tr>
	<%
		for (String s : str){
	%>
	<tr>
	<td><%=s %></td>
	</tr>
	<%} %>
	</table>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%! 
		//선언부 : 변수 선언, 메소드 선언
		public int multiply(int a, int b) {
		int c = a * b;
		return c;
	}%>
<!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>선언부를 사용한 두 정수 값의 곱</title>
</head>
<body>
	10 * 25 =
	<%=multiply(10, 25)%>
</body>
</html>


가능은 하지만 거의 사용하지 않는 방법


'JSP > 기본(Oracle)' 카테고리의 다른 글

JSP Calendar 클래스 사용, URL/URI의 구분  (0) 2012.05.30
JSP 배열의 내용 출력  (0) 2012.05.30
Servlet 생명주기 ,JSP 구성요소  (0) 2012.05.30
POST 방식과 GET 방식  (0) 2012.05.30
Form & Servlet  (0) 2012.05.30

※멤버변수 사용시 :상수같이 변동없는값은 가능하지만
                        차감이나 증감같이 변동시키도록 코드를짜면 동기화에 문제가 생길수있다.


package com.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LifeCycle extends HttpServlet{
    
    int initCnt=0;
    int serviceCnt=0;
    int destroyCnt=0;
    
    public void init() throws ServletException{
        System.out.println("init 메소드는 첫 요청만 호출됨: " + (++initCnt));
    }
    
    public void service(HttpServletRequest request, HttpServletResponse response) 
    		throws ServletException,IOException{

        System.out.println("service 메소드는  요청때마다 호출됨: " + (++serviceCnt));
    
        response.setContentType("text/html;charset=euc-kr");
        PrintWriter out = response.getWriter();
        out.println("<html><head><title>Servlet Life Cycle</title></head>");
        out.println("<body>");
        out.println("서블릿 인스턴스의 ID:"+initCnt+"<br/>");
        out.println("서블릿 service 호출 ID:"+serviceCnt+"<br/>");
        Thread th=Thread.currentThread();
        out.println("현재 Thread 이름: "+th.getName()+"<br/>");
        out.println("</body></html>");    
    }
    
    public void destory(){
        System.out.println("destroy 메소드는 본 Servlet이 더이상 사용되지 않을 때만 호출됨 : " 
        		+ (++destroyCnt));
    }    
}



스크립트 요소

1.스크립트릿 <%   %> : 지역변수 선언

2.표현식      <%=  %> : 출력 , 변수 값을 출력, 메소드 결과값, 연산의 결과 출력

3.선언부      <%!  %> : 멤버 변수 선언, 메소드 선언



<%@ 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>1-10까지의 합</title>
</head>
<body>
	<%
	//스크립트릿 : 지역변수 선언, 연산, 제어, 출력
		int sum = 0;
		for (int i = 1; i <= 10; i++) {
			sum = sum + i;
		}
	%>
<%-- 표현식 : 변수의 값, 메소드의 결과값, 연산의 값 출력 --%>
<!-- HTML 주석 -->
1부터 10까지의 합은 <%= sum %> 입니다.
</body>
</html>




'JSP > 기본(Oracle)' 카테고리의 다른 글

JSP 배열의 내용 출력  (0) 2012.05.30
JSP 메소드 선언 및 호출  (0) 2012.05.30
POST 방식과 GET 방식  (0) 2012.05.30
Form & Servlet  (0) 2012.05.30
Servlet  (0) 2012.05.30

+ Recent posts