728x90
728x90
BIG
https://hyejin283.tistory.com/312
[JSP]JSP프로젝트 MVC2로 만들어보기 1(셋팅, 커넥션풀) Model
우선 새로운 프로젝트를 만든다 계속 넥스트를 누르고 web.xml을 만들어준다 데이터베이스연동 그다음에 커넥션풀 준비 web.xml Oracle Datasource example jdbc/javadbOracle javax.sql.DataSource Container 그 다음에 d
hyejin283.tistory.com
이 후 이어보겠다
view만들기
테이블을 하나 만들어보겠다
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book List</title>
</head>
<body>
<center>
<h2>Book List</h2>
<table border="1" width="80%" align="center">
<tr>
<th>책넘버</th>
<th>코드명</th>
<th>책제목</th>
<th>작 가</th>
<th>가 격</th>
</tr>
<tr>
<td style="width:10%">책넘버</td>
<td style="width:10%">코드명</td>
<td style="width:30%">책제목</td>
<td style="width:15%">작 가</td>
<td style="width:15%">가 격</td>
</tr>
</table>
</center>
</body>
</html>
컨트롤러 만들기(서블릿)
index.html 실행하면 도서목록을 읽을 수 있는 컨트롤러를 호출해줘야함(서블릿을 실행하도록 만들어줘야함)
서블릿 세개를 관장한다
넥스트를 누르면 맵핑을 해야되는데 /bList.do를 누군가 호출하면 북리스트서블릿이라는 것으로 맵핑시키겠다를 의미함
우선 겟방식으로 들어오니까 어노테이션가지고 맵핑을 당분간은 해보겠다. 나중에는 달리 할 수 도 있음(다른 방법이 있음) 여러방법이 있음
그 다음에 겟방식인 doGet에다가 입력해야됨
알단 한글깨짐을 방지하기 위해
setCharacterEncoding("utf-8"); 을 해줘야하는데
리퀘스트가 요청을 보낼때 쓰는거고
레스폰스가 요청에 대한 답변을 클라이언트에게 보내는걸 레스폰스라고함
그걸 캐릭터셋을 해주면 한글깨짐을 방지할 수 잇음
BIG
package controller;
import java.io.IOException;
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 book.dao.BookDAO;
import book.vo.BookVO;
/**
* Servlet implementation class bookListServlet
*/
@WebServlet("/bList.do")
public class bookListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public bookListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//index.html이 여기로 오는데
//이게 컨트롤러 역할을 함
// 어떤 요청이 들어오면 디비안에 데이터를 다 읽어서 저장해야됨
//그 요청처리를 해서 그결과를 북리스트.jsp로 반환해줘야함
//만약 한글이 들어온다고 하면 한글깨짐 처리를 해줘야함
//한글깨짐을 처리해주기위해 처리
request.setCharacterEncoding("utf-8");
//디에이오를 불러오기 위한 작업
BookDAO dao = BookDAO.getInstance();
//어레이리스트에 담아야하니까 어레이리스트를 받아주고
ArrayList<BookVO> list = dao.selectAll();
//그결과를 반환해줘야함
request.setAttribute("list", list);
//첫번째는 스트링 이름을 뜻하고, 어디에 담을꺼냐고 하는게 뒤에껀데(어레이리스트 북브이오를 뜻함)
//리퀘스트객체랑 레스폰스를 보내야되서 리퀘스트디스패쳐를 사용해야됨
RequestDispatcher rd = request.getRequestDispatcher("book/book_list.jsp");
//가야할 곳의 주소를 안에 넣어줘야한다(디스패쳐안에)
rd.forward(request, response);
//넘겨주기위해 포워드를 써야함
//샌드리다이렉트는 데이터가 안넘어가서 그냥 화면만 넘길때 사용
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
그다음에 잘 돌아가는 지 확인해본다
index에서 먼저 시작함
그 뒤 디비넣기
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="book.vo.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book List</title>
</head>
<body>
<center>
<h2>Book List</h2>
<table border="1" width="80%" align="center">
<tr>
<th>책넘버</th>
<th>코드명</th>
<th>책제목</th>
<th>작 가</th>
<th>가 격</th>
</tr>
<c:forEach var="vo" items="${list}">
<tr>
<td style="width: 10%"> ${vo.bno}</td>
<td style="width: 10%"> ${vo.code}</td>
<td style="width: 30%"> ${vo.title}</td>
<td style="width: 15%"> ${vo.writer}</td>
<td style="width: 15%; align: right">
<fmt:formatNumber
value="${vo.price}" type="number" /></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
근데 번호가 맘에 안들어서 바꿔보겠음
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="book.vo.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book List</title>
</head>
<body>
<center>
<h2>Book List</h2>
<table border="1" width="80%" align="center">
<tr>
<th>책넘버</th>
<th>코드명</th>
<th>책제목</th>
<th>작 가</th>
<th>가 격</th>
</tr>
<c:forEach var="vo" items="${list}" varStatus="st">
<tr>
<td style="width: 10%"> ${st.count }</td>
<td style="width: 10%"> ${vo.code}</td>
<td style="width: 30%"> ${vo.title}</td>
<td style="width: 15%"> ${vo.writer}</td>
<td style="width: 15%; align: right">
<fmt:formatNumber
value="${vo.price}" type="number" /></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
728x90
반응형
BIG
'🎵JSP' 카테고리의 다른 글
[JSP]한글깨짐현상 해결하기2 (0) | 2023.02.14 |
---|---|
[JSP]JSP프로젝트 MVC2로 만들어보기3 수정창만들기 (0) | 2023.02.14 |
[JSP]JSP프로젝트 MVC2로 만들어보기 1(셋팅, 커넥션풀) Model (0) | 2023.02.14 |
[JSP] JSTL core를 이용해서 로그인 페이지 만들기(c:when, c:choose) (0) | 2023.02.14 |
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 에러잡기(jsp생성직후에러잡기) (0) | 2023.02.14 |
댓글