<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello">hello</a>
</body>
</html>
HelloServlet ("바로 이곳으로 이동")
@WebServlet("/hello") 를 사용해서 a href와 일치시켜준다
package sample09;
import java.io.IOException;
import java.io.PrintWriter;
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;
@WebServlet("/hello") // namespace -> 이름공간 (주석)
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet doGet");
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>제목</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>Hello Servlet Start</h1>");
//include
//RequestDispatcher dis = req.getRequestDispatcher("include");
//dis.include(req, resp);
//forward
RequestDispatcher dis = req.getRequestDispatcher("forward");
dis.forward(req, resp);
pw.println("<h1>Hello Servlet End</h1>");
pw.println("</body>");
pw.println("</html>");
pw.close();
// include(가져오다), forward(그쪽으로 이동)
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
ForwardServlet.java
package sample09;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/forward")
public class ForwardServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>제목</title>");
pw.println("</head>");
pw.println("<body bgcolor='blue'>");
pw.println("<h1>Forward Servlet</h1");
pw.println("</body>");
pw.println("</html>");
// pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
'Java > java 기초' 카테고리의 다른 글
Servlet session 넘기기및 삭제 (0) | 2020.01.17 |
---|---|
Servlet 쿠키값 넘기기 (0) | 2020.01.17 |
자바 JFRAME + JDBC + OracleDB를 활용한 카페 주문 시스템 구현하기 (3) | 2019.12.31 |
쓰레드를 활용한 오브젝트 전송하기 (0) | 2019.12.10 |
쓰레드 공부하기 (0) | 2019.12.10 |