- 创建springboot项目
- 写servlet,重写doGet 、doPost方法
@WebServlet(urlPatterns = "/myservlet")
public class Myservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("世界您好。Hello World!!!");
resp.setContentType("text/html;character=utf-8");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- 入口类加扫描
@SpringBootApplication
@ServletComponentScan(basePackages = "com.bjpowernode.springboot.servlet")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 启动,出现乱码
- 打开核心配置文件,设置请求响应字符编码
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true
server.servlet.encoding.charset=UTF-8