WEB-INF/web.xml
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.base.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
src/com.base/Hello.java
package com.base;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Hello extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try{
response.setContentType("text/html;charset=utf-8");
PrintWriter out= response.getWriter();
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
out.println("<html>");
out.println("<head><title>form&servlet</title></head>");
out.println("<body>");
out.println(name+"님 방문을 환영합니다.");
out.println("</body>");
out.println("</html>");
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
WebContent/greet.html
<!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>폼 과 서플릿</title>
</head>
<body>
<!-- /ServletMain/hello2 == http://localhost:8080/ServletMain/hello2 -->
<form method="post" action="/ServletMain/hello2">
당신의 이름은
<input type="text" name="name"><br/>
<input type="submit" value="전송"><br/>
</form>
</body>
</html>
web.xml
<servlet>
<servlet-name>TodayMenu</servlet-name>
<servlet-class>com.base.TodayMenu</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TodayMenu</servlet-name>
<url-pattern>/TodayMenu</url-pattern>
</servlet-mapping>
lunch.html
<!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>점심 선택</title>
</head>
<body>
<h3> 오늘점심은 무엇을 먹을까?(2개이상 선택)</h3>
<form method="post" action="/ServletMain/TodayMenu">
<input type="checkbox" name="lunch" value="떡볶기"> 떡볶기
<input type="checkbox" name="lunch" value="버섯덮밥"> 버섯덮밥
<input type="checkbox" name="lunch" value="칼국수"> 칼국수
<input type="checkbox" name="lunch" value="치즈김밥"> 치즈김밥
<input type="checkbox" name="lunch" value="피자"> 피자
<input type="submit" value="전송">
</form>
</body>
</html>
TodayMenu.java
package com.base;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TodayMenu extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
try{
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head><title>SELECT & POST</title></head>");
out.println("<body>");
out.println("<div align=center><h3>메뉴 선택</h3></div>");
String values[]= request.getParameterValues("lunch");
for(int i=0; i<values.length; i++){
out.print("<br/>");
out.print(values[i]);
}
out.println(" 나 먹어야 겠다.");
out.println("</body></html>");
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}