본문 바로가기

Servlet

Servlet 기본

 

기본 개념

 

	Server + Applet = Servlet
	
	Web(html, client) Server	< -- > Java(String, Object(Class)) Server < -- > DB Server

	Web Brower							Web Application Server(WAS)
	
	client ----(request:(http://x.x.x.x:8090/projext명/index.html))-> Web Server, Web Container <-- DB
											Servlet
											JSP
											resource(보관함)
											web.xml파일:(Web Server가 
											동작 헀을시에 setting & initialize)
								< ---(response(응답))--			
								
	*Server 동작방식
	Get(open), Post(close)

 

 

 

 

 

 

 

 

 


완성본

 

 

 

 

 

 

1. tomcat web.xml 파일에서

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0">

부분만 복사해다가 붙여 새 web.xlm파일을 만든다

 

 

2. 자바 리소스에 HttoServlet을 상속받은 자바 파일을 만든다 (HelloServlet.java)

 

 

3. <java>

override로 doGet과 doPost생성자를 생성한다

package sample01;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("HelloWervlet doGet");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("HelloWervlet doPost");
	}
}

 

 

 

4. .xml 설정

----> servlet Class 등록 
1. servlet의 name은 servlet-mapping의 name은 동일해야 한다
2. class는 클래스가 있는 주소 이름
3. url-pattern : 

<servlet>
	<servlet-name>helloServlet</servlet-name>
	<servlet-class>sample01.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>helloServlet</servlet-name>
	<url-pattern>/location</url-pattern>
</servlet-mapping>
</web-app>

 

 

5. index.html에 버튼 및 원하는 내용 입력 후 실행한다

<h1>Servlet</h1>
<form action="location" method="get">
	<input type="submit" value="get 방식">
</form>

<form action="location" method="post">
	<input type="submit" value="post 방식">
</form>
</body>
</html>

 

 

 


자동으로 web.xml 생성하는 방법

프로젝트명 마우스 오른쪽 클릭 -> Java EE Tools -> Generate Deployment ...