나이 미입력시 다시 index.html로 복귀
html
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<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>
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]);
}
}
@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' 카테고리의 다른 글
에러상태 코드 select로 값을 넘기기 (웹으로 확인하기) (0) | 2020.07.20 |
---|---|
앙케이트 조사// 결과받아 web에 출력하기 (0) | 2020.07.20 |
servlet 코드소스 (0) | 2020.07.20 |
text입력받아 링크로 넘기기 (0) | 2020.07.20 |
Servlet 기본 (0) | 2020.07.20 |