[Áú¹®] ¼­ºí¸´¿¡¼­ doGet°ú doPostÀ» Çϴµ¥..


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

±Û¾´ÀÌ :À̱ݿì 1999³â 10¿ù 08ÀÏ 01:42:32

Áú¹®ÀÔ´Ï´Ù.


°£´ÜÇÑ ÃÊ°£´ÜÇÑ ¼îÇθô ºñ½º¹Ç¸® Àä..


¼Ò½º´Â..¾Æ·¡¿¡ ÀÖ±¸¿ä..


¹®Á¦´Â ¸¸¾à ¹öÆ°(submit)À» ´©¸£¸é ¿øÇÏ´Â °á°ú°¡ ³ª¿ÀÁö ¾Ê°í ¼­ºí¸´Å¬·¡½ºÀ̸§¿¡ ÀÌ»óÇÑ ±Û¾¾°¡ µ¡ºÙ¿©Á®¼­ ´Ù¿îÀ» ÇÏ·Á ÇÕ´Ï´Ù. ¾î¶»°Ô ÇØ¾ß ÇÒ±î¿ä?..


¹®Á¦Á¡ÀÌ ¹¹ÁÒ?..


¾È³ç°Ô¼¼¿ä..


--±×¹«--



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ShopServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html;charset=euc-kr");
PrintWriter out = new PrintWriter(new OutputStreamWriter(
res.getOutputStream(), "KSC5601"));

out.println("<html><body bgcolor=\"yellow\">");
out.println("<p>°¡»ó¼îÇθô</p>");
out.println("<form name=\"form\" method=\"post\"action=\"/servlet/ShopServlet\">");
out.println("<p><input type=\"submit\" name=\"tomato\" value=\"¼îÇι鿡 Å丶Åä ³Ö±â\">");
out.println("<p><input type=\"submit\" name=\"apple\" value=\"¼îÇι鿡 »ç°ú ³Ö±â\">");
out.println("<p><input type=\"submit\" name=\"see\" value=\"¼îÇÎ¹é º¸±â\">");
out.println("<p><input type=\"submit\" name=\"buy\" value=\"»óÇ°ÁÖ¹®\">");
out.println("</form></body></html>");
out.close();
}

protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String msg;

HttpSession session = req.getSession(true);

if(session.isNew()) {
session.putValue("apple", new int[] {0});
session.putValue("tomato", new int[] {0});
}

int[] apple = (int[])session.getValue("apple");
int[] tomato = (int[])session.getValue("tomato");

if(req.getParameter("apple") != null) {
apple[0]++;
msg = "»ç°ú¸¦ ¼îÇι鿡 Ãß°¡ÇÏ¿´½À´Ï´Ù. ´ç½ÅÀº »ç°ú¸¦ "
+ apple[0] + "¸¦ °¡Áö°í ÀÖ½À´Ï´Ù.";
}
else if(req.getParameter("tomato") != null) {
tomato[0]++;
msg = "Å丶Å並 ¼îÇι鿡 Ãß°¡ÇÏ¿´½À´Ï´Ù. ´ç½ÅÀº Å丶Å並 "
+ tomato[0] + "¸¦ °¡Áö°í ÀÖ½À´Ï´Ù.";
}
else if(req.getParameter("buy") != null) {
session.invalidate();
msg = "´ç½ÃÀº »ç°ú¸¦" + apple[0] + "°³¿Í Å丶Åä " + tomato[0]
+ "°³°¡ ÁÖ¹® ÇÏ¿´½À´Ï´Ù. ¼îÇιéÀÇ »óÇ°Àº »èÁ¦µÇ¾ú½À´Ï´Ù.";
}
else {
msg = "´ç½ÅÀº »ç°ú" + apple[0] + "°³¿Í Å丶Åä" + tomato[0]
+ "¸¦ ¼îÇι鿡 Ãß°¡ ÇÏ¿´½À´Ï´Ù.";
}

res.setContentType("text/html ; charset=euc-kr");
res.setHeader("pragma", "no-cache");
PrintWriter out = res.getWriter();
out.print("<html><head><title>Shopping Cart</title></head>");
out.print(msg);
out.print("<hr><a href=\"");
out.print(req.getRequestURI());
out.print("\">µÚ·Î°¡±â</a></body></html>");
out.close();
}
public String getServletInfo() {
return "ShopServlet";
}
}


´ÙÀ½ ±Ûµé:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
HTML ÅÂ±× Æ÷ÇÔ ¿©ºÎ: HTML ¹®¼­ÀÏ °æ¿ì üũ
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]