JSP
JSP - userBean_ex 회원정보 입력/수정
기련이
2020. 6. 12. 15:56
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>시작 - 첫페이지</title>
</head>
<body>
<a href="pInputFrm.jsp">입력하기</a><br>
<a href="./pupdate">수정하기</a><br>
</body>
</html>
<%@ 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>
<%@ 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:setProperty name="pInfo" property="*"/>
<jsp:forward page="/pinput"/>
작성한 회원정보를 inPorc.jsp로 보낼 데이터들을 Bean에 전달
package com.bean;
public class PersonInfo {
//이름, 성별, 나이
private String name;
private String gender;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
bean -> class에 데이터 저장
데이터 저장 완료 페이지▲
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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", "/pupdate"})
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) throws ServletException, IOException {
//uri로 서비스를 구분해서 처리
String command = request.getServletPath();
PersonInfo pInfo = null;
if(command.equals("/pinput")) {
//DB에 입력처리(서비스-> DAO -> DB)
//여기서는 내용확인을 위해 출력만 함.
pInfo = (PersonInfo)request.getAttribute("pInfo");
System.out.println("이름 : " + pInfo.getName());
System.out.println("성별 : " + pInfo.getGender());
System.out.println("나이 : " + pInfo.getAge());
//브라우저 창에 출력하기
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>입력결과</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>입력 완료</h2>");
out.println("<a href='index.jsp'>시작으로</a>");
out.println("</body>");
out.println("</html>");
}
else if(command.equals("/pupdate")) {
//DB에서 해당 키로 검색한 정보를 가져와서
//jsp 페이지로 전달
//여기서는 임의의 데이터를 전달함
pInfo = new PersonInfo();
pInfo.setName("유상민");
pInfo.setGender("남자");
pInfo.setAge(23);
request.setAttribute("uInfo", pInfo);
RequestDispatcher dis = request.getRequestDispatcher("pUpdate.jsp");
dis.forward(request, response);
}
}
}
데이터 전송이 되면 out.println으로 입력완료 문구와 시작페이지로갈 수 있는는 <a href>태그 입력
else if 로 수정시 저장되는 임의의 데이터를 입력
else if(command.equals("/pupdate")) {
//DB에서 해당 키로 검색한 정보를 가져와서
//jsp 페이지로 전달
//여기서는 임의의 데이터를 전달함
pInfo = new PersonInfo();
pInfo.setName("유상민");
pInfo.setGender("남자");
pInfo.setAge(23);
request.setAttribute("uInfo", pInfo);
RequestDispatcher dis = request.getRequestDispatcher("pUpdate.jsp");
dis.forward(request, response);
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>수정하기</title>
</head>
<body>
<h3>검색한 회원</h3>
<form action="inProc.jsp" method="post">
<table>
<tr>
<th width="100px">이름</th>
<td width="200px"><input type="text" name="name" value="${uInfo.name}"></td>
</tr>
<tr>
<th>성별</th>
<td width="200px"><input type="text" name="gender" value="${uInfo.gender}"></td>
</tr>
<tr>
<th>나이</th>
<td width="200px"><input type="text" name="age" value="${uInfo.age}"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" value="전송">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
<a href="index.jsp">시작으로</a>
</body>
</html>
DB에서 입력한 정보를 가져와서 페이지에 표출
수정된 데이터가 DB에 입력됨