JSP03: Start of JSP


<Scriptlet>

Scriptlet is code block to execute Java code on JSP page.

Scriptlet has following structure:

1
2
3
4
5
6
7
8
9
<%
 
    JAVA CODE1;
 
    JAVA CODE2;
 
    JAVA CODE3;
 
%>
cs


Example of Scriptlet

<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>1-10까지의 합</title></head>
<body>
<%
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum = sum + i;
}
%>
1부터 10까지의 합은 <%= sum%>    //1부터 10까지의 합은 55입니다
</body>
</html>


<Expression>

Expression is used to include value for output result.

Expression has following structure:

1
<%= value%>
cs


Example of Expression

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head><title>1-10까지의 합: 표현식만 사용</title></head>
<body>
1부터 10까지의 합은
<%= 1 + 2+ 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10%>    //55
입니다.
</body>
</html>


<Declaration>

Declaration is used to declare method for scriptlet or expression.

Declaration has following structure:

1
2
3
4
5
6
7
8
<%!
    public return_type method_name(parameter) {
        JAVA CODE 1
        JAVA CODE 2
        ...
        return value;
    }
%>
cs


Example of Declaration

<%@ page contentType = "text/html; charset=utf-8" %>
<%!
public int multiply(int a, int b) {
int c = a * b;
return c;
}
%>
<html>
<head><title>선언부를 사용한 두 정수값의 곱</title></head>
<body>
10 * 25 = <%= multiply(10, 25) %>
</body>
</html>


<Request object>

If you enter address of web site on web browser, web browser send request information to web server.

The request information is included into request object.


-Function of Request object:

1. Get information of web browser

2. Get information of web server

3. Get information of request parameter

4. Get information of request header

5. Get information of cookie

6. Get information of attribute 


Example of Request object

<%@ page contentType = "text/html; charset = UTF-8" pageEncoding = "utf-8" %>
<html>
<head><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>
컨텍스트 경로 = <%= request.getContextPath() %> <br>
서버이름 = <%= request.getServerName() %> <br>
서버포트 = <%= request.getServerPort() %>
</body>
</html>


<Get and Post method type>

Web browser can send parameter using Get or Post method type.

The difference between two method is that Get method send parameter using query string.

The example of Get method is :

http://localhost:8080/chap03/viewParameter.jsp?name=cbk&address=seoul&pet=cat


<Processing request header information>

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.Enumeration" %>

<html>
<head><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>


<Response Object>

Response object include information to send web browser.


<Redirect>

One of the most used function on response object is redirect.

You can go to other page using redirect:

1
response.sendRedirect(String location)
cs


Example of redirect

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String id = request.getParameter("memberId");
if (id != null && id.equals("madvirus")) {
response.sendRedirect("/chap03/index.jsp");
} else {
%>
<html>
<head><title>로그인에 실패</title></head>
<body>
잘못된 아이디입니다.
</body>
</html>

<%
}
%>


<JSP Comment>

<%-- Write JSP Comment here --%>