순서 :
index -> xml -> java1 -> xml -> java2 ?
html
<body>
<!-- 링크를 <button>을통한 자바스크립트로 보내주기 -->
<h1>앙케이트 조사입니다</h1>
<form action="hello" id="frm" method="get">
<table>
<tr>
<td>이름</td>
<td>
<input type="text" name="name" size="20" value="유갱">
</td>
</tr>
<tr>
<td>나이</td>
<td>
<input type="text" name="age" size="20" value="30">
</td>
</tr>
<tr>
<td>좋아하는 과일</td>
<td>
<select name="fruit" multiple="multiple">
<option value="apple">사과</option>
<option value="pear">배</option>
<option value="banana">바나나</option>
</select>
</td>
</tr>
</table>
</form>
<button type="button" id="btn">송신</button>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function() {
$("#frm").submit();
});
});
</script>
</body>
java1(html에서 받고 java로 보내기)
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String sAge = req.getParameter("age");
System.out.println("name:" + name);
System.out.println("age:" + sAge);
int age = 0;
if (sAge == null || sAge.trim().equals("")) {
resp.sendRedirect("index.html");//다시 html로 돌려줌
}else {
age= Integer.parseInt(sAge);
}
String fruits[] = req.getParameterValues("fruit");
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
//servlet(java) -> servlet(java)로 이동 OR servlet(java) -> JSP로 이동
//(get에서 받았으면 get으로 이동함 post는 post로)
/* 1. sendRedirect 사용
* ResultServlet으로 이동
resp.sendRedirect("result");
*/
//데이터 모으기
ObjectDto dto = new ObjectDto(name, age, fruits);
//짐싸
req.setAttribute("myfruit", dto);
/* 2. forward 사용 -> 짐싸먼저
RequestDispatcher dispatch = req.getRequestDispatcher("result");
dispatch.forward(req, resp);
*/
//위에것을 한줄로 적기
req.getRequestDispatcher("result").forward(req, resp);
//이것도 가능하나 잘 안씀
//resp.sendRedirect("result?name=" + name + "&age=" + age + "&fruit=" + fruits[0]);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
java2(java1에서 보낸것 받기고 web에 2차출력)
public class ResultServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//보냈던 이름으로 받기
ObjectDto dto = (ObjectDto) req.getAttribute("myfruit");
System.out.println(dto.toString());
resp.setContentType("text/html; charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>제목</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>Hello Result</h1>");
pw.println("이름:<input type='text' value="+dto.getName()+">");
pw.println("나이:<input type='text' value="+dto.getAge()+">");
String fruits[] = dto.getFruit();
for (int i = 0; i < dto.getFruit().length; i++) {
pw.println("<p>"+fruits[i]+"</p>");
}
pw.println("</body>");
pw.println("</html>");
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
xml
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>sample06.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>result</servlet-name>
<servlet-class>sample06.ResultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>result</servlet-name>
<url-pattern>/result</url-pattern>
</servlet-mapping>
dto.java
public class ObjectDto implements Serializable{
private String name;
private int age;
private String fruit[];
public ObjectDto(String name, int age, String[] fruit) {
super();
this.name = name;
this.age = age;
this.fruit = fruit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getFruit() {
return fruit;
}
public void setFruit(String[] fruit) {
this.fruit = fruit;
}
@Override
public String toString() {
return "ObjectDto [name=" + name + ", age=" + age + ", fruit=" + Arrays.toString(fruit) + "]";
}
}
'Servlet' 카테고리의 다른 글
select로 웹사이트 주소 송신하기 (0) | 2020.07.20 |
---|---|
에러상태 코드 select로 값을 넘기기 (웹으로 확인하기) (0) | 2020.07.20 |
servlet 코드소스 (0) | 2020.07.20 |
앙케이트 조사// 링크를 <button>을 통한 자바스크립트로 보냄 (0) | 2020.07.20 |
text입력받아 링크로 넘기기 (0) | 2020.07.20 |