상세 컨텐츠

본문 제목

JSP - JSTL 서버에 데이터 전달2(회원정보입력)

JSP

by 기련이 2020. 6. 11. 15:57

본문

액션 태그(Action Tag)
  XML 형식으로 코드를 기술하는 문법.
    
  액션 태그의 종류
  1. 표준 액션 태그
     include와 forward가 있음. bean 관련 태그(중요)
  2. 커스텀 액션 태그 
 별도의 라이브러리를 설치(포함)해야만 사용할 수 있는 태그
 (대표적으로 JSTL)

  예) 표준 액션의 include
  <jsp:include page="포함할페이지.jsp">
  
  JSTL의 변수 선언(cnt라는 변수를 만들어서 0으로 초기화)
  <c:set var="cnt" value="0">

package com.bean;

public class PersonInfo {
	//이름, 성별, 나이
	private String name;
	private String gneder;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGneder() {
		return gneder;
	}
	public void setGneder(String gneder) {
		this.gneder = gneder;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

데이터(회원정보)를 저장할 bean을 생성

회원정보를 입력

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보</title>
</head>
<body>
	<h2>회원 정보 입력</h2>
	<form action="inProc.jsp" method="post">
		<table>
			<tr>
				<th width="100px">이름</th>
				<td width="200px"><input type="text" name="name"></td>
			</tr>
			<tr>
				<th>성별</th>
				<td width="200px"><input type="text" name="gender"></td>
			</tr>
			<tr>
				<th>나이</th>
				<td width="200px"><input type="text" name="age"></td>
			</tr>
			<tr>
				<td colspan="2" style="text-align: center"><input type="submit"
					value="전송"> <input type="reset" value="취소"></td>
			</tr>
		</table>
	</form>
</body>
</html>

DB에 입력된 회원정보

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8");%>
<jsp:useBean id="pInfo" class="com.bean.PersonInfo"
	scope="request"/>
	
<jsp:setProperty name="pInfo" property="name"
	value="${param.name}"/>
	
<jsp:setProperty name="pInfo" property="gender"
	value="${param.gender}"/>
	
<jsp:setProperty name="pInfo" property="age"
	value="${param.age}"/>

<jsp:forward page="/pinput"/>

회원정보를 전달

package com.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.PersonInfo;

@WebServlet("/pinput")
public class PersonController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) {
		String identy = request.getServletPath();
		System.out.println(identy);
		
		PersonInfo pInfo = (PersonInfo)request.getAttribute("pInfo");
		
		System.out.println("이름 : " + pInfo.getName());
		System.out.println("성별 : " + pInfo.getGender());
		System.out.println("나이 : " + pInfo.getAge());
	}

}

회원정보를 출력

<jsp:setProperty name="pInfo" property="*"/>
<jsp:forward page="/pinput"/>

" * " 를 써서 모든 데이터를 쉽게가져올 수 있다.

관련글 더보기