Java/JSP&Servlet

[Servlet] 회원가입

퓨어맨 2022. 5. 13. 17:49
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="ex10join" method="post">
        <!-- 표 -->
        <table width="400px">
            <!-- Alt + Shift + 아래방향키 -->
            <!-- STEP1 -->
            <tr bgcolor="gray" height="50px">
                <th colspan="2" align="left">STEP1 : 아이디/비번 입력</th>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right">아이디 :</td>
                <td>
                    <input type="text" name="id">
                </td>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right">비밀번호 :</td>
                <td>
                    <input type="password" name="pw">
                </td>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right">비밀번호 확인 :</td>
                <td>
                    <input type="password" name="pwC">
                </td>
            </tr>

            <!-- STEP2 -->
            <tr bgcolor="gray" height="50px">
                <th colspan="2" align="left">STEP2 : 개인정보</th>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right"> 성별 :</td>
                <td>
                    남성 <input type="radio" name="gender" value="man">
                    여성 <input type="radio" name="gender" value="woman"> 
                </td>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right"> 혈액형 :</td>
                <td>
                    <select name="abo">
                        <option value="A">A형</option>
                        <option value="B">B형</option>
                        <option value="O">O형</option>
                        <option value="AB">AB형</option>
                    </select>
                </td>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right"> 생일 :</td>
                <td>
                    <input type="date" name="date">
                </td>
            </tr>

            <!-- STEP3 -->
            <tr bgcolor="gray" height="50px">
                <th colspan="2" align="left">STEP3 : 선호도</th>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right">취미 :</td>
                <td>
                    축구<input type="checkbox" name="hobby" value="soc">
                    야구<input type="checkbox" name="hobby" value="bs">
                    농구<input type="checkbox" name="hobby" value="bb">
                </td>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right">좋아하는 색깔 :</td>
                <td>
                    <input type="color" name="color">
                </td>
            </tr>

            <!-- STEP4 -->
            <tr bgcolor="gray" height="50px">
                <th colspan="2" align="left"> STEP4 : 적고 싶은 말</th>
            </tr>
            <tr bgcolor="whitesmoke" height="35px">
                <td align="right" colspan="2">
                    <textarea name="talk" cols="54" rows="5" ></textarea>
                </td>
            </tr>

            <tr bgcolor="whitesmoke" height="35px">
                <td colspan="2" align="center">
                    <input type="submit">
                    <input type="reset">
                </td>
            </tr>

        </table>

    </form>

</body>
</html>

 

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

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("/ex10join")
public class ex10join extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 0. 인코딩
		// Get방식의 경우
		// Servers 프로젝트 > server.xml 파일에서 수정해줘야한다.
		// Connector 태그의 URIEncoding 속성을 주면된다.
		
		// Post방식의 경우
		// request 객체에 인코딩 방식 지정
		// request.setCharacterEncoding("인코딩 방법");
		// 파라미터 수집 이전에 인코딩 진행할 것!
		request.setCharacterEncoding("UTF-8");
		
		// 1. 파라미터 수집
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		String pwC = request.getParameter("pwC");
		String gender = request.getParameter("gender");
		String abo = request.getParameter("abo");
		String birth = request.getParameter("date");
		
		// String hobby = request.getParameter("hobby");
		// 같은 name의 input태그값 전부 가져오기
		// request.getParameterValues("Name");
		// 결과값이 String 배열로 리턴된다.
		String[] hobbys = request.getParameterValues("hobby");
		// 배열 > String
		// ["soc", "bs", "bk"]
		String hobby = Arrays.toString(hobbys);
		
		
		String color = request.getParameter("color");
		String talk = request.getParameter("talk");
		
		// 2. 응답
		// 응답 형식 지정
		response.setContentType("text/html; charset=utf-8");
		
		// PrintWriter 생성
		PrintWriter out = response.getWriter();
		
		// 응답 내용 작성
		out.print("<html>");
		out.print("<head> </head>");
		out.print("<body>");
		
		out.print("ID:"+id+"<br>");
		out.print("pw:"+pw+"<br>");
		out.print("pwC:"+pwC+"<br>");
		out.print("성별:"+gender+"<br>");
		out.print("혈액형:"+abo+"<br>");
		out.print("생일:"+birth+"<br>");
		out.print("취미:"+hobby+"<br>");
		out.print("좋아하는색깔:"+color+"<br>");
		out.print("하고싶은말:"+talk+"<br>");
		
		out.print("</body>");
		out.print("</html>");
		
		
		
	}

}

 

html 데이터 전송

 

 

결과화면