본문 바로가기

JSP, Servlet/JSP, Servlet 기초

[JSP/Servlet] 라디오버튼 실습

여러개의 라디오버튼을 이용하여 데이터를 전송해보자

(GET방식을 이용한다.)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="get" action="RadioServletEx">
		
		<br>
		<h5>성별 :</h5>
		<input type="radio" id="chk_gender" name="chk_gender" value="male" checked="checked">남자
		 <input type="radio" id="chk_gender" name="chk_gender" value="female">여자
		 <br>
		<h5>이메일 수신 여부 :</h5>
		<input type="radio" id="check_mail" name="check_mail" value="yes" checked="checked">수신
		 <input type="radio" id="check_mail" name="check_mail" value="no">거부 
		 <br> 
		 <h5>가입 인사</h5>
		<textarea rows="5" cols="80" name="content"></textarea>
		 <input type="submit" value="ok">
	</form>
</body>
</html>

 

 

package RadioServletEx;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 입력 parameter 수신
				String content = request.getParameter("content");
				String check_mail = request.getParameter("check_mail");
				String chk_gender = request.getParameter("chk_gender");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.println("<html><head><meta charset=\"utf-8\" /></head><body>");
				out.println("당신이 입력한 정보입니다.");
				out.println("<br>성별 : " + chk_gender);
				out.println("<br>메일 수신 여부 : " + check_mail);
				out.println("<br>가입 인사 : " + content);
				out.print("<br><a href='javascirpt.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
반응형