Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- jsp연결
- NCS
- 참조타입
- 자바
- w3school
- 데이터베이스
- 정보처리기사
- php
- jsp
- ospf
- VLAN
- html
- 정처기필기
- 네트워크
- rinux
- Cisco
- sql
- 버추얼머신
- autoset
- Java
- cisco packet
- Oracle
- 원형그래프
- 이것이 자바다
- 리눅스
- javaee
- 오라클
- 네트워크관리사
- 라우터
- ciscopacket
Archives
- Today
- Total
기록해! 정리해!
Model 2 : DispatcherServlet 만들기 본문
[ web.xml ]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://Java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.springbook.view.controller.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
=====================================================================
package com.springbook.view.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DispatcherServlet
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DispatcherServlet() {
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.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("UTF-8");
process(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);
request.setCharacterEncoding("UTF-8");
process(request,response );
}
private void process(HttpServletRequest request, HttpServletResponse response) {
String uri=request.getRequestURI(); // 결과 : /BoardWeb/delete.do
String path=uri.substring(uri.lastIndexOf("/")); // 결과 : /delete.do
System.out.println("경로확인:" +path);
if (path.equals("/login.do")) {
System.out.println("로그인 처리");
} else if (path.equals("/logout.do")) {
System.out.println("로그아웃 처리");
} else if (path.equals("/insertBoard.do")) {
System.out.println("글 등록 처리");
} else if (path.equals("/updateBoard.do")) {
System.out.println("글 수정 처리");
} else if (path.equals("/deleteBoard.do")) {
System.out.println("글 삭제 처리");
} else if (path.equals("/getBoard.do")) {
System.out.println("글 상세 조회 처리");
} else if (path.equals("/getBoardList.do")) {
System.out.println("글 목록 검색 처리");
} else if (path.equals("/index.do")) {
System.out.println("시작페이지 이동처리");
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
process(request,response );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
process(request,response );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
String uri=request.getRequestURI(); // 결과 : /BoardWeb/delete.do
String path=uri.substring(uri.lastIndexOf("/")); // 결과 : /delete.do
System.out.println("경로확인:" +path);
HttpSession session = request.getSession();
if (path.equals("/login.do")) {
System.out.println("로그인 처리");
String id=request.getParameter("id");
String password=request.getParameter("password");
UserVo vo = new UserVo();
vo.setId(id);
vo.setPassword(password);
UserDao3 service = new UserDao3();
UserVo user = service.getUser(vo);
if(user != null){
response.sendRedirect("getBoardList.do");
}else{
response.sendRedirect("login.jsp");
}
} else if (path.equals("/logout.do")) {
System.out.println("로그아웃 처리");
session.invalidate();
response.sendRedirect("login.jsp");
} else if (path.equals("/insertBoard.do")) {
System.out.println("글 등록 처리");
// 1. 사용자 입력 정보 추출
request.setCharacterEncoding("UTF-8");
String title=request.getParameter("title");
String writer=request.getParameter("writer");
String content=request.getParameter("content");
// 2. DB연동 처리
BoardVo vo = new BoardVo();
vo.setTitle(title);
vo.setWriter(writer);
vo.setContent(content);
BoardDao dao = new BoardDaoImpl();
dao.insert(vo);
// 3.
response.sendRedirect("getBoardList.do");
} else if (path.equals("/updateBoard.do")) {
System.out.println("글 수정 처리");
// 1. 사용자 입력 정보 추출
request.setCharacterEncoding("UTF-8");
int seq= Integer.parseInt(request.getParameter("seq")) ;
String title=request.getParameter("title");
String content=request.getParameter("content");
// 2. DB연동 처리
BoardVo vo = new BoardVo();
vo.setSeq(seq);
vo.setTitle(title);
vo.setContent(content);
BoardDao dao = new BoardDaoImpl();
dao.update(vo);
// 3.
response.sendRedirect("getBoardList.do");
} else if (path.equals("/deleteBoard.do")) {
System.out.println("글 삭제 처리");
request.setCharacterEncoding("UTF-8");
int seq= Integer.parseInt(request.getParameter("seq")) ;
BoardDao dao = new BoardDaoImpl();
dao.delete(seq);
response.sendRedirect("getBoardList.do");
} else if (path.equals("/getBoard.do")) {
System.out.println("글 상세 조회 처리");
int seq= Integer.parseInt(request.getParameter("seq")) ;
BoardDao dao = new BoardDaoImpl();
BoardVo m = dao.getBoard(seq);
session.setAttribute("m", m);
response.sendRedirect("getBoard.jsp");
} else if (path.equals("/getBoardList.do")) {
System.out.println("글 목록 검색 처리");
String searchCondition=request.getParameter("searchCondition");
String searchKeyword=request.getParameter("searchKeyword");
BoardVo vo = new BoardVo();
vo.setSearchCondition(searchCondition);
vo.setSearchKeyword(searchKeyword);
BoardDao dao = new BoardDaoImpl();
List<BoardVo> li = dao.getBoardList(vo);
session.setAttribute("li", li);
response.sendRedirect("getBoardList.jsp");
} else if (path.equals("/index.do")) {
System.out.println("시작페이지 이동처리");
response.sendRedirect("index.jsp");
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.springbook.biz.board.impl.*" %>
<%@ page import="com.springbook.biz.board.*" %>
<%
BoardVo m = (BoardVo) session.getAttribute("m");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br>
<h1> 글 상세 </h1>
<a href=logout.do> Log-out </a>
<hr />
<form action=updateBoard.do method=get>
<input type="hidden" name="seq" value="<%=m.getSeq() %>" >
<table border=1>
<tr><td>제목 </td><td><input type="text" name="title" value="<%=m.getTitle() %>" > </td></tr>
<tr><td>작성자</td> <td> <%=m.getWriter() %> </td></tr>
<tr><td>내용 </td> <td><textarea name=content cols=40 rows=10><%=m.getContent() %></textarea> </td></tr>
<tr><td>등록일 </td><td><%=m.getRegDate() %> </td></tr>
<tr><td>조회수 </td><td><%=m.getCnt() %> </td></tr>
<tr><td colspan=2 align="center">
<input type="submit" value="글수정" >
</td></tr>
</table>
</form>
<hr />
<a href=insertBoard.jsp>글등록</a>  
<a href=deleteBoard.do?seq=<%=m.getSeq() %> >글삭제</a>  
<a href=getBoardList.do>글목록</a>  
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.springbook.biz.board.impl.*" %>
<%@ page import="com.springbook.biz.board.*" %>
<%@ page import="java.util.*" %>
<%
List<BoardVo> li =( List<BoardVo>) session.getAttribute("li");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br>
<h1> 글목록 </h1>
<h3> 테스트님 환영합니다. <a href=logout.do>Log-out</a> </h3>
<!-- 검색 시작 -->
<table border=1 width=700>
<tr><td align="right">
<form action=getBoardList.do >
<select name="searchCondition">
<option value="title"> 제목 </option>
<option value="content"> 내용 </option>
</select>
<input type=text name="searchKeyword" >
<input type=submit value="검색하기" >
</form>
</td></tr>
</table>
<table border=1 width=700>
<tr>
<th> 번호 </th> <th> 제목 </th> <th> 작성자 </th>
<th> 등록일 </th> <th> 조회수 </th>
</tr>
<% for(BoardVo m : li) {%>
<tr>
<td> <%=m.getSeq() %> </td>
<td> <a href=getBoard.do?seq=<%=m.getSeq() %>>
<%=m.getTitle() %>
</a>
</td>
<td> <%=m.getWriter() %> </td>
<td> <%=m.getRegDate() %> </td> <td> <%=m.getCnt() %> </td>
</tr>
<% } %>
</table>
<br>
<a href=insertBoard.jsp> 새글등록 </a>
</div>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!doctype html>
<html>
<head>
<meta charset="UTF-8"> <title>DB 연동</title>
</head>
<body>
<div align="center">
<br><br><br>
<h2> Model - II 아키텍처로 게시판 개발 </h2>
<br><br><br>
<a href=getBoardList.do>게시판 목록보기</a><br><br>
<a href=login.jsp>로그인</a><br><br>
</div>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------
<%@ 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 align="center">
<br><br>
<h1> 글 등록 </h1>
<a href=logout.do> Log-out </a>
<hr />
<form action=insertBoard.do method=get>
<table border=1>
<tr><td>제목 </td><td><input type="text" name="title" > </td></tr>
<tr><td>작성자</td> <td><input type="text" name="writer" > </td></tr>
<tr><td>내용 </td> <td><textarea name=content cols=40 rows=10></textarea> </td></tr>
<tr><td colspan=2 align="center">
<input type="submit" value="새글등록" >
</td></tr>
</table>
</form>
<hr />
<a href=getBoardList.jsp>글목록</a>  
</div>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------
<%@ 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 align="center">
<br><br>
<h1> 로그인 </h1>
<hr />
<form action=login.do method=get>
<table border=1>
<tr><td>아이디 </td> <td><input type="text" name="id" value="TEST" > </td></tr>
<tr><td>비밀번호 </td> <td><input type="text" name="password" value="TEST123" > </td></tr>
<tr><td colspan=2 align="center">
<input type="submit" value="로그인" >
</td></tr>
</table>
</form>
<hr />
</div>
</body>
</html>
'Spring' 카테고리의 다른 글
EL / JSTL 사용하기 (0) | 2022.09.23 |
---|---|
MVC 프레임워크 구현하기 (0) | 2022.09.23 |
Model 1 아키텍처로 게시판 개발 (0) | 2022.09.23 |
트랜잭션 처리 (0) | 2022.09.22 |
Spring JDBC 방법 2 (0) | 2022.09.22 |
Comments