Lprod 동기 / 비동기

LprodDao

package kr.or.ddit.basic.json;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import kr.or.ddit.basic.vo.LprodVO;
import kr.or.ddit.util.DBUtil3;

public class LprodDao {

	private static LprodDao dao;
	
	private LprodDao() {}
	
	public static LprodDao getInstance() {
		if(dao==null) dao = new LprodDao();
		return dao;
	}
	
	public List<LprodVO> getLprodList() {
		Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	
    	List<LprodVO> lprodList = new ArrayList<LprodVO>();
    	
    	try {
    		conn = DBUtil3.getConnection();
    		stmt = conn.createStatement();
    		String sql = "select * from lprod order by lprod_id ";		
    		rs = stmt.executeQuery(sql);
    		
    		while(rs.next()) {
    			LprodVO lvo = new LprodVO();
    			lvo.setLprod_id(rs.getInt("lprod_id"));
    			lvo.setLprod_gu(rs.getString("lprod_gu"));
    			lvo.setLprod_nm(rs.getString("lprod_nm"));
    			lprodList.add(lvo);
    		}
    		
		} catch (SQLException e) {
			lprodList = null;
			e.printStackTrace();
		}finally {
			if(rs!=null)try {rs.close();}catch(SQLException e) {}
			if(stmt!=null)try {stmt.close();}catch(SQLException e) {}
			if(conn!=null)try {conn.close();}catch(SQLException e) {}
		}
    	return lprodList;
	}
	
}

lprodlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>lprod 자료 목록</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-3.6.0.min.js"></script>
<script type="text/javascript">

$(function(){
	
	//-------------------------------------
	
	//비동기 방식(같은 페이지 안에서 변경됨)
	$("#lprodBtn").on("click", function(){
		$.ajax({
		    url: "<%=request.getContextPath()%>/lprodList.do",
		    //데이터를 안보내도되서 데이터는 생략됨
		    type : "post",
		    success : function(data){
		    	let htmlCode = "<table border='1'>"
		    	htmlCode += "<tr><th>LPROD_ID</th><th>LPROD_GU</th><th>LPROD_NM</th></tr>"
		    	
		    	//한줄씩 값 넣어줌
		    	$.each(data, function(i,v){
		    		htmlCode += "<tr><td>" + v.lprod_id + "</td>";
		    		htmlCode += "<td>" + v.lprod_gu + "</td>";
		    		htmlCode += "<td>" + v.lprod_nm + "</td></tr>";
		    	})
		    		
		    	htmlCode +="</table>"
		    	
		    	$("#result").html(htmlCode);
		    },
		    dataType : "json"
		});
		
	});
	//-------------------------------------------
	
	//동기 방식 처리(주소바뀜)
	$("#lprodBtn2").on("click", function(){
		location.href="<%=request.getContextPath()%>/lprodList2.do";
	});
	
});
</script>
</head>
<body>
  <form action="<%=request.getContextPath() %>/jsonDataTest02.do" 
        method="get">
    <input type ="button" id="lprodBtn" value="Lprod 자료 가져오기(ajax)">
    <input type ="button" id="lprodBtn2" value="Lprod 자료 가져오기(non 0ajax)">
  
 
  </form>
  <h2>Lprod 자료 목록</h2>
  <div id="result"></div>
</body>
</html>

LprodList.java

package kr.or.ddit.basic.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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.google.gson.Gson;

import kr.or.ddit.basic.vo.LprodVO;


@WebServlet("/lprodList.do")
public class LprodList extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		
		LprodDao dao = LprodDao.getInstance();
		
		List<LprodVO> lprodList = dao.getLprodList();
		
		String jsonData = gson.toJson(lprodList);
		
		out.write(jsonData);
		response.flushBuffer();
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

lprodList2.jsp

<%@page import="kr.or.ddit.basic.vo.LprodVO"%>
<%@page import="java.util.List"%>
<%@ 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>
  <%
     //서블릿이 보낸 데이터를 받는다.
     List<LprodVO> lprodList = 
         (List<LprodVO>)request.getAttribute("lpList");
  
  %>


 <h2>Lprod 자료 목록</h2>
 <table border="1">
  <tr>
  <th>LPROD_ID</th><th>LPROD_GU</th><th>LPROD_NM</th>
 </tr>
 
 <%
 for(LprodVO lvo : lprodList){
	 %>	 
 
 <tr>
     <td><%=lvo.getLprod_id() %></td>
     <td><%=lvo.getLprod_gu() %></td>
     <td><%=lvo.getLprod_nm() %></td>
 </tr>
 
 <%	 
 }
 %>
 
 </table>
</body>
</html>

LprodList2.java

package kr.or.ddit.basic.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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.google.gson.Gson;

import kr.or.ddit.basic.vo.LprodVO;


@WebServlet("/lprodList2.do")
public class LprodList2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
		
		LprodDao dao = LprodDao.getInstance();
		
		List<LprodVO> lprodList = dao.getLprodList();
		
		// 처리된 결과 데이터를 JSP문서로 forward방식으로 보낸다.
		
		request.setAttribute("lpList", lprodList);
		
		request.getRequestDispatcher("/json/lprodList2.jsp")
		.forward(request, response);
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

좋은 웹페이지 즐겨찾기