import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/jsonP1") public class jsonP1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // jsonP跨域的时候只支持get请求,因为我们写src本质就是一个get请求的传递 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //如果是输出前端代码呢? // out.println("alert(123);"); // out.println("sayHello();"); String fun = request.getParameter("fun"); // out.println(fun + "({\"name\" : \"Jack\"});"); out.println(fun + "(5,6);"); } }
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/jsonP1")
public class jsonP1 extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// jsonP跨域的时候只支持get请求,因为我们写src本质就是一个get请求的传递
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//如果是输出前端代码呢?
// out.println("alert(123);");
// out.println("sayHello();");
String fun = request.getParameter("fun");
// out.println(fun + "({\"name\" : \"Jack\"});");
out.println(fun + "(5,6);");
}
}
import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/hello") public class Hello extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置响应头 response.setHeader("Access-Control-Allow-Origin","http://localhost:8080"); //跨域访问的资源允许访问,设置响应头的方式原理是 response.getWriter().println("Halla Ajax"); } }
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/hello")
public class Hello extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应头
response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");
//跨域访问的资源允许访问,设置响应头的方式原理是
response.getWriter().println("Halla Ajax");
}
}