이클립스로 톰캣을 이용해보자
톰캣의 서블릿




jsp 생성하면 utf-8 이 아님
전부 utf-8로 세팅

webapp/home.jsp 생성하고
body 에 스크립트릿 / 표현식
<% // 스크립트릿
String name = "홍길동";
%>
<h1> // 표현식
나의 이름은 : <%=name%>
</h1>





소스탭 누르고 맨 아래로 내림
<Context docBase="web" path="/" reloadable="true"
source="org.eclipse.jst.jee.server:web" />
<Context docBase="web2" path="/web2" reloadable="true"
source="org.eclipse.jst.jee.server:web2" />


// localhost:8080/***.do
@WebServlet("*.do")
public class MyServlet extends HttpServlet {
public MyServlet() {
super();
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("서비스 요청됨");
// super.service(request, response); // super.service()가 get post 를 연결시켜줌
System.out.println(request.getMethod()); // 콘솔에 `GET` 으로 출력
// super.service() 내부적으로 아래의 구조가 있음
if(request.getMethod().equals("GET")) {
doGet(request,response); // 콘솔에 `doGet 요청됨`
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet 요청됨");
// http://localhost:8080/34256.do - 콘솔에 / doGet 요청됨
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 서블릿에 html 코드 작성하려니 너무 끔찍하다
String html ="<!DOCTYPE html>"
+ "<html>"
+ "<head>"
+ "<meta charset=\"UTF-8\">"
+ "<title>Insert title here</title>"
+ "</head>"
+ "<body>";
String name = "홍길동123ddddddddddddddddddddddddddddd";
html += " <h1>"
+ " 나의 이름은 : "+name+" </h1>"
+ "</body>"
+ "</html>";
// 스트림에 버퍼 달기
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(
response.getOutputStream()),true);
pw.println(html);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost 요청됨");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = "홍길동";
%>
<h1>
나의 이름은 : <%=name%>
</h1>
</body>
</html>
왜 jsp 에 자바 코드를 작성하는지 알수 있음