-
Servlets : 기본 사용 방법Spring 2021. 6. 18. 21:52
먼저 dynamic web project를 실행한다.
servletdemo프로젝트를 만들고 servlet을 새로 만들어준다.
(com.yunhalee.servletdemo 패키지 안에 HelloWorldServlet생성)
생성된 servlet파일에서 doget 함수만 오버라이딩 해준다.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Step 1: set the content type response.setContentType("text/html"); //Step 2: get the printwriter PrintWriter out = response.getWriter(); //Step 3: generate HTML content out.println("<html><body>"); out.println("<h2>Hello World</h2>"); out.println("Time on the server is: " + new java.util.Date()); out.println("</body></html>"); }
jsp와 서블렛 차이를 알아보자.
서블렛 예시를 만들어보자.
html을 servlet에서 보여주게끔 설정하는 데,
먼저 student-form.html파일을 만들어준다.
<html> <body> <form action="StudentServlet" method="GET"> First name: <input type="text" name="firstName"/> <br/><br/> Last name: <input type="text" name="lastName"/> <br/><br/> <input type="submit" value="Submit"/> </form> </body> </html>
그다음 새로운 서블렛을 생성해준다.
doget 메소드만 새롭게 오버라이딩 해준다.
*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Step 1: set content type response.setContentType("text/html"); //Step 2: get the printwriter PrintWriter out = response.getWriter(); //Step 3: generate the HTML content out.println("<html><body>"); out.println("The student is confirmed : " + request.getParameter("fristName")+ " " + request.getParameter("lastName")); out.println("</body></html>"); }
이번엔 post를 구현해보자.
Servlet Parameter를 읽어오는 코드를 작성해보자.
WEB-INF>web.xml파일에 다음과 같이 param을 추가해준다.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>servletdemo</display-name> <context-param> <param-name>max-shopping-cart-size</param-name> <param-value>99</param-value> </context-param> <context-param> <param-name>project-team-name</param-name> <param-value>The Coding Gurus</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
TestParamServlet을 만들어주고 다음과 같이 doget을 오버라이딩 해준다.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Step 1: set content type response.setContentType("text/html"); //Step 2: get printwriter PrintWriter out = response.getWriter(); //Step 3: read configuration params ServletContext context = getServletContext();//Inherit from HttpServlet String maxCartSize = context.getInitParameter("max-shopping-cart-size"); String teamName = context.getInitParameter("project-team-name"); //Step 4: generate HTML content out.println("<html><body>"); out.println("Max cart : "+ maxCartSize); out.println("<br/><br/>"); out.println("Team name : " + teamName); out.println("</body></html>"); } /**
'Spring' 카테고리의 다른 글
Database App with JDBC : 학생 리스트 DB에서 가져오기 (Read) (0) 2021.06.18 MVC : MVC의 기본작용방법 (0) 2021.06.18 JSP : Standard JSP Tag Library (JSTL) - I18N Tags (Multi-Lingual App) (0) 2021.06.18 JSP : Standard JSP Tag Library (JSTL) - Functional Tags (0) 2021.06.18 JSP : Standard JSP Tag Library (JSTL) - Core Tags (0) 2021.06.18