[활용] Servlet, 싱글 파라미터 넘기기
웹/백엔드, Spring

[활용] Servlet, 싱글 파라미터 넘기기

책 정보 전달 프로그램

 

1. 로직

2. 코드

Login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style type="text/css">

</style>
</head>
<script type="text/javascript">
	$(document).ready(function() {
		$('button').click(function (){
			var id = $('input[type=text]').val();
			var pw = $('input[type=password]').val();
			if(id=='' || pw==''){
				alert('아이디 비번 확인해!!!')
				return false;
			}
		})
	});
</script>
<body>
	<div style="width: 500px; text-align: center; border: 1px solid black; padding-bottom: 20px">
		<h1>로그인 해!</h1>
		<form action="MainServlet.do" method="post">
			<table style="margin: 10px auto">
				<tbody>
					<tr>
						<td class="first">id:</td>
						<td><input type="text" name='id'><br></td>
					</tr>
					<tr>
						<td>password:</td>
						<td><input type="password" name='pw'><br></td>
					</tr>
				</tbody>
			</table>
			<button type="submit">Login</button>
		</form>
	</div>
</body>
</html>

form태그 사용해서 서블릿으로 data전송

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

 

Mainservlet.java

넘어온 정보를 servlet에서 판단 후, 에러페이지나 Book.html로 넘긴다

package servlet;

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;

@WebServlet("/MainServlet.do")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public MainServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String data[] = new String[3];
		request.setAttribute("title", request.getParameter("title"));
		request.setAttribute("author", request.getParameter("author"));
		request.setAttribute("price", request.getParameter("price"));
		request.getRequestDispatcher("info.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		if(id.equals("moon") && pw.equals("123")) {
			response.sendRedirect("Result.html");
		}else {
			request.setAttribute("msg", "아이디와 비번을 확인해 주세요.");
			request.getRequestDispatcher("Error.jsp").forward(request, response);
		}
	}
}

 

 

 

Book.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
	$(document).ready(function(){
		$('button').click(function(){
			var text = $('input[name=title]').val();
			var author = $('input[name=author]').val();
			var price = $('input[name=price]').val();
			console.log(text+" |"+ author+"|"+price);
			if(text=='' || author=='' || price==''){
				alert("다시 입력해!!!")
				return false;
			}
		})
	});
</script>
<body>
	<div style="width: 500px; text-align: center; border: 1px solid black; padding: 20px 0">
		<h1>도서 등록</h1>
		<form action="MainServlet.do" method="get">
			<table style="margin: 0 auto;">
				<tbody>
					<tr>
						<td>제목: </td>
						<td><input type="text" name="title"></td>
					</tr>
					<tr>
						<td>저자: </td>
						<td><input type="text" name="author"></td>
					</tr>
					<tr>
						<td>가격: </td>
						<td><input type="text" name="price"></td>
					</tr>
				</tbody>
			</table>
			<button type="submit">등록</button>
		</form>
	</div>
</body>
</html>

Error.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>
	<div style="width: 500px; text-align: center; border: 1px solid black; padding-bottom: 20px">
		<h1>도서 정보 관리 ERROR</h1>
		<%=request.getAttribute("msg") %>
	</div>
</body>
</html>

Info.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>
<style type="text/css">
	table{
		border-collapse: collapse;
	}
	table, tr,td{
		border: 1px solid black;
	}
	a{
		text-decoration: none;
	}
</style>
</head>
<body>
	<div class="content" style="width: 500px; text-align: center; border: 1px solid black; padding: 20px 0">
		<h3>입력된 도서 정보</h3>
		<table style="margin: 10px auto">
			<thead style="background-color: gray; color: white;">
				<tr>
					<td colspan="2">입력된 도서 정보</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td style="background-color: gray;color: white;">제목</td>
					<td><%=request.getAttribute("title")%></td>
				</tr>
				<tr>
					<td style="background-color: gray;color: white;">저자</td>
					<td><%=request.getAttribute("author")%></td>
				</tr>
				<tr>
					<td style="background-color: gray;color: white;">가격</td>
					<td><%=request.getAttribute("price")%></td>
				</tr>
			</tbody>
		</table>
		<a href="Book.html">도서 등록</a>
	</div>
</body>
</html>

3. 실행