본문 바로가기
뒤끝 (Back-End)

[JSP] request 내장 객체

728x90
메소드 선언 반환값 타입 의미
getParameterNames() java.util.Enumeration 요청된 모든 파라미터의 이름과 값을 Enumeration 객체 타입으로 반환
getParameter(String name) String HTTP 요청에 따라 name 파라미터의 값을 반환하고 요청 파라미터의 값이 없으면 null 반환
getParameterValues(String name) String[ ] 파라미터 리스트에서 이름이 name인 파라미터가 여러 개일 경우 배열 형태로 전달받음, 요청 파라미터 값이 없으면 null로 반환
getParameterMap() Java.util.Map 모든 요청 파라미터의 이름과 값을 Map 객체 타임으로 반환

 

String 변수명 = request.getParameter(요청한 파라미터명);
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
  <title> 로그인 화면 </title>
</head>
<body>
	Home > Login
	<hr>
	<form action="login_process.jsp" method="get">
		ID : <br> 
			<input type="text" name="id"><br><br>
		PASSWORD : <br>
		<input type="password" name="passwd"><br><br>
		&nbsp; <input type="submit" value=" Login "> &nbsp;
		<input type="reset" value=" Cancel ">
	</form>
</body>
</html>

loginForm.jsp

<body>
	Home > Login > Information
	<hr>
	<%
	request.setCharacterEncoding("utf-8");  //한글 깨짐 방지
		
	String id = request.getParameter("id");
	String passwd = request.getParameter("passwd");
	%>
	
	<table border=1>
		<tr>
				<th>아이디</th>
				<th>비밀번호</th>	
		</tr>
		<tr>
				<td><%= id %></td>
				<td><%= passwd %></td>	
		</tr>
	</table>	
</body>

login_process.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title> 개인 정보 입력 </title>
</head>
<body>
	Home > Person Information
	<hr>
	<form action="personInfo_proc.jsp" name="person_info" method="get">
		이름 : 
		<input type="text" name="p_name" size="10"><br><br>
		
		사원코드 : 
		<input type="text" name="e_code" size="10"><br><br>
		
		성별 : 
		<input type="radio" name="gender" value="남">남
		<input type="radio" name="gender" value="여">여 <br><br>
		
		취미 :
		<input type="checkbox" name="hobby1" value="운동">운동
		<input type="checkbox" name="hobby2" value="독서">독서			 
		<input type="checkbox" name="hobby3" value="음악감상">음악감상 <br>
        /*
        <input type="checkbox" name="hobby" value="운동">운동
		<input type="checkbox" name="hobby" value="독서">독서			 
		<input type="checkbox" name="hobby" value="음악감상">음악감상 <br>
        */
		<hr> &nbsp;
		
		<input type="submit" value=" 가입하기 "> &nbsp;&nbsp;
		<input type="reset" value=" 다시작성 ">
	</form>
</body>
</html>

personInfo.jsp

<body>
	Home > Person Information
	<hr>
<%
request.setCharacterEncoding("utf-8");

String p_name = request.getParameter("p_name");
String e_code = request.getParameter("e_code");
String gender = request.getParameter("gender");
String hobby1 = request.getParameter("hobby1");
String hobby2 = request.getParameter("hobby2");
String hobby3 = request.getParameter("hobby3");
// String[] hob = request.getParameterValues("hobby");

%>

	<table border=1 width=350>
		<tr>
				<th>이름(성별)</th>
				<th>사원코드</th>
				<th>취미</th>		
		</tr>
		<tr align=center>
				<td><%=p_name + "(" + gender +")" %></td>
				<td><%=e_code %></td>
				<td><%=hobby1 + "&nbsp;" + hobby2 + "&nbsp;" + hobby3 %></td>
                /*
                <td>
					<%
					if(hob != null) {
						for(int count = 0; count < hob.length; count++) {
							out.println(" " + hob[count]);
						}
					}	
					else {
						out.println("없음");
					}
					%>
				</td>
                */
		</tr>
	</table>	

</body>

personInfo_proc.jsp
주석 코드 실행 결과


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title> 회원 가입 </title>
</head>
<body>
	Home > Join the Membership
	<hr>
	<form action="form_membership02.jsp" name="person_info" method="get">
		<fieldset style="width:330px">
			<legend> 개인 정보 입력 </legend><p>
	
			아이디 : <br>
			<input type="text" name="p_id" size="16"><br><br>
		
			비밀번호 : <br> 
			<input type="password" name="p_pw" size="16"><br><br>
			
			이름 : <br> 
			<input type="text" name="p_name" size="16"><br><br>		
		
			연락처 : <br>
			<select name="choice" style="width:60px;height:20.5px"> 
				<option value="choice"> 선택 </option>
				<option value="KT"> KT </option>
				<option value="SKT"> SKT </option>
				<option value="LGU+"> LGU+ </option>						
			</select>
			<input type="text" maxlength="3" size="3" name="phone1"> -
			<input type="text" maxlength="4" size="4" name="phone2"> -
			<input type="text" maxlength="4" size="4" name="phone3"><br><br>  		
			
			성별 : 
			<input type="radio" name="gender" value="남성">남
			<input type="radio" name="gender" value="여성">여 <br><br>
			
			취미 :
			<input type="checkbox" name="hobby" value="운동">운동
			<input type="checkbox" name="hobby" value="독서">독서			 
			<input type="checkbox" name="hobby" value="여행">여행
			<input type="checkbox" name="hobby" value="음악감상">음악감상 <br><br>			
			
			본인 소개 : <br>	
			<textarea name="Introduction" cols="50" rows="3">
			</textarea>			
			<hr>
			
		<div align="center">
			<input type="submit" value=" 가입하기 "> &nbsp;&nbsp;
			<input type="reset" value=" 다시작성 ">
		</div><br>
		</fieldset>
	</form>
</body>
</html>

<body>
	Home > Personal Information Inquiry
	<hr>
	<%
		request.setCharacterEncoding("UTF-8");
	
		String id = request.getParameter("p_id");
		String pw = request.getParameter("p_pw");
		String name = request.getParameter("p_name");
	
		String choice = request.getParameter("choice");
		String ph01 = request.getParameter("phone1");
		String ph02 = request.getParameter("phone2");
		String ph03 = request.getParameter("phone3");
	
		String gender = request.getParameter("gender");
		String[] hob = request.getParameterValues("hobby");
		String intro = request.getParameter("Introduction");		
	%>
	<p> 아이디 : <%= id %>
	<p> 비밀번호 : <%= pw %>
	<p> 이름 : <%= name %>
	<p> 연락처 : <%= choice %> <%= ph01 %>-<%= ph02 %>-<%= ph03 %>
	<p> 성별 : <%= gender %>
	<p> 취미 : 
		<%
			if(hob != null)
			{
				for(int count = 0; count < hob.length; count++)
				{
					out.println(" " + hob[count]);
				}
			}
            else {
            	out.println("없음");
            }
		%>
	<p> 본인 소개 : <%= intro %>
</body>

?p_id=zen&p_pw=123456&p_name=젠&choice=KT&phone1=010&phone2=1234&phone3=5678&gender=여성&hobby=운동&hobby=여행&Introduction=Hi%09

728x90

'뒤끝 (Back-End)' 카테고리의 다른 글

[JSP] 액션 태그  (0) 2024.10.18
[JSP] 폼 태그  (3) 2024.10.14
[JSP] JSP의 동작 구조  (1) 2024.10.14
[JSP] 하이퍼 링크 & 테이블 태그  (0) 2024.10.14
[JSP] 절대 경로와 상대 경로  (1) 2024.10.09