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 |
Tags
- 웹소켓
- Spring
- docker
- model1
- tiles.xml
- websocket
- 웹게임
- SQL
- express
- 비트코인
- HTML
- PL/SQL
- 블록체인
- AWS
- Cookie
- 암호화
- EC2
- CSS
- JSP
- 알고리즘
- autowired
- 도커
- phaser
- RDS
- JavaScript
- node.js
- 배포
- Servlet
- Ajax
- jQuery
Archives
- Today
- Total
記錄
Servlet) 예제(단순 알림 or 객체 전달) 본문
<연산을 통해 알림을 받을 때>-> script로 alert전달
<form name="f" action="MemoServlet" method="post" id="f">
cf) action에서 매핑값을 설정
@WebServlet("/MemoServlet")
public class Memoservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
servlet 소스
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
}
private void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.한글처리
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//2.데이터 받기
String id = request.getParameter("id");
String email = request.getParameter("email");
String memo = request.getParameter("memo");
//3.DB연결
//4.Insert 실행
memodao dao = new memodao();
int n = dao.insertMemo(id, email, memo);
//5.응답구성
PrintWriter out = response.getWriter();
if(n>0) {
out.print("<script>");
out.print("alert('성공적으로 회원가입이 완료되었습니다');");
out.print("location.href = 'http://localhost:8090/WebServlet_4_memo_mvc/memo.html'");
out.print("</script>");
}else {
// int resultrow = 0
// 여기서 else문이 발동된다는 것은 dao 단에서 exception이 발생했다는 의미이다
out.print("<script>");
out.print("alert('회원 가입에 실패하였습니다');");
out.print("</script>");
}
}
<연산을 통해 객체를 받을 때> -> RequestDispatcher
예제 1)
<c:set var="memolist" value="${requestScope.memolist}"></c:set>
<div align=center>
<hr color=green width=400><h2> Line Memo List </h2><hr color=green width=400>
<table>
<tr>
<th>Writer</th>
<th>MemoContent</th>
<th>Email</th>
</tr>
<c:forEach var="memo" items="${memolist}">
<tr>
<td>${memo.id}</td> <-- ~.으로 접근하려면 getter구현 선행이 필수!!
<td>${memo.email}</td>
<td>${memo.content}</td>
</tr>
</c:forEach>
</table>
</div>
<a href='memo.html'>글쓰기</a>
</body>
</html>
EL에서 ~.으로 접근하려면 getter구현 선행이 필수!!
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
}
private void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// View page 별도 사용 : memolist.jsp
try {
// 전체 데이터 조회
memodao dao = new memodao();
ArrayList<memo> memolist = dao.getMemoList();
// 요청 결과 저장
request.setAttribute("memolist", memolist);
// View 페이지 설정
RequestDispatcher dis = request.getRequestDispatcher("/memolist.jsp");
// request와 response가 가진 주소를 포워드
dis.forward(request, response);
}catch (Exception e) {
e.printStackTrace();
}
}
}
예제 2)
public class Empdao {
Connection cn = null;
ResultSet rs = null;
public Empdao() {
cn = Singleton_Helper.getConnection("oracle");
}
public Emp emp_search(int empno){
Emp temp = new Emp();
String sql = "select * from emp where id = " + "'" + empno + "'";
try {
Statement stmt = cn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
temp = new Emp(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getInt(4),
rs.getString(5),
rs.getInt(6),
rs.getInt(7),
rs.getInt(8)
);
return temp;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Singleton_Helper.close(rs);
Singleton_Helper.close(cn);
}
return temp;
}
}
private void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
int empno = Integer.parseInt(request.getParameter("empno"));
Empdao dao = new Empdao();
Emp emp = dao.emp_search(empno);
RequestDispatcher dis = request.getRequestDispatcher("/memolist.jsp");
dis.forward(request, response);
}
}
'Web > Servlet' 카테고리의 다른 글
Servlet) "?cmd=~" vs @WebServlet("*.do") (0) | 2018.04.03 |
---|---|
Servlet) 커넥션 풀(DBCP) (0) | 2018.03.29 |
Servlet) out.print()에 대한 이해 (0) | 2018.03.29 |
Servlet) Life Cycle, @WebServlet 설정 (0) | 2018.03.29 |
Servlet) 키워드 받아서 경로 나눠주는 방식(cmd) (0) | 2018.03.28 |
Comments