백문이불여일타

[Jsp/Servlet]스크립트요소_내장객체기능(out 내장객체 실습) 본문

Jsp

[Jsp/Servlet]스크립트요소_내장객체기능(out 내장객체 실습)

퇴근각 2019. 7. 11. 13:01

out 내장 객체 이용해 데이터 출력하기 예제


 

1. 실습파일 out1.jsp, out2.jsp 준비한다

2.이름과 나이를 out2.jsp 로 포워딩(=위임)한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>      
<html>
<head>
  <title>데이터입력창</title>
<meta charset="UTF-8">
</head>
 
<body>
   <form method="post" action="out2.jsp">
     이름: <input type="text" name="name"><br>
     나이: <input type="text" name="age"><br>
       <input type ="submit" value="전송">
   </form>
   
</body>
</html>
 
cs

3.전송된 이름과 나이를 표현식<%=%> 과 out내장객체를 이용해 출력한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
   request.setCharacterEncoding( "utf-8" );
   String name=request.getParameter("name");
   String age=request.getParameter("age");
%> 
 
<!DOCTYPE html>          
<html>
<head>
<meta charset="UTF-8">
  <title>데이터출력창</title>
</head>
 
<body>
<%
 if(name!=null ||name.length()!=0){
%>
     <h1><%=name %> ,<%=age %>  </h1>
<%
}else{
%>
    <h1>이름을 입력하세요</h1>
<%
}
%>
 
<%
 if(name!=null ||name.length()!=0){
%>
    <h1><% out.println(name+" , "+age); %></h1>
<%
}else{
%>
    <h1>이름을 입력하세요</h1>
<%
}
%>
 
</body>
</html>
 
cs

4. 톰켓실행하여 브라우저에서 요청하여 이름과 나이를 입력한후 전송한다 

5. 브라우저에서 전달받은정보를 표현식과 out내장객체로 출력한다

 

Comments