[Day3] JSP/Servlet 3 [12/6]
request ๊ฐ์ฒด ํ๋ผ๋ฏธํฐ
p 76 request ๊ฐ์ฒด์ ํ๋ผ๋ฏธํฐ ์ฝ๊ธฐ ๋ฉ์๋
*** String request.getParameter()
*** String[] request.getParameterValues()
Enumeration getparameterNames()
Map getParameterMap()
jsp
<form action="ex05_ok.jsp" method="post">
<!-- <form action="ex05_ok.jsp" method="get"> -->
์ด๋ฆ : <input type="text" name="name" placeholder="์ด๋ฆ์ ์
๋ ฅํ์ธ์"><br>
์ฑ๋ณ :
<input type="radio" name="gender" value="m" checked="checked"> ๋จ์
<input type="radio" name="gender" value="f">์ฌ์<br>
์ข์ํ๋ ๋๋ฌผ :
<input type="checkbox" name="pet" value="dog" checked="checked">๊ฐ
<input type="checkbox" name="pet" value="cat">๊ณ ์์ด
<input type="checkbox" name="pet" value="pig" checked> ๋ผ์ง
<br>
<input type="submit">
</form>
ok.jsp
request.setCharacterEncoding("UTF-8");
// ์คํฌ๋ฆฝํธ๋ฆฟ : ์๋ฐ์ฝ๋ฉ.
// ?name=ํ๊ธธ๋
String name = request.getParameter("name");
String gender = request.getParameter("gender"); // "m" / "f"
// pet=dog&pet=cat
String [] pets = request.getParameterValues("pet");
> ์ด๋ฆ : <%= name %><br>
> EL ์ด๋ฆ : ${ param.name }<br>
> ์ฑ๋ณ : <%= gender.equals("m")? "๋จ์" :"์ฌ์" %><br>
> ์ข์ํ๋ ๋๋ฌผ : <%= Arrays.toString(pets) %><br>
<hr>
<%
// request.getParameterNames(): ์ค๋ช
// ?[name]=admin&[gender]=m&[pet]=dog&pet=dog
Enumeration<String> en = request.getParameterNames();
while(en.hasMoreElements()){
String pname = en.nextElement();
%>
<li><%= pname %></li>
<%
}
%>
<hr>
<ol>
<%
// pname, pvalue
Map<String, String[]> map = request.getParameterMap();
Set<Entry<String, String[]>> set = map.entrySet();
Iterator<Entry<String, String[]>> ir = set.iterator();
while(ir.hasNext()){
Entry<String, String[]> entry = ir.next();
String pname = entry.getKey();
String[] pvalue = entry.getValue();
%>
<li><%= pname %> : <%= Arrays.toString(pvalue) %></li>
<%
}
%>
p 88 ~ request ๊ธฐ๋ณธ ๊ฐ์ฒด, ์์ฒญ ํค๋ ์ ๋ณด ์ฒ๋ฆฌ.
p 89 ํ 3.6 ํค๋ ๋ฉ์๋
*** String getHeader(String name) ์ง์ ํ ์ด๋ฆ์ ํค๋ ๊ฐ์ ๋ฐํ
En getHeaders(String name) ์ง์ ํ ์ด๋ฆ์ ํค๋ ๋ชฉ๋ก ๋ฐํ
*** En getHeaderNames() ๋ชจ๋ ํค๋ ์ด๋ฆ์ ๋ฐํ
int getIntHeader(String name) ์ง์ ํ ํค๋์ ๊ฐ์ ์ ์๋ก ๋ฐํ
long getDateHeader( String name) ์ง์ ํ ํค๋์ ๊ฐ์ ์๊ฐ๊ฐ์ผ๋ก ๋ฐํ( 1970.1.1 ~ ms)
์์ฒญ ํค๋ ๋ชฉ๋ก ์ถ๋ ฅ
<ol>
<%
Enumeration<String> en = request.getHeaderNames();
while( en.hasMoreElements() ){
String headerName = en.nextElement();
String headerValue = request.getHeader(headerName);
%>
<li><%= headerName %> : <%= headerValue %></li>
<%
} // while
%>
</ol>
p 90 JSP ๊ธฐ๋ณธ 9๊ฐ์ง ๊ฐ์ฒด : request ์์ฒญ ๊ฐ์ฒด
1) ํค๋ ์ ๋ณด ์ฝ๊ธฐ.
p 90 JSP ๊ธฐ๋ณธ๊ฐ์ฒด : [ response ์๋ต ๊ฐ์ฒด ] ๊ธฐ๋ฅ
* request ๊ธฐ๋ณธ ๊ฐ์ฒด๊ฐ ์น ๋ธ๋ผ์ฐ์ ๊ฐ ์ ์กํ ์์ฒญ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ค๋ฉด, reponse ๊ธฐ๋ณธ ๊ฐ์ฒด๋ ์น ๋ธ๋ผ์ฐ์ ์ ๋ณด๋ด๋ ์๋ต ์ ๋ณด๋ฅผ ๋ด๋๋ค.
p 90 JSP ๊ธฐ๋ณธ๊ฐ์ฒด : [ response ์๋ต ๊ฐ์ฒด ] ๊ธฐ๋ฅ
1) ํค๋ ์ ๋ณด ์ ๋ ฅ(์ค์ )
p91 ํ 3.7 ํค๋ ์ ๋ณด ์ถ๊ฐ ๋ฉ์๋
addDateHeader( name, long) addHeader(name, value); addIntHeader(name, value) setDateHeader() setHeader() setIntHeader() boolean containsHeader( name ) |
2) *** ๋ฆฌ๋ค์ด๋ ํธ(redirect) ํ๊ธฐ *** p 93
- ํ์ด์ง ์ด๋ํ๋ ๊ธฐ๋ฅ
- ์ฌ์ ์ ์๋ฏธ : ๋ค๋ฅธ ๋ฐฉํฅ์ผ๋ก ๋ค์ ๋ณด๋ด๋ค(์ ์กํ๋ค)
๊ทธ๋ฆผ 3.19 ๋ฆฌ๋ค์ด๋ ํธ
ใฑ. ํด๋ผ์ด์ธํธ <- ์๋ต(์ง์)
ใด. ํด๋ผ์ด์ธํธ ์์ฒญ ->
- ์น ์๋ฒ๊ฐ ์น๋ธ๋ผ์ฐ์ ์๊ฒ ๋ค๋ฅธ ํ์ด์ง๋ก ์ด๋ํ๋ผ๊ณ ์๋ต์ ํ๋ ๊ธฐ๋ฅ
-> ํด๋ผ์ด์ธํธ ์น๋ธ๋ผ์ฐ์ A๊ฐ B์๊ฒ ์์ฒญ -> B๊ฐ Cํํ ์์ฒญํ๋ผ๊ณ ๋งํ๋ ์ง์๊ฐ ๋ฐ๋ก ์ผ์ด๋์ง ์๊ณ -> B๊ฐ ๋ค์ ํด๋ผ์ด์ธํธ์ ๊ฐ๋ค๊ฐ ๋ค์ C์๊ฒ ์์ฒญ (๋ฆฌ๋ค์ด๋ ํธ) |
์) ex03.jsp ์ธ์ฆ ์์ฒญ -> ex03_ok.jsp
์์ด๋ ํ์ํ ์ด๋ธ /์์ด๋/๋น๋ฐ๋ฒํธ ํ์ธ
๋น๋ฐ๋ฒํธ ์ธ์ฆ O -> ์์ฒญ ํ์ด์ง, ๋ฉ์ธํ์ด์ง ์ด๋
์ธ์ฆ X -> ๋ก๊ทธ์ธ ํ์ด์ง ์ด๋
[๋ก๊ทธ์ธ]
-jsp-
<%
// ๋ก๊ทธ์ธ ์ฑ๊ณต ex03.jsp?id=admin&ok
// ๋ก๊ทธ์ธ ์คํจ ex03.jsp?error
String ok = request.getParameter("ok");
String error = request.getParameter("error");
if( ok != null ){ // ๋ก๊ทธ์ธ ์ฑ๊ณต
%>
<script>
$(function (){
alert("๋ก๊ทธ์ธ ์ฑ๊ณต");
});
</script>
<%
}else if( error != null ){ // ๋ก๊ทธ์ธ ์คํจ
%>
<script>
$(function (){
alert("๋ก๊ทธ์ธ ์คํจ");
$(":text").eq(0)
.focus()
.select();
});
</script>
<%
}
%>
- script ํ๊ทธ๋ ์๋ฒ๊ฐ ์๋๋ผ ์๋ตํ๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์คํํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ํผ์ผํธ ๋ฐ์ ๋ฌ์ผํจ!
- function์ผ๋ก ๋ฃ๋ ์ด์ : form ํ๊ทธ๊ฐ ์๋์ ์๊ธฐ ๋๋ฌธ์ ์ธ์ํ์ง ๋ชปํจ, function์ ๋ฃ์ด์ค์ผํจ!
<form action="ex03_ok.jsp" method="get">
์์ด๋ : <input type="text" name="id" value="admin"><br>
๋น๋ฐ๋ฒํธ : <input type="password" name="passwd" value="1234"><br>
<input type="submit" value="logon">
</form>
-ok.jsp-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
// DB ์ฐ๋ ์ฒ๋ฆฌ X
String location = "ex03.jsp";
if( id.equals("admin") && passwd.equals("1234") ){
// ๋ก๊ทธ์ธ ์ฑ๊ณต : jsp ์ฟ ํค, ์ธ์
location += "?id="+id+"&ok";
}else{
// ๋ก๊ทธ์ธ ์คํจ
location += "?error";
}
response.sendRedirect(location);
// js location.href=location
%>
*์ฃผ์ : jsp - ์๋ ํ๋ผ๋ฏธํฐ ๊ฐ ๊ธฐ์ต
<% // ex03_02.jsp param1 = null // ex03_02.jsp?error param1 = "" // ex03_02.jsp?error=404 param1 = 404 String param1 = request.getParameter("error"); %> param1 = <%= param1 %> |
์) ex04.jsp ex04_ok.jsp
๋ก๊ทธ์ธํ๋ฉด๊ตฌ์ฑ
๋ก๊ทธ์ธ์ฑ๊ณต ํ๋ฉด๊ตฌ์ฑ
๊ฒ์ํ ๋ฉ๋ด - ๋ก๊ทธ์ธ O X ๋ชจ๋ ์ฌ์ฉ
์ค๋ฌธ์กฐ์ฌ ๋ฉ๋ด - ๋ก๊ทธ์ธ O ์ฌ์ฉ
์ผ์ ๊ด๋ฆฌ ๋ฉ๋ด
-jsp-
String ok = request.getParameter("ok");
String error = request.getParameter("error");
<div id="logon">
<form action="ex04_ok.jsp" method="get">
์์ด๋ : <input type="text" name="id" value="admin"><br>
๋น๋ฐ๋ฒํธ : <input type="password" name="passwd" value="1234"><br>
<input type="submit" value="logon">
</form>
</div>
<div id="logout">
[${ param.id }]๋ ๋ก๊ทธ์ธํ์
จ์ต๋๋ค.<br>
<button>๋ก๊ทธ์์</button>
</div>
<script>
$("#logout").hide();
</script>
<a href="#">๊ฒ์ํ </a><br>
<%
if( ok != null ){ // ๋ก๊ทธ์ธ ์ฑ๊ณต
%>
<a href="#">์ค๋ฌธ์กฐ์ฌ </a><br>
<a href="#">์ผ์ ๊ด๋ฆฌ </a><br>
<script>
$(function (){
alert("๋ก๊ทธ์ธ ์ฑ๊ณต");
$("#logon").hide();
$("#logout").show();
});
</script>
<%
}else if( error != null ) { // ๋ก๊ทธ์ธ ์คํจ
%>
<script>
$(function (){
alert("๋ก๊ทธ์ธ ์คํจ");
$(":text").eq(0)
.focus()
.select();
});
</script>
<%
}
%>
-ok.jsp-
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
// DB ์ฐ๋ ์ฒ๋ฆฌ X
String location = "ex04.jsp";
if( id.equals("admin") && passwd.equals("1234") ){
// ๋ก๊ทธ์ธ ์ฑ๊ณต : jsp ์ฟ ํค, ์ธ์
// p 95 ์ค๋ช
// ***** ๋ฆฌ๋ค์ด๋ ํธ ํ ๋ ํ๊ธ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด ์๋ค๋ฉด
// ๋ฐ๋์ ์ธ์ฝ๋ฉ์ ํด์ผ ํ๋ค. -> ๋์ฝ๋ฉ์ ํด์ผ ํ๋ค.
location += "?id="+ URLEncoder.encode( id , "UTF-8" )+"&ok";
// URLDecoder.decode(s, charset)
}else{
// ๋ก๊ทธ์ธ ์คํจ
location += "?error";
}
response.sendRedirect(location);
// js location.href=location
%>
1. ๋ฆฌ๋ค์ด๋ ํธ( redirect )
2. ํฌ์๋ฉ( forward )
์ฐจ์ด์
ex05.jsp -> ์์ฒญ ->
<- ์๋ต
a ๋ฆฌ๋ค์ด๋ ํธ( redirect )๋งํฌ ํ๊ทธ ํด๋ฆญ -> ex05_redirect.jsp?name=admin&age=20 -> ์์ฒญ -> ๋ฆฌ๋ค์ด๋ ํธ( finish.jsp)
<- ์๋ต
ex05_finish.jsp -> ์์ฒญ ?name,age ํ๋ผ๋ฏธํฐ X
<- ์๋ต
-jsp-
<%
String name = "admin";
int age = 20;
%>
<!--
ex05_redirect.jsp?name=admin&age=20
ex05_forward.jsp?name=admin&age=20
-->
<a href="ex05_redirect.jsp">๋ฆฌ๋ค์ด๋ ํธ( redirect )</a><br>
<a href="ex05_forward.jsp">ํฌ์๋ฉ( forward ) </a><br>
<script>
$("a").on("click", function (){
$(this).attr("href", function (i, val){
return val +"?name=<%= name %>&age=<%= age %>";
});
});
</script>
-redirect.jsp-
<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!--
<h3>ex05_redirect.jsp?name=admin&age=20</h3>
-->
<%
// ์ธ์
, ์ฟ ํค ์ ์ฅ X
String name = request.getParameter("name");
String age = request.getParameter("age");
String location = String.format("ex05_finish.jsp?name=%s&age=%s"
, URLEncoder.encode(name, "UTF-8") , age);
response.sendRedirect(location);
// 1. ์ด์ ? finish.jsp name, age ํ๋ผ๋ฏธํฐ ์ ์ถ๋ ฅ.
// 2. ํด๊ฒฐ๋ฐฉ๋ฒ ? name, age ํ๋ผ๋ฏธํฐ ์ถ๋ ฅ ์ฒ๋ฆฌ.
%>
-forward.jsp-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!--
<h3>ex05_forward.jsp?name=admin&age=20</h3>
-->
<%
// ํฌ์๋ฉ( forward )
String path = "ex05_finish.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path) ;
dispatcher.forward(request, response);
%>
-finish.jsp-
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 6. ์ค์ 11:19:45</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex05_finish.jsp</h3>
<%
String name = request.getParameter("name");
%>
> name = <%= URLDecoder.decode(name, "UTF-8") %><br>
> age = ${ param.age }<br>
<script>
</script>
</body>
</html>
* ๋ฆฌ๋ค์ด๋ ํธ์ ๊ฒฝ์ฐ ์๋ ํ์ด์ง๋ก ์๋ตํ ํ ๋ค์ ์์ฒญ์ ๋ ๋ฆฌ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ location๊ฐ์ ํ๋ผ๋ฏธํฐ ๋ฃ์ด์ค์ผํจ!
* ํฌ์๋ฉ์ ๊ฒฝ์ฐ ์๋ ํ์ด์ง๋ก ๋์๊ฐ์ง ์๊ณ ๋ฐ๋ก ํ์ด์ง ์ด๋
1. ์๋ธ๋ฆฟ ์ฌ์ฉ
2. a ๋งํฌ( http://localhost/jspPro/scott/dept )์์ฒญ -> ์๋ธ๋ฆฟ ๊ฐ์ฒด ์์ฒญ( ScottDept.java )
[ get ๋ฐฉ์ ] doGet(){} ๊ตฌํ
post ๋ฐฉ์ doPost(){} X
// g + p service(){}
3. doGet(){
// 1.๋ถ์ ์ ๋ณด select ์ฝ๋ฉ
// 2.ArrayList<DeptDTO> list => request ๊ฐ์ฒด ์ ์ฅ
// 3. ํฌ์๋ฉ ex06_dept.jsp ( request [list]) ***
}
[๊ธฐ์ต]
ํ๋ก์ ํธ ํ ๋ ๊ฒฝ๋ก ์ค์ ํ ๋ ์๋๊ฒฝ๋ก X -> /jspPro/~~ context path(root) ๊ผญ ์ค์ .
** ์๋ธ๋ฆฟ url-pattern ์ค์ ์ฒดํฌ
-jsp-
<a href="<%= contextPath %>/scott/dept">scott.dept(๋ถ์ ์ ๋ณด)</a>
-scottdept.java- (servlet)
package days03;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
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.util.DBConn;
import domain.DeptDTO;
//@WebServlet("/scott/dept")
public class ScottDept extends HttpServlet {
private static final long serialVersionUID = 1L;
public ScottDept() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
// System.out.println("ScottDept.doGet() ํธ์ถ๋จ.");
// 1. ArrayList<DeptDTO> list -> request ์ ์ฅ
String sql = "SELECT deptno, dname, loc "
+" FROM dept";
Connection conn = DBConn.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<DeptDTO> list = null;
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if( rs.next() ){
list = new ArrayList<DeptDTO>();
do{
DeptDTO dto = new DeptDTO(
rs.getInt("deptno")
, rs.getString("dname")
, rs.getString("loc"));
list.add(dto);
}while( rs.next() );
} // if
pstmt.close();
rs.close();
}catch(Exception e){
e.printStackTrace();
} finally{
DBConn.close();
}
request.setAttribute("list", list);
// 2. ํฌ์๋ฉ ex06_dept.jsp
// /scott/ex06_dept.jsp
// String path = "ex06_dept.jsp"; ํ์ผ๋ช
์๋๊ฒฝ๋ก
// http://localhost/jspPro[/scott/ex06_dept.jsp]
// /jspPro/scott/dept"
// /jspPro/scott/ex06_dept.jsp"
//String path = "/days03/ex06_dept.jsp"; // ์ดํด.
//String path = "/ex06_dept.jsp"; // ์ดํด.
//http://localhost/jspPro[/ex06_dept.jsp]
// String path = "days03/ex06_dept.jsp"; // ์ดํด.
//http://localhost/jspPro[/scott/days03/ex06_dept.jsp]
String path = "/days03/ex06_dept_jstl.jsp"; // ์ดํด.
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
scott.dept(๋ถ์ ์ ๋ณด) -> ์๋ธ๋ฆฟ O -> ex06_dept.jsp 404 X ํฌ์๋ฉ 404 |
-dept.jsp-
<%@page import="java.util.Iterator"%>
<%@page import="domain.DeptDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String contextPath = request.getContextPath();
%>
<%
// ํ๋ผ๋ฏธํฐ : request.getParameter(name)
// request.setAttribute("list", list);
ArrayList<DeptDTO> list = (ArrayList<DeptDTO>)request.getAttribute("list");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 6. ์คํ 12:12:23</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex06_dept.jsp</h3>
<p>.</p>
<p>.</p>
<select id="selDept" name="deptno">
<option>์ ํ...</option>
<%
if( list != null ){ // ๋ถ์๊ฐ ์กด์ฌํ๋ค.
Iterator<DeptDTO> ir = list.iterator();
while( ir.hasNext() ){
DeptDTO dto = ir.next();
%>
<option value="<%= dto.getDeptno() %>"><%= dto.getDname() %></option>
<%
} // while
}else{ // 1๊ฐ์ ๋ถ์๋ ์กด์ฌํ์ง ์๋๋ค.
%>
<option> 1๊ฐ์ ๋ถ์๋ ์กด์ฌํ์ง ์๋๋ค. </option>
<%
}
%>
</select>
<script>
$("#selDept")
.wrap("<form></form>")
.change( function (){
$(this)
.parent() // form
.attr({ // /jspPro
action: "<%= contextPath %>/scott/emp" ,
method: "get"
})
.submit();
});
</script>
<script>
</script>
</body>
</html>
1. select ํ๊ทธ์์ ํ๋์ ๋ถ์(option)๋ฅผ ์ ํ
-> ์๋ธ๋ฐ(submit)
http://localhost/jspPro[/scott/emp] GET ๋ฐฉ์ + ์ ํํ ๋ถ์๋ฒํธ(deptno = 20)
jquery ์ฌ์ฉ.
2. ์๋ธ๋ฆฟ(ScottEmp.java) ์์ฒญ - doGet() { ์ฝ๋ฉ}
3. ์๋ธ๋ฆฟ(ScottEmp.java) doGet(){
pdeptno = 20;
ArrayList<EmpDTO> list ;
request ์ ์ฅ
ํฌ์๋ฉ ex06_emp.jsp
4. ex06_emp.jsp
ํ ์ด๋ธ ArrayList<EmpDTO> list ;
์ถ๋ ฅ.
}
-scottEmp.java-
package days03;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList; // Ctrl + Shift + O
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.util.DBConn;
import domain.DeptDTO;
import domain.EmpDTO;
// @WebServlet("/scott/emp")
public class ScottEmp extends HttpServlet {
private static final long serialVersionUID = 1L;
public ScottEmp() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("ScottEmp.doGet() ํธ์ถ๋จ.");
int pdeptno = 10;
try{
pdeptno = Integer.parseInt( request.getParameter("deptno") );
}catch(NumberFormatException e){ }
//
Connection conn = DBConn.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
// 1. ๋ถ์ ์ ๋ณด X
// 2. ํด๋น ๋ถ์์ ์ ๋ณด
ArrayList<EmpDTO> empList = null;
String sql = "SELECT * "
+" FROM emp "
+" WHERE deptno = ? "
+" ORDER BY ename ASC";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pdeptno );
rs = pstmt.executeQuery();
if( rs.next() ){
empList = new ArrayList<>();
do{
// empno, ename, job, hiredate, mgr, sal, comm, deptno
EmpDTO dto = new EmpDTO(
rs.getInt("empno")
, rs.getString("ename")
, rs.getString("job")
, rs.getDate("hiredate")
, rs.getInt("mgr")
, rs.getDouble("sal")
, rs.getDouble("comm")
, rs.getInt("deptno")
);
empList.add(dto);
}while( rs.next() );
} // if
pstmt.close();
rs.close();
}catch(Exception e){
e.printStackTrace();
} // try
DBConn.close();
// 1. request ์ ์ฅ
request.setAttribute("empList", empList);
// 2. ํฌ์๋ฉ
//String path = "/days03/ex06_emp.jsp";
String path = "/days03/ex06_emp_jstl.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
-emp.jsp-
<%@page import="java.util.Iterator"%>
<%@page import="domain.EmpDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String contextPath = request.getContextPath();
%>
<%
// request.setAttribute("empList", empList);
ArrayList<EmpDTO> empList = (ArrayList<EmpDTO>)request.getAttribute("empList");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 6. ์คํ 2:47:59</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table{
width:100%;
min-width: 700px;
}
table, th, td{
border:1px solid gray;
}
</style>
</head>
<body>
<h3>ex06_emp.jsp</h3>
<br>
<br>
<table>
<thead>
<tr>
<th><input type="checkbox" id="ckbAll" name="ckbAll">์ ์ฒด ์ ํ</th>
<th>empno</th>
<th>ename</th>
<th>job</th>
<th>hiredate</th>
<th>mgr</th>
<th>sal</th>
<th>comm</th>
<th>deptno</th>
</tr>
</thead>
<tbody>
<%
if( empList != null ){ // ์ฌ์ ์กด์ฌ O
Iterator<EmpDTO> ir2 = empList.iterator();
while( ir2.hasNext() ){
EmpDTO dto = ir2.next();
%>
<tr>
<!-- <td><input type="checkbox" name="ckbEmp" data-์์ฑ๋ช
="์์ฑ๊ฐ" data-์์ฑ๋ช
="์์ฑ๊ฐ"></td> -->
<td><input type="checkbox" name="ckbEmp" data-empno="<%= dto.getEmpno() %>"></td>
<td><%= dto.getEmpno() %></td>
<td><%= dto.getEname() %></td>
<td><%= dto.getJob() %></td>
<td><%= dto.getHiredate() %></td>
<td><%= dto.getMgr() %></td>
<td><%= dto.getSal() %></td>
<td><%= dto.getComm() %></td>
<td><%= dto.getDeptno() %></td>
</tr>
<%
} // while
}else{ // ์ฌ์ ์กด์ฌ X
%>
<tr>
<td colspan="9" style="text-align: center">employee does not exist.</td>
</tr>
<%
} // if
%>
</tbody>
<tfoot>
<tr>
<td colspan="9" style="text-align: center">
<button id="checkedEmpno">์ ํํ empno ํ์ธ</button>
</td>
</tr>
</tfoot>
</table>
<a href="<%= contextPath %>/scott/dept">๋ค์ํ๊ธฐ</a>
[ JSTL ] p 292 Chapter 12 ํ์ค ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (JSTL)
1. ex06_dept.jsp / ex06_emp.jsp ์ฝ๋ฉ ์์
ex06_dept_jstl.jsp / ex06_emp_jstl.jsp ๋ณต์ฌ , ๋ถ์ด๊ธฐ.
2. JSTL
- ์ปค์คํ ํ๊ทธ
jsp:~~~> ์ก์ ํ๊ทธ์ ๊ฐ์ด
๊ฐ๋ฐ์ ํ์์ ์ํด์ ํ๊ทธ ์ ์ํด์ ์ฌ์ฉํ๋ค.
- ์ฌ๋ฌ ๊ฐ๋ฐ์๊ฐ ๊ณตํต์ ์ผ๋ก ์ปค์คํ ํ๊ทธ๋ฅผ ๋ง๋ค๊ธฐ์ ์ด๊ฒ์ ํ์คํ์ํจ ํ๊ทธ๊ฐ
JSP Standard Tag Library( JSTL)
- ๋ณ์ ์ ์ธ + ํ๊ทธ, ํจ์ , ์ ์ด๋ฌธ ํ๊ทธ, URL ์ฒ๋ฆฌ + ํ๊ทธ, ์ถ๋ ฅ ํ๊ทธ ๋ฑ๋ฑ
- p 294 ํ 12.1 JSTL ํ๊ทธ์ 5๊ฐ์ง ์ข ๋ฅ
*** 1) ์ฝ์ด : ๋ณ์, ์ ์ด๋ฌธ, URL ์ฒ๋ฆฌ ํ๊ทธ : ์ ๋์ด( c ) : http://java.sun.com/jsp/jstl/core
2) XML
3) ๊ตญ์ ํ
4) ๋ฐ์ดํฐ๋ฒ ์ด์ค
5) ํจ์
- JSTL ์ฌ์ฉํ๋ ค๋ฉด jstl-1.2.jar ๋ค์ด๋ก๋
JAVA+ JSP + Spring ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
web-inf ํด๋์ ์ ๋งํฌ์์ jstl 1.2๋ค์ด (usages ๋ง์๊ฑฐ), ojdbc๋ ๋ฐ์์ ์ฌ๊ธฐ ๋ฃ์ด์ผํจ!
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
dept.jstl
<%@page import="java.util.Iterator"%>
<%@page import="domain.DeptDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String contextPath = request.getContextPath();
%>
<%
// request.setAttribute("list ์์ฑ๋ช
", list);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 6. ์คํ 12:12:23</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex06_dept_jstl.jsp</h3>
<p>.</p>
<p>.</p>
<select id="selDept" name="deptno">
<option>์ ํ...</option>
<c:if test="${ not empty list }">
<c:forEach items="${ list }" var="dto">
<option value="${ dto.deptno }">${ dto.dname }</option>
</c:forEach>
</c:if>
<c:if test="${ empty list }">
<option> 1๊ฐ์ ๋ถ์๋ ์กด์ฌํ์ง ์๋๋ค. </option>
</c:if>
</select>
<script>
$("#selDept")
.wrap("<form></form>")
.change( function (){
$(this)
.parent() // form
.attr({ // /jspPro
action: "<%= contextPath %>/scott/emp" ,
method: "get"
})
.submit();
});
</script>
emp.jstl
<%@page import="java.util.Iterator"%>
<%@page import="domain.EmpDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String contextPath = request.getContextPath(); // EL
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 6. ์คํ 2:47:59</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table{
width:100%;
min-width: 700px;
}
table, th, td{
border:1px solid gray;
}
</style>
</head>
<body>
<h3>ex06_emp_jstl.jsp</h3>
<br>
<br>
<table>
<thead>
<tr>
<th><input type="checkbox" id="ckbAll" name="ckbAll">์ ์ฒด ์ ํ</th>
<th>empno</th>
<th>ename</th>
<th>job</th>
<th>hiredate</th>
<th>mgr</th>
<th>sal</th>
<th>comm</th>
<th>deptno</th>
</tr>
</thead>
<tbody>
<c:if test="${ not empty empList }">
<c:forEach items="${ empList }" var="dto">
<tr>
<!-- <td><input type="checkbox" name="ckbEmp" data-์์ฑ๋ช
="์์ฑ๊ฐ" data-์์ฑ๋ช
="์์ฑ๊ฐ"></td> -->
<td><input type="checkbox" name="ckbEmp" data-empno="${ dto.empno }"></td>
<td>${ dto.empno }</td>
<td>${ dto.ename }</td>
<td>${ dto.job }</td>
<td>${ dto.hiredate }</td>
<td>${ dto.mgr }</td>
<td>${ dto.sal }</td>
<!-- .PropertyNotFoundException: [com] ํน์ฑ์ด [domain.EmpDTO] ์ ํ -->
<td>${ dto.comm }</td>
<td>${ dto.deptno }</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${ empty empList }">
<tr>
<td colspan="9" style="text-align: center">employee does not exist.</td>
</tr>
</c:if>
</tbody>
<tfoot>
<tr>
<td colspan="9" style="text-align: center">
<button id="checkedEmpno">์ ํํ empno ํ์ธ</button>
</td>
</tr>
</tfoot>
</table>
<a href="<%= contextPath %>/scott/dept">๋ค์ํ๊ธฐ</a>
ex07.jsp ํ์ด์ง์์
form get -> Member.java ( doGet(){})
form post -> Member.java( doPost(){} )
*๊ฐ์ ๋ฐ์ดํฐ ๋ ๋ ธ์ ๋ Member.java๋ผ๋ ์๋ธ๋ฆฟ ๋ง๋๋๋ฐ ๊ฐ๊ฐ ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌ
-jsp-
<%
String contextPath = request.getContextPath();
%>
<form action="<%= contextPath %>/member/get" method="get">
name : <input type="text" name="name" value="ํ๊ธธ๋"><br>
age : <input type="text" name="age" value="20"><br>
<input type="submit" value="GET ์ ์ก">
</form>
<form action="<%= contextPath %>/member/post" method="post">
name : <input type="text" name="name" value="ํ๊ธธ๋"><br>
age : <input type="text" name="age" value="20"><br>
<input type="button" value="POST ์ ์ก">
</form>
<script>
$("form:last-of-type :button").on("click", function (){
// ์
๋ ฅ๊ฐ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ํ ํ
// $(this).parent();
$(this).parents("form").submit();
});
</script>
-Member.java-
package days03;
import java.io.IOException;
import java.net.URLEncoder;
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;
// ํ๋์ ์๋ธ๋ฆฟ Member ๊ฐ์ง๊ณ
// doGet() ๋ฐ๋ก ์ฒ๋ฆฌ
// doPost() ๋ฐ๋ก ์ฒ๋ฆฌ..
@WebServlet({ "/member/get", "/member/post" })
public class Member extends HttpServlet {
private static final long serialVersionUID = 1L;
public Member() {
super();
}
// /member/get + get
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
// ๋ฆฌ๋ค์ด๋ ํธ
// (์ฃผ์) ํ๊ธ ์ธ๊ฒฝ์ฐ ์ธ์ฝ๋ฉ.
String location = String.format(
"/jspPro/days03/ex07_ok.jsp?name=%s&age=%s"
, URLEncoder.encode( name , "UTF-8" )
, age );
response.sendRedirect(location);
}
// /member/post + post
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
System.out.println( name );
// ํฌ์๋ฉ.
String path = "/days03/ex07_ok.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
}
* ๋ฆฌ๋ค์ด๋ ํธ์ ๊ฒฝ์ฐ /jspPro ๋ถํฐ ์์ํด์ผํจ!
-ok.jsp-
<%
String name = request.getParameter("name");
%>
> Member ์๋ธ๋ฆฟ : name = <%= name %><br>
> Member ์๋ธ๋ฆฟ : age = ${ param.age }<br>
<br><br>
<a href="ex07.jsp">๋ค์ํ๊ธฐ</a>
๊ฒฐ๊ณผ)
- get ๋ฆฌ๋ค์ด๋ ํธ์ ๊ฒฝ์ฐ ํ๋ผ๋ฏธํฐ ๋์ด๊ฐ์ง ์์์ URL์ ๋ฃ์ด์ฃผ๋ ๋ฐฉ์์ผ๋ก
- get์ ๋ค์ํ๊ธฐ ๊ฐ๋ฅ -> url์ด ์ ์กํ
- post๋ ํ๊ธ ๊นจ์ง๊ณ
- post ๋ค์ํ๊ธฐ ๋๋ฅด๋ฉด 404์๋ฌ -> url ์ด member๋ก ์กํ
ex08.jsp -> ex08_02.jsp -> ex08_03.jsp
์ด๋ฆ ์ฃผ์
๋์ด ์ฐ๋ฝ์ฒ
์๋ธ๋ฐ ์๋ธ๋ฐ
-1.jsp-
<form action="ex08_02.jsp" method="get">
name : <input type="text" name="name" value="ํ๊ธธ๋"><br>
age : <input type="text" name="age" value="20"><br>
<input type="submit" value="Next">
</form>
-2.jsp-
<form action="ex08_03.jsp" method="get">
address : <input type="text" name="address" value="์์ธ ์ญ์ผ๋"><br>
tel : <input type="text" name="tel" value="010-1234-1234"><br>
<input type="button" value="Prev" onclick="history.back()">
<input type="submit" value="Next">
</form>
-3.jsp-
์ต๊ทผ๋๊ธ