https://pearl23.tistory.com/27
이거 이어서 응답페이지 작성하기
구현 화면은 전에꺼 그대로
재밌는 테스트~
데이터를 입력 후 확인버튼을 입력하세요!
response.setContentType("text/html;charset=utf-8");
클라이언트와 연결되는 문자열 stream을 가져오는 메소드
getWriter();
PrintWriter out=response.getWriter();
이 후 html코드 작성해주기
String html="<html>";
html+="<body>";
html+="<h1>응답페이지 <br> 테스트 결과</h1>";
html+="<h2>이름 : "+name+", </h2>";
html+="<h2>나이는 "+age+", 키는 "+height+"</h2>";
html+="<p>좋아하는 색은 <span style=\'color:"+color+"\'>"+color+"</span>, </p>";
html+="<ul>좋아하는 음식";
for(String f : foods) {
String src="";
switch(f) {
case "떡볶이" : src="https://cdn.kihoilbo.co.kr/news/photo/202008/880134_302005_3430.png";break;
case "마라탕" : src="http://www.straightnews.co.kr/news/photo/201912/61797_33389_525.jpg";break;
case "치킨" : src="https://pelicana.co.kr/resources/images/menu/best_menu02_200824.jpg";break;
case "초밥" : src="https://www.google.com/url?sa=i&url=https%3A%2F%2Fgurunavi.com%2Fko%2Fjapanfoodie%2F2010%2F03%2Fsushi.html&psig=AOvVaw3L6Sch7Yi2OxhyER9B7Yu0&ust=1619952061369000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCMjS7vKlqPACFQAAAAAdAAAAABAD";break;
case "피자" : src="https://www.google.com/url?sa=i&url=https%3A%2F%2Fweb.dominos.co.kr%2Fgoods%2Fdetail%3Fdsp_ctgr%3DC0104%26code_01%3DRPZ007SL&psig=AOvVaw3Hqfa6WX59Rj5ujEMel9aw&ust=1619952100006000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPDMkP6lqPACFQAAAAAdAAAAABAD";break;
default : src="https://lh3.googleusercontent.com/proxy/OzuosxyoQLdOJZ5izxixiOOw5ZtyTn__hbcJmCapqoZ1t_86Bmt-_JbP9lq6gSM1widGBLAak6D-mz6Jl4VwT1NsjjBuy21yc1FPdBPBzV9KPkkui3vNRZARNmgfpF4nLA";break;
}
html+="<li><img src='"+src+"' width='100px' height='100px'>"+f+"</li>";
}
html+="</ul>";
html+="<p>좋아하는 동물은 "+animal+"</p>";
html+="</body>";
html+="</html>";
out.write(html);
색상에 스타일도 집어넣어주고 음식에 사진도 넣어주기
데이터 선택 후 확인버튼을 누르면
응답페이지를 확인할 수 있다.
servlet 최종 코드
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package com.data.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/testservlet.do")
public class NewFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public NewFileServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
double height=Double.parseDouble(request.getParameter("weight"));
String color=request.getParameter("color");
String[] foods = request.getParameterValues("foods");
String animal = request.getParameter("animal");
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("키 : " + height);
System.out.println("색상 : " + color);
System.out.print("음식 : ");
for(String f : foods) {
System.out.print(f+" ");
}
System.out.println();
System.out.println("동물 : " + animal);
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String html="<html>";
html+="<body>";
html+="<h1>응답페이지 <br> 테스트 결과</h1>";
html+="<h2>이름 : "+name+", </h2>";
html+="<h2>나이는 "+age+", 키는 "+height+"</h2>";
html+="<p>좋아하는 색은 <span style=\'color:"+color+"\'>"+color+"</span>, </p>";
html+="<ul>좋아하는 음식";
for(String f : foods) {
String src="";
switch(f) {
case "떡볶이" : src="https://cdn.kihoilbo.co.kr/news/photo/202008/880134_302005_3430.png";break;
case "마라탕" : src="http://www.straightnews.co.kr/news/photo/201912/61797_33389_525.jpg";break;
case "치킨" : src="https://pelicana.co.kr/resources/images/menu/best_menu02_200824.jpg";break;
case "초밥" : src="https://www.google.com/url?sa=i&url=https%3A%2F%2Fgurunavi.com%2Fko%2Fjapanfoodie%2F2010%2F03%2Fsushi.html&psig=AOvVaw3L6Sch7Yi2OxhyER9B7Yu0&ust=1619952061369000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCMjS7vKlqPACFQAAAAAdAAAAABAD";break;
case "피자" : src="https://www.google.com/url?sa=i&url=https%3A%2F%2Fweb.dominos.co.kr%2Fgoods%2Fdetail%3Fdsp_ctgr%3DC0104%26code_01%3DRPZ007SL&psig=AOvVaw3Hqfa6WX59Rj5ujEMel9aw&ust=1619952100006000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPDMkP6lqPACFQAAAAAdAAAAABAD";break;
default : src="https://lh3.googleusercontent.com/proxy/OzuosxyoQLdOJZ5izxixiOOw5ZtyTn__hbcJmCapqoZ1t_86Bmt-_JbP9lq6gSM1widGBLAak6D-mz6Jl4VwT1NsjjBuy21yc1FPdBPBzV9KPkkui3vNRZARNmgfpF4nLA";break;
}
html+="<li><img src='"+src+"' width='100px' height='100px'>"+f+"</li>";
}
html+="</ul>";
html+="<p>좋아하는 동물은 "+animal+"</p>";
html+="</body>";
html+="</html>";
out.write(html);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
|
cs |
'Programming | Study > JSP | Servlet' 카테고리의 다른 글
[servlet] 외부 servlet 호출 (RequestDispatcher) (0) | 2021.05.01 |
---|---|
[servlet] 응답기능 로직 구현 (0) | 2021.05.01 |
[servlet] 클라이언트가 보낸 데이터 확인하기 (0) | 2021.05.01 |
[servlet] 데이터 처리 - get방식과 post방식 (0) | 2021.05.01 |
[servlet] RequestListener 구현 (0) | 2021.04.24 |
댓글