– SelectServlet
package com.kook.ch02;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SelectServlet
*/
@WebServlet("/ss")
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.getWriter().append("Served at: ").append(request.getContextPath());
String job = request.getParameter("job"); //select가 multiple속성이 없을때
String interests() = request.getParameterValues("interest"); //select가 multiple속성을 지닌다.
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<html><body>");
out.println("당신이 선택한 직업 : <b>");
out.print(job);
out.println("</b><hr>당신이 선택한 관심 분야 : <b>");
if(interests == null) {
out.print("선택 항목이 없음");
} else {
for (String interest : interests) {
out.print(interest + " ");
}
}
out.println("</b><br><a href="http://itrecipe.m/javascript:history.go(-1)">refresh</a>");
out.print("</body></html");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
-10_select.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>10_select</title>
</head>
<body>
<form method="get" action="ss">
<!
-- 하나만 선택 -->
<span style="float left; margin-right: 20px">
<label for="job" name="job" size="1"></label>
<select id="job" name="job" size="1"> <!
-- size가 1이면 보통 combobox라고 한다.
-->
<option value="">선택해라</option>
<option value="학생">학생</option>
<option value="컴퓨터/인터넷">컴퓨터/인터넷</option>
<option value="언론">언론</option>
<option value="공무원">공무원</option>
<option value="군인">군인</option>
<option value="서비스업">서비스업</option>
<option value="교육">교육</option>
</select>
<br/><br/>
<!
-- 다중선택 -->
<label for="interest">관심분야</label>
<select id="interest" name="interest" size="5" multiple="multiple"> <!
-- multiple : 리스트박스 -->
<option value="에스프레소">에스프레소</option>
<option value="로스팅">로스팅</option>
<option value="컴퓨터/인터넷">컴퓨터/인터넷</option>
<option value="생두">생두</option>
<option value="원두">원두</option>
<option value="핸드드립">핸드드립</option>
</select>
<br/><br/>
<input type="submit" value="전송"/>
</span>
</form>
</body>
</html>