protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().writeString("Hello World!"); }
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { StringBuilder sb = new StringBuilder(); sb.append("Name: ").append(req.getParameter("name")).append("\n"); sb.append("Email: ").append(req.getParameter("email")).append("\n"); sb.append("Message: ").append(req.getParameter("message")).append("\n"); resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().writeString(sb.toString()); }Both examples use the javax.servlet.http package, specifically the HttpServletResponse class, to write a string to the response output stream using the writeString() method.