Servlet은 Controller(제어)의 역할을 담당하게 한다.
RequestDispatcher가 경우에 따라 jsp(present 구현)해주는 페이지로 넘겨준다.
(프레임 웤이 하는 일을 서블릿이 하도록 작성해 보자)
: 아직 프레임 웤을 사용하지 않기 때문
소스)
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
현재시간 : <%= new java.util.Date() %> <br></br>
<p>데이터를 입력하고 전송 버튼을 눌러 주세요</p>
<form action = "first.do" method = "get">
이름 : <input type = "text" name = "name"/><br/>
좋아하는 동물은 ?
<input type = "checkbox" name = "pet" value = "강아지">강아지</input>
<input type = "checkbox" name = "pet" value = "고양이">고양이</input>
<input type = "checkbox" name = "pet" value = "송아지">송아지</input>
<br/>
<input type = "submit" value = "전송"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
현재시간 : <%= new java.util.Date() %> <br></br>
<p>데이터를 입력하고 전송 버튼을 눌러 주세요</p>
<form action = "first.do" method = "get">
이름 : <input type = "text" name = "name"/><br/>
좋아하는 동물은 ?
<input type = "checkbox" name = "pet" value = "강아지">강아지</input>
<input type = "checkbox" name = "pet" value = "고양이">고양이</input>
<input type = "checkbox" name = "pet" value = "송아지">송아지</input>
<br/>
<input type = "submit" value = "전송"/>
</form>
</body>
</html>
FirstSetvlet.java
package simple.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int count;
public FirstServlet() {
super();
// TODO Auto-generated constructor stub
}
/*
* init()
* 서블릿을 시작할 때 호출한다.
* 시작할 때 무엇인가를 처리하고 싶다면..
* init()에 처리
*/
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
count = 100;
System.out.println("init() called... count = " + count);
//콘솔에 창 띄움.
}
/*
* destroy()
* 서버를 종료시키면 호출된다.
* 서블릿을 종료시킬 때 처리하고 싶은 것이 있다면...
* destory()에 처리
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy() called... count = " + count);
}
//새로 생성. doGet방식으로 들어오든, doPost방식으로 들어오든(요청)
//같은 방식으로 처리하겠다.
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//POST 방식으로 보냈을 때 한글로 처리하기 위함
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
if( name.equals("김영숙")){
RequestDispatcher dispatcher =
request.getRequestDispatcher("success.jsp");
dispatcher.forward(request, response);
return;
} else{
RequestDispatcher dispatcher =
request.getRequestDispatcher("failed.jsp");
dispatcher.forward(request, response);
return;
}
}
/*
* 서비스는 종류에 따라 불러짐.
* get - doget
* post - dopost
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
}
package simple.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int count;
public FirstServlet() {
super();
// TODO Auto-generated constructor stub
}
/*
* init()
* 서블릿을 시작할 때 호출한다.
* 시작할 때 무엇인가를 처리하고 싶다면..
* init()에 처리
*/
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
count = 100;
System.out.println("init() called... count = " + count);
//콘솔에 창 띄움.
}
/*
* destroy()
* 서버를 종료시키면 호출된다.
* 서블릿을 종료시킬 때 처리하고 싶은 것이 있다면...
* destory()에 처리
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy() called... count = " + count);
}
//새로 생성. doGet방식으로 들어오든, doPost방식으로 들어오든(요청)
//같은 방식으로 처리하겠다.
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//POST 방식으로 보냈을 때 한글로 처리하기 위함
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
if( name.equals("김영숙")){
RequestDispatcher dispatcher =
request.getRequestDispatcher("success.jsp");
dispatcher.forward(request, response);
return;
} else{
RequestDispatcher dispatcher =
request.getRequestDispatcher("failed.jsp");
dispatcher.forward(request, response);
return;
}
}
/*
* 서비스는 종류에 따라 불러짐.
* get - doget
* post - dopost
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
}
sucess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor = "green">
<b1><font color = "white" size = "6" > 환영합니다.</b1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor = "green">
<b1><font color = "white" size = "6" > 환영합니다.</b1>
</body>
</html>
failed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor = "red">
<font color = "white" size = "6" > 실패하셨습니다.</font>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor = "red">
<font color = "white" size = "6" > 실패하셨습니다.</font>
</body>
</html>
'Java > JSP' 카테고리의 다른 글
색상테이블 / 색상표 / 색상코드 (0) | 2011.01.25 |
---|---|
login 처리. / home 간단 sample 처리. (0) | 2011.01.25 |
JSP == Servlet (궁극적으로...) (0) | 2011.01.25 |
JSP 페이지 구성 요소 / 웹 요청 처리 SCOPE (0) | 2011.01.25 |
서블릿 (ⅰ) (0) | 2011.01.24 |