본문 바로가기

JSP, Servlet/JSP, Servlet 기초

[JSP/Servlet] 콤보박스, 리스트박스(<select>)

목록 상자
 목록에 있는 여러 항목 중에 하나를 선택하거나 여러 개를 선택하는 콤보박스와 리스트 박스는 <select> 태그로 만든다.

 

<JSP 파일>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>select 예제</title>
</head>
<body>
	<form method="get" action="SelectServlet">
		<label for="job">직업</label> 
		<select id="job" name="job" size="1">
			<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" style="float:left">관심분야</label> 
		<select id="interest" name="interest" size="5" multiple="multiple">
			<option value="에스프레소">에스프레소</option>
			<option value="로스팅">로스팅</option>
			<option value="생두">생두</option>
			<option value="원두">원두</option>
			<option value="핸드드립">핸드드립</option>
		</select>
		<br><br>
		<input type="submit" value="확인" style="margin-left:50px">
	</form>
</body>
</html>

 

<Servlet 파일>

package unit07;

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("/SelectServlet")
public class SelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String job = request.getParameter("job"); // select : Combobox
		String interests[] = request.getParameterValues("interest"); // select: List
		
		PrintWriter out = response.getWriter();
		out.println("<html><head></head><body>");
		out.println("당신이 선택한 직업 : <b>");
		out.println(job+"</b><br><hr>");
		out.println("당신이 선택한 관심 분야 : <br>");
		
		if(interests == null) {
			out.println("선택한 항목이 없습니다.");
		} else {
			for(String item: interests) {
				out.println("[" + item + "] ");
			}
		}
		out.println("<br><a href'javascript.history.go(-1)'>다시</a>");
		out.println("</body></html>");
		out.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

728x90
반응형