javax.servlet.Servlet 인터페이스
<- javax.servlet.http.HttpServlet
c.f. javax.servlet.SingleTheadModel 인터페이스 : ~2.3
.doGet()
.doPost()
HttpServletRequest
.setCharacterEncoding()
.setAttribute()
.getRequestDispatcher()
RequestDispatcher
.forward()
request.setAttribute()
request.getAttribute() c.f. request.getParameter()
HttpServletResponse
.setContentType()
.getWriter()
/*
// Hundred.jsp
${result}
*/
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HundredServlet2
*/
public class HundredServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HundredServlet2() {
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
int total = 0;
for (int cnt=1; cnt <= 100; cnt++)
total += cnt;
request.setAttribute("result", total);
RequestDispatcher rd = request.getRequestDispatcher("Hundred.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
}
}
import java.io.*;
//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 HundredServlet
*/
public class HundredServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HundredServlet() {
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
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Sum of 1 to 100</TITLE></HEAD>");
out.println("<BODY>");
int total = 0;
for (int cnt=1; cnt <= 100; cnt++)
total += cnt;
out.println("1 + 2 + 3 + ... + 10 = " + total);
out.println("</BODY>");
out.println("</HTML>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
out.println("..");
..
}
/*
c.f.
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("euc-kr");
res.setContentType("text/html;charset=euc-kr");
..
try {
..
} catch (Exception e) {
throw new ServletException(e);
}
req.setAttribute("..", ..);
req.getRequestDispatcher("...").forward(req, res);
}
*/
}
참조 사이트:
http://jjjryu.tistory.com/entry/%ED%95%84%ED%84%B0-WAS