개발속이야기/Java

JSP ajax 테스트 예제

스토리지기 2018. 1. 8. 09:32

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:set value="${pageContext.request.contextPath}" var = "cp" />

<!DOCTYPE html">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script src="jquery.js"></script>

<!--  <script src="https://code.jquery.com/jquery-3.2.1.js"></script> -->

<script>

// ajax 테스트


$(document).ready(function(){

$.ajax({

type: "get",

//data:{"command", "select"},

dataType: "json",

url: '${cp}/GetBbs',

success: function(data){

//alert(data.length);

$.each(data, function(){

var trElement = $("table .header").clone().removeClass().empty(); //<tr></tr> 상태

$("table tbody").append(trElement.addClass("content"));

trElement.append("<td>" + this.bbsno + "</td>");

trElement.append("<td>" + this.title + "</td>");

trElement.append("<td>" + this.uploader + "</td>");

trElement.append("<td>" + this.readcount + "</td>");

trElement.append("<td>" + this.regdate + "</td>");

});

},

error: function(request, status){

alert(status);

}

});

});

</script>

<style>

table {

width: 500;

border: 1px solid gray;

font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;

font-size: 12px;

}

td {

border: 1px solid gray;

text-align: center;

}

tr.header * {

background-color: #666666;

color: white;

}

</style>

</head>

<body>

<div>

<table>

<thead>

<tr class="header">

<th>게시판번호</th><th>제목</th><th>올린이</th><th>조회수</th><th>등록일</th>

</tr>

</thead>

<tbody></tbody>

</table>

</div>

</body>

</html>





package com.oraclejava.servlet;


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 javax.servlet.http.HttpSession;


import com.google.gson.Gson;

import com.oraclejava.model.Bbs;

import com.oraclejava.model.BbsDao;


/**

 * Servlet implementation class ListServlet

 */

@WebServlet("/GetBbs")

public class GetBbsServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public GetBbsServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub


response.setContentType("application/json;charset=utf-8");

Gson gson = new Gson();

BbsDao dao = new BbsDao();

List<Bbs> list = dao.selectBbs();

try (PrintWriter out = response.getWriter()){

out.print(gson.toJson(list));

} catch (Exception e) {

// TODO: handle exception

System.out.println(e);

}

}


}




package com.oraclejava.model;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;


import util.DBUtil;


public class BbsDao {

public List<Bbs> selectBbs(){

List<Bbs> list = null;

String sql = "select * from bbs";

try(Connection connection = DBUtil.getDataSource().getConnection();

//Statement st = connection.createStatement();

PreparedStatement pstmt = connection.prepareStatement(sql);

){

// pstmt.setString(1, id);

// pstmt.setString(2, passwd);

ResultSet rs = pstmt.executeQuery();

list = new ArrayList<Bbs>();

Bbs bbs = null;

while(rs.next()) {

bbs = new Bbs();

bbs.setBbsno(rs.getInt("BBSNO"));

bbs.setTitle(rs.getString("TITLE"));

bbs.setUploader(rs.getString("UPLOADER"));

bbs.setContent(rs.getString("CONTENT"));

bbs.setReadcount(rs.getInt("READCOUNT"));

bbs.setRegdate(rs.getDate("REGDATE"));

list.add(bbs);

}

}

catch(Exception e){

e.printStackTrace();

}

return list;

}

}




package com.oraclejava.model;


import java.util.Date;


public class Bbs {

private int bbsno;

private String title;

private String uploader;

private String content;

private int readcount;

private Date regdate;

public int getBbsno() {

return bbsno;

}

public void setBbsno(int bbsno) {

this.bbsno = bbsno;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getUploader() {

return uploader;

}

public void setUploader(String uploader) {

this.uploader = uploader;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public int getReadcount() {

return readcount;

}

public void setReadcount(int readcount) {

this.readcount = readcount;

}

public Date getRegdate() {

return regdate;

}

public void setRegdate(Date regdate) {

this.regdate = regdate;

}

}




'개발속이야기 > Java' 카테고리의 다른 글

Spring 과제  (0) 2018.01.08
JSP file upload download 예제  (0) 2018.01.08
JSON Gson 예제  (0) 2018.01.05
JSP REF 예제  (0) 2018.01.04
JSP getFileName() 예제  (0) 2018.01.04