본문 바로가기

DATA 분석 교육 과정 (2024.02~08)/JSP&Servlet

Servlet_(실습) POST방식 (user-info)

728x90

<HTML>

<body>
 <form action="Ex09_userInfo" method="post">
        <table  width='400px'>
            <tr bgcolor = 'gray'  align = 'left'>
                <th colspan='2' height ='50px'>Step1. 아이디/비번입력</th>         
            </tr>
            <tr bgcolor = 'whitesmoke' > 
                <td align = 'right' height ='35px' width ='150px'>아이디</td>
                <td>
                    <!--여기서 말고 전체적 밖으로 한번에 빼주기  <form action=#> -->
                        <input type="text" name = "id">
                    <!-- </form> -->
                </td>
            </tr>
    
            <tr bgcolor = 'whitesmoke'>
                <td align = 'right' height ='35px'>비밀번호</td>
                <td>
                    <!-- <form action=#> -->
                        <input type="text" name = "pw">
                    <!-- </form> -->
                </td>
            </tr>
    
            <tr bgcolor = 'whitesmoke'>
                <td align = 'right' height ='35px'>비밀번호 확인</td>
                <td>
                    <!-- <form action=#> -->
                        <input type="text" name="repw">
                    <!-- </form> -->
                </td>
            </tr>
        </table>
  
        <Table width='400px'>
            <tr bgcolor = 'gray'  align = 'left'> 
                <th colspan='2' height ='50px'>Step2. 개인정보</th>
            </tr>

            <tr bgcolor = 'whitesmoke'> 
                <td align = 'right' height ='35px' width ='150px'>성별</td>
                <td>
                    <input type="radio" name = "gender" value="male"><span>남</span>
                    <input type="radio" name = "gender" value="female"><span>여</span>

                </td>
            </tr>
            <tr bgcolor = 'whitesmoke'> 
                <td align = 'right' height ='35px'>혈액형</td>
                <td>
                    <select name="blood">
                        <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'> 
                <td align = 'right' height ='35px'>생일</td>
                <td>
                    <input type="date" name = "birth">
                </td>
            </tr>
        </Table>


        <Table width='400px'>
            <tr bgcolor = 'gray'  align = 'left'> 
                <th colspan='2' height ='50px' >Step3.선호도</th>
            </tr>

            <tr bgcolor = 'whitesmoke'> 
                <td align = 'right' height ='35px' width ='150px'>취미</td>
                <td>
                    <input type="checkbox" name = "hobby" value="sco"><span>축구</span>
                    <input type="checkbox" name = "hobby" value="base"><span>야구</span>    
                    <input type="checkbox" name = "hobby" value="bask"><span>농구</span>    

                </td>
            </tr>
            
            <tr bgcolor = 'whitesmoke'> 
                <td align = 'right' height ='35px' width ='150px'>좋아하는 색깔</td>
                <td>
                    <input type="color" name = "like" value="col">
                       

                </td>
            </tr>
            
            
        </Table>

        <Table width='400px'>
            <tr bgcolor = 'gray'  align = 'left'> 
                <th colspan='2' height ='50px'>Step4.하고 싶은 말 </th>
            </tr>

            <tr> 
                <td bgcolor = 'white' height ='150px' colspan="2">
                    <textarea name="shuo" id="" cols="52" rows="10">

                    </textarea>
                </td>
            </tr>
            
            
        </Table>

    <input type = 'submit' value = '제출'>

</body>

 

<JAVA>

 

package servlet;

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;


@WebServlet("/Ex09_userInfo")
public class Ex09_userInfo extends HttpServlet {
	
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		 // 1. post 방식 : 디코딩 (코드>> 문자로)
		 request.setCharacterEncoding("UTF-8");
		
		 // 2. 데이터 꺼내오기
		 String id = request.getParameter("id");
		 String pw = request.getParameter("pw");
		 String repw = request.getParameter("repw");
		 String gender = request.getParameter("gender");
		 String blood = request.getParameter("blood");
		 String birth = request.getParameter("birth");
		 String[] hobby = request.getParameterValues("hobby");
		 String like = request.getParameter("like");
		 String shuo = request.getParameter("shuo");
		
		 // 3. 출력 타입
		response.setContentType("text/html; charset=utf-8");
		
		 // 4. 웹에 출력 
		PrintWriter out = response.getWriter();
		out.print("<style> div{display:inline-block; "
		            + "width:100px; height:20px;"
		            + "background-color : "
		            + like
		            + ";} </style>");

		out.print("아이디:" + id+"<br>");
		out.print("비밀번호:" + pw+"<br>");
		out.print("비밀번호 확인:" + repw+"<br>");
		out.print("성별:" + gender+"<br>");
		out.print("혈액형:" + blood+"<br>");
		out.print("생일:" + birth+"<br>");
		  //out.print("취미:" + hobby+"<br>"); // 여기가 문제
		out.print("취미:");
		for(int i=0; i<hobby.length; i++) {
			out.print(hobby[i]+" ");
		}
		out.print("<br>");
		//div는 display block
		// out.print("색깔:" + like+"<div style = 'width: 100px; height: 10px; background-color:"+like+"'> <br>"); //여기도 문제
		out.print("색깔:" + like);
		out.print("<div> </div>"+"<br>");
		out.print("하고싶은말:" +shuo+"<br>");
		
	}

}

출처: 스마트인재개발월

728x90
반응형