๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ์–ธ์–ด/JSP

JSP - ์„œ๋ธ”๋ฆฟ ๊ธฐ์ดˆ ( model 2 ๋ฐฉ์‹ ) STEP 1

by yunamom 2022. 3. 28.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค.

 

์„œ๋ธ”๋ฆฟ ์˜ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํฌ์ŠคํŒ…ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์ดˆ๋ณด์˜ ์ž…์žฅ์ด๋‹ˆ ์˜คํƒ€/์‹ค์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ๋‹ต๊ธ€ ๋ถ€ํƒ๋“œ๋ ค์š” :D

 

๋จผ์ € ์‹ค์Šต์„ ์œ„ํ•ด jspํŒŒ์ผ ์„ ๋จผ์ € ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘‡LoginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="box">
<div class="login">
	<form action="LoginPro.do" method="post">
		<div>
		<input type="text" name="id" placeholder=" ์•„์ด๋””">
		</div>
		<div>
		<input type="text" name="pw" placeholder=" ๋น„๋ฐ€๋ฒˆํ˜ธ">
		</div>
		<div>
		<input class="btn" type="submit" value="Login">
		</div>
		<div>
		<input class="btn" type="button" value="Join">
		</div>
	</form>
</div>
</div>
</div>

</body>
</html>

๐Ÿ‘‡style.css

@charset "UTF-8";

body{	
	background: #fbf0d7;
	font-size: 17px;
}
.wrapper{
	display: flex;
	justify-content: center;
	align-items: center;
}
.box{
	margin: 5rem;
}
.login{
	width: 300px;
}
input{
	margin: 3px;
	width: 100%;
	height: 35px;
	border: 0;
	border-radius: 0.3rem;
	font-size: 17px;
}
.btn{
	width: 100%;
	height: 50px;
	border: 0;
	background-color: #706b61;
	color: #fff;
	cursor: pointer;
}
.btn:hover{
	border: 1px solid #706b61;
	background-color: #fbf0d7;
	color: #706b61;	
}

LoginForm.jsp

<form action="LoginPro.do" method="post">

form ํƒœ๊ทธ์— action ๋ถ€๋ถ„์„ ๋ณด์‹œ๋ฉด ์ง€๊ธˆ๊ป ์ž‘์„ฑํ•ด์™”๋˜ .jsp ๊ฐ€ ์•„๋‹Œ .do ๋ผ๊ณ  ์ž‘์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋ถ€๋ถ„์ด ์•„์ฃผ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ์„œ๋ธ”๋ฆฟ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

controller ํŒจํ‚ค์ง€ ์ƒ์„ฑํ›„ ๊ทธ์•ˆ์— LoginPro.java ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’ก์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•˜์…”์•ผ ํ• ๋ถ€๋ถ„์€ @WebServlet ์–ด๋…ธํ…Œ์ด์…˜ ๊ฒฝ๋กœ์™€ form ํƒœ๊ทธ์˜ action ์ด๋ฆ„์ด ๊ฐ™์•„์•ผํ•ฉ๋‹ˆ๋‹ค (*๋Œ€์†Œ๋ฌธ์ž ์ฃผ์˜)

๐Ÿ‘‡LoginPro.java ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ฃผ์‹ ํ›„ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

package com.test.web.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;


/* form action ์˜ ์ด๋ฆ„๊ณผ ์•„๋ž˜ ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค */
@WebServlet("/LoginPro.do")
public class LoginPro extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		requestPro(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		requestPro(request, response);
	}
	
	public void requestPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		System.out.println("์•„์ด๋”” = " + id);
		System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ = " + pw);
	}
}

์ž‘์„ฑํ›„ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์ฃผ์„ธ์š”.

 

์ฝ˜์†”์ฐฝ์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋„˜์–ด์˜ค๋Š”๊ฒƒ์„ ํ™•์ธํ•˜์‹ค์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜Š

 

์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” ์ด๋ ‡๊ฒŒ ์„œ๋ธ”๋ฆฟ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด๋™ํ•˜๋Š”์ง€ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜ ์˜์ƒ์€ ๋งŽ์€ ๋„์›€์„ ๋ฐ›์€ ์‹ ํ˜•์„ญ ๊ฐ•์‚ฌ๋‹˜์˜ jsp mvc ๊ฐ•์˜์ž…๋‹ˆ๋‹ค.

 

์ง์ ‘ ํ•ด๋ณด๋ฉด์„œ ์ดํ•ดํ•˜๋Š”๊ฒŒ ์ฐธ ์ค‘์š”ํ•œ๊ฑฐ๊ฐ™์•„์š” ์—ฌ๋Ÿฌ๋ถ„๋„ ๊ผญ ๋”ฐ๋ผํ•ด๋ณด์„ธ์š” :D

 

 

300x250

์ฝ”๋“œ