index.html -- > HelloServlet -- > SampleServlet -- > detail.jsp
name을 Object로 묶어 여기서 풀어서 여기에 뿌려줌
index
<!DOCTYPE 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>
<form action="" id="frm" method="post">
<table>
<tr>
<td>이름</td>
<td>
<input type="text" id="_name" name="name" size="20" placeholder="이름">
</td>
</tr>
<tr>
<td>나이</td>
<td>
<input type="text" id="_age" name="age" size="5" placeholder="나이">
</td>
</tr>
</table>
<button type="button" id="btn">전송</button>
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#btn").click(function() {
//alert('click');
if ($("#_name").val()=='') {
alert('이름을 입력해 주십시오');
$("#_name").focus();
}else if ($("#_age").val()=="") {
alert("나이를 입력해 주십시오");
$("#_age").focus();
}else {
$("#frm").attr("action", "hello").submit();
}
});
});
</script>
</body>
</html>
xml
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>sample07.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
HelloServlet.java
package sample07;
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;
import dto.HumanDto;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("HelloServlet doPost");
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
System.out.println("name : " +name);
System.out.println("age : " +age);
//그냥 넘기기 -> Get으로 감
//String ename = URLEncoder.encode(name);
//resp.sendRedirect("sample?name=" + name + "&age=" + age);
//-> 한글깨져서 뒷Java에서 Encoder로 설정 후 추가해줌
//Obj로 넘기기
HumanDto dto = new HumanDto(name, age);
req.setAttribute("hman", dto);
/* 아래 함수로 만듬
* RequestDispatcher dispatcher = req.getRequestDispatcher("sample");
* dispatcher.forward(req, resp);
*/
this.forward("sample", req, resp);
}
public void forward(String linkname, HttpServletRequest req, HttpServletResponse resp) {
RequestDispatcher dispatcher = req.getRequestDispatcher(linkname);
try {
dispatcher.forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SampleServlet.java
package sample07;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dto.HumanDto;
@WebServlet("/sample")
public class SampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
System.out.println("SampleServlet doGet");
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
System.out.println("name : " +name);
System.out.println("age : " +age);
//get
/*
* resp.sendRedirect("sample?name=" + name + "&age=" + age);
*/
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SampleServlet doPost");
HumanDto dto = (HumanDto) req.getAttribute("hman");
System.out.println(dto.toString());
//다시 짐싸
req.setAttribute("dto", dto);
forward("detail.jsp", req, resp);
}
public void forward(String linkname, HttpServletRequest req, HttpServletResponse resp) {
RequestDispatcher dispatcher = req.getRequestDispatcher(linkname);
try {
dispatcher.forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
detail.jsp
<%@page import="dto.HumanDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
HumanDto dto = (HumanDto)request.getAttribute("dto");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>detail</title>
</head>
<body>
<h1>detail.jsp</h1>
<%
out.println("<b>" + dto.toString() + "</b>");
%>
</body>
</html>
Dto.java
package dto;
public class HumanDto {
private String name;
private int age;
public HumanDto(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "HumanDto [name=" + name + ", age=" + age + "]";
}
}
'Servlet' 카테고리의 다른 글
session//세션 삭제//id//방문회수//name 삭제 (0) | 2020.07.21 |
---|---|
id, 비번, 방문회수 보기// Cookie//쿠키 //WebServlet//저장공간 (0) | 2020.07.21 |
select로 웹사이트 주소 송신하기 (0) | 2020.07.20 |
에러상태 코드 select로 값을 넘기기 (웹으로 확인하기) (0) | 2020.07.20 |
앙케이트 조사// 결과받아 web에 출력하기 (0) | 2020.07.20 |