在后端写一个java方法在前端调用时输出helloworld
时间: 2024-03-08 08:50:06 浏览: 67
Flex和java通信之 helloworld
好的,以下是一个简单的Java Servlet,当它被前端页面调用时,会向前端输出"helloworld"。
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("helloworld");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
当你在前端页面中向这个Servlet发送HTTP请求时,它会向前端输出"helloworld"。
阅读全文