반응형
이클립스로 실행
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 ReqServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); // Req 정보 보기 doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = resp.getWriter(); out.println("<html><body>"); out.println("<h1>"+ req.getRemoteAddr()+"</h1>"); // ipv6 버전으로 출력된다. out.println("<h1>"+ req.getProtocol()+"</h1>"); // protocol 출력 out.println("<h1>"+ req.getContextPath()+"</h1>"); out.println("</body></html>"); } } |
AutoServlet
버튼을 이용해 Servlet을 만들수 있다.
Web.xml을 맵핑을 추가하지 않고 바로 만들어 준다. 3.0이상에서만 가능.
Service() 메서드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 ReqServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.service(arg0, arg1); PrintWriter out = resp.getWriter(); if(req.getMethod() =="GET"){ out.println("<html><body>"); out.println("<h1>"+ req.getRemoteAddr()+"</h1>"); // ipv6 버전으로 출력된다. out.println("<h1>"+ req.getProtocol()+"</h1>"); // protocol 출력 out.println("<h1>"+ req.getContextPath()+"</h1>"); out.println("</body></html>"); } } } |
get방식인지 post방식인지 구별해주는 메서드이다.
service메서드를 사용 결과
동일하다 .
Servlet은 javacode안에 html이 들어가있는 것이고, JSP는 html코드안에 Javacode가 들어가 있는 형태이다.
<%@ %> - 페이지 지시자
include방식
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <hr> <jsp:include page="test2.jsp"> <jsp:param value="kkb" name="id"/> <jsp:param value="4444" name="pw"/> </jsp:include> <hr> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <%= request.getParameter("id") %> <%= request.getParameter("pw") %> </body> </html> |
forward 방식
include액션과 유사하지만 현재페이지의 제어권을 완전히 다른 페이지로 기본형식
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> forwarding실습 <jsp:forward page="test2.jsp"> <jsp:param value="bit" name="id"/> <jsp:param value="bit1234" name="pw"/> </jsp:forward> </body> </html> |
Error
페이지의 에러처리
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page errorPage="error.jsp" %> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <%=request.getParameter("id").toUpperCase() %> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> 에러!! </body> </html> |
반응형
'(Before)BitSchool' 카테고리의 다른 글
2014/05/31 JSP - JDBC, connectionPool (0) | 2014.05.31 |
---|---|
2014/05/30 JSP - session,cookie, javaBean (0) | 2014.05.30 |
2014/05/28 JSP - servlet, get방식, post방식 (0) | 2014.05.28 |
2014/05/27 JSP (0) | 2014.05.27 |
2014/05/26 javanetwork - 직렬화, UDP (0) | 2014.05.26 |