Skip to content

Instantly share code, notes, and snippets.

@junsuk5
Created May 20, 2022 05:37
Show Gist options
  • Save junsuk5/7e7fe052389e0f43e4f103bd820da002 to your computer and use it in GitHub Desktop.
Save junsuk5/7e7fe052389e0f43e4f103bd820da002 to your computer and use it in GitHub Desktop.
서블릿
package controller;
import java.io.IOException;
import dto.Product;
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(name = "MyController", urlPatterns = {"/product", "/products"})
public class MyController extends HttpServlet {
private static final long serialVersionUID = 6953161035339908798L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String command = req.getRequestURI().substring(req.getContextPath().length());
resp.setContentType("text/html; charset=utf-8");
req.setCharacterEncoding("utf-8");
if (command.equals("/product")) {
req.setAttribute("items", new Product("ppp111", "nnnaa", 10));
req.setAttribute("json", "{\"code\":\"200\", \"msg\":\"success\"}");
resp.getWriter().print("{\"code\":\"200\", \"msg\":\"success\"}");
// req.getRequestDispatcher("productjson.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment