方法一:
@WebServlet(urlPatterns = "/test2")
public class TestServlet extends HttpServlet
{
//重写get方法
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置返回类型为json
response.setContentType("application/json");
//设置返回字符集
response.setCharacterEncoding("utf-8");
//输出对象
PrintWriter writer = response.getWriter();
//输出error消息
writer.write("执行TestServlet内doGet方法成功!");
writer.close();
}
}
@Configuration
@ServletComponentScan
public class ServletConfiguration {
/*@Bean
public ServletRegistrationBean servletRegistrationBean()
{
return new ServletRegistrationBean(new TestServlet(),"/test");
}*/
}
网友评论