(1)从JSP调用Servlet可用,请求信息自动转到Servlet
FromJspToServlet.jsp
(2)从Servlet调用JSP可以使用RequestDispatcher接口的forward(req, resp)方法,请求信息需要显示传递
ServletToJSP.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 ServletToJSP extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置属性并将它分发给/servlet_jsp/ServletUseJsp.jsp处理
resp.setContentType("text/html;charset=gb2312");
req.setAttribute("servletName", "ServletToJSP");
// RequestDispatcher getRequestDispatcher(String path):
// Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
getServletConfig().getServletContext().getRequestDispatcher("/servlet_jsp/ServletUseJsp.jsp").forward(req, resp);
}
}
(3)ServletUseJsp.jsp
< contentType="text/html;charset=gb2312" %>
Servlet使用JSP
Servlet使用JSP的例子
这个页面是被Servlet调用的
说明:以上相互调用也可以直接使用sendRedirect
阅读(3082) | 评论(0) | 转发(0) |