회원등록 페이지를 만들어보자.
메인페이지에서 회원등록 페이지로 넘어가는 servlet을 작성한다.(회원등록 페이지는 admin계정일때만 접근할 수 있다.)
<EmpAddFormSerlvet.java>
package com.green.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;
/**
* Servlet implementation class EmpAddFormServlet
*/
@WebServlet("/EmpAddFormServlet")
public class EmpAddFormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EmpAddFormServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = "emp_add.jsp";
request.getRequestDispatcher(url).forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
회원등록 페이지를 만들기 위해 HTML코드는 앞에서 만든 회원 정보 수정 페이지에서 가져와 조금만 수정한다.
readonly 부분을 다 지워주고 value 부분에 값을 지워준다.
<emp_add.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Member detailed Information</title>
</head>
<body>
<form method="post" action="EmpAddServlet" name="frm">
<table align="center" width="600">
<tr>
<td colspan="2">Join</td>
</tr>
<tr>
<td>ID</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>PASSWORD</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>NAME</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>LEVEL</td>
<td><select name="lev">
<option value="A">Admin</option>
<option value="B">Member</option>
</select></td>
</tr>
<tr>
<td>GENDER</td>
<td><select name="gender">
<option value="1">Male</option>
<option value="2">Female</option>
</select></td>
</tr>
<tr>
<td>PHONE</td>
<td><input type="text" name="phone"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="join">
<input type="reset" value="cancle">
<input type="button" value="main page" onclick="location.href='main.jsp'"></td>
</tr>
</table>
</form>
</body>
</html>
회원 등록할 수 있는 메소드를 작성한다.
<EmlolyeeDAO.java - insertEmployee>
public int insertEmployee(EmployeeVO empVo) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = -1;
String sql = "insert into employees(ID, PASS, NAME, LEV, GENDER, PHONE) values(?, ?, ?, ?, ?, ?)";
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, empVo.getId());
pstmt.setString(2, empVo.getPass());
pstmt.setString(3, empVo.getName());
pstmt.setString(4, empVo.getLev());
pstmt.setString(5, empVo.getGender());
pstmt.setString(6, empVo.getPhone());
result = pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
DBManager.close(conn, pstmt);
}
return result;
}
회원 등록 페이지에서 받아온 정보를 저장할 수 있는 SQL문을 작성하여 VO에 저장된 데이터를 pstmt를 통해 저장하도록 한다.
servlet을 통해서 jsp파일에 있는 데이터를 받아 메소드를 이용하여 저장하는 코드를 작성한다.
<EmpAddServlet.java>
package com.green.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.green.dao.EmployeeDAO;
import employee.EmployeeVO;
/**
* Servlet implementation class EmpAddServlet
*/
@WebServlet("/EmpAddServlet")
public class EmpAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EmpAddServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Korean setting
request.setCharacterEncoding("UTF-8");
EmployeeVO empVo = new EmployeeVO();
empVo.setId(request.getParameter("id"));
empVo.setPass(request.getParameter("pass"));
empVo.setName(request.getParameter("name"));
empVo.setLev(request.getParameter("lev"));
empVo.setGender(request.getParameter("gender"));
empVo.setPhone(request.getParameter("phone"));
// insert DB
EmployeeDAO empDao = EmployeeDAO.getInstance();
empDao.insertEmployee(empVo);
// show Employee info
EmployeeVO newEmp = empDao.getEmployee(empVo.getId());
request.setAttribute("employee", newEmp);
request.setAttribute("message", "Success join member");
request.getRequestDispatcher("update_done.jsp").forward(request, response);
}
}
728x90
반응형
'JSP, Servlet > JSP를 이용하여 회원관리 페이지 만들기' 카테고리의 다른 글
[JSP] 회원관리 페이지 만들기 - 메인페이지와 회원수정페이지 (0) | 2020.09.04 |
---|---|
[JSP] 회원관리 페이지 만들기 - 데이터 설정 및 데이터 연결 (0) | 2020.09.03 |
[JSP] 회원관리 페이지 만들기 - 로그인 폼 만들기 (0) | 2020.09.03 |