반응형
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Nana extends HttpServlet
{
public void service(HttpServletRequest request
, HttpServletResponse response)
throws IOException, ServletException
{
//OutputStream os = response.getOutputStream();
//PrintStream out = new PrintStream(os,true);
PrintWriter out = response.getWriter();
out.println("Hello Servlet!");
}
}
한글 출력
한글을 출력으로 내보낼때 setContentType으로 contenttype을 지정해서 보내준다.(인코딩 방식의 차이 때문에)
package com.newlec.web;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class Nana extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setCharacterEncoding("UTF-8");
res.setContentType("text/html; charset=UTF-8");
for (int i =0; i<100; i++) {
out.println((i+1)+": 한글 servlet <br >");
}
}
}
get요청과 쿼리스트링
query로 요청할때 http://localhost:8080/hello?cnt=3
위처럼 cnt 값을 파라미터로 넣어주면 된다.
package com.newlec.web;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class Nana extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setCharacterEncoding("UTF-8");
res.setContentType("text/html; charset=UTF-8");
int cnt = Integer.parseInt(req.getParameter("cnt"));
for (int i =0;i<cnt; i++) {
out.println("안녕 servlet<br/>");
}
}
}
반응형
'Back-end > JSP & Servlet' 카테고리의 다른 글
servlet servlet context 상태 저장소 (0) | 2022.07.03 |
---|---|
servlet 입력으로 배열 받는 방법 (0) | 2022.07.03 |
servlet utf-8 type 설정 및 serlvet filter 사용 (0) | 2022.07.02 |
servlet 어노테이션을 활용한 url 매핑 (0) | 2022.07.01 |
개념 및 설치 (0) | 2022.06.27 |