Java

자바 + 서블릿/ 회원가입 페이지

유호야 2020. 11. 29. 23:21
반응형

아주 좋은 글을 발견해서, 참고해서 id와 pw 값만 가져가게 만들어봤다.

출처 : opentutorials.org/module/3569/21226

 

join_member_page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>회원가입</h1>
	<form action = "memberList" method = "post">
		ID : <input type = "text" name = "id" placeholder = "아이디를 입력해주세요"><br>
		PW : <input type = "password" name = "pw" placeholder = "비밀번호를 입력해주세요"><br>
		<input type = "submit" value = "가입"><br>
	</form>
</body>
</html>

 

MemberListServlet.java


import java.io.IOException;

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;

/**
 * Servlet implementation class MemberListServlet
 */
@WebServlet("/memberList") //MemberListServlet이었음
public class MemberListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberListServlet() {
    
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		//getParameter는 중복되지 않고 유일하게 하나만 넘어올 떄 사용된다.
        //getParameter의 반환값은 String이다. 
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		//Member객체에 회원정보를 저장.
		Member member = new Member();
		member.setId(id);
		member.setPw(pw);
		
		//HttpServletRequest 객체에 회원정보를 등록한다?
		//위의 두 값을 저장하는 건가?
		request.setAttribute("member", member);
		
		//????
		//디스패처에 의한 요청 재지정
        //멤버객체를 출력 할 서블릿의 URI를 지정한다.
		RequestDispatcher rd = request.getRequestDispatcher("memberOutput");
		rd.forward(request, response);
		
	}

}

 

Member.java

public class Member {
	
	private String id;
	private String pw;
	
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPw() {
		return pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	
}

 

 

MemberOutputServlet.java


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;

/**
 * Servlet implementation class MemberOutputServlet
 */
@WebServlet("/memberOutput") //원래 MemberOutputServlet이었음
public class MemberOutputServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberOutputServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter(); //이게 뭐지
		
		 //HttpServletRequest 객체에 등록된 회원 정보를 추출한다.
		Member member = (Member) request.getAttribute("member");
		out.println("User ID : " + member.getId());
		out.println("User PW : " + member.getPw());
		
		out.close();
		
		doGet(request, response);
	}

}
반응형

'Java' 카테고리의 다른 글

redirect forward 차이점  (0) 2020.11.30
HashMap이란?  (0) 2020.11.30
System.out.printf 정리  (0) 2020.11.18
[생활코딩 JAVA] 008 자바 문서 보는 법  (0) 2020.08.16
[생활코딩 JAVA] 008 직접 컴파일하고 실행하기  (0) 2020.08.16