文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[学习笔记]马士兵 Servlet & JSP(3.Servlet和JSP..

[学习笔记]马士兵 Servlet & JSP(3.Servlet和JSP..

时间:2010-04-04  来源:mcuflower

(1)从JSP调用Servlet可用<jsp:forward>,请求信息自动转到Servlet FromJspToServlet.jsp <html>
    <body bgcolor="green">
        <!-- Forward to a servlet, 这个servlet存放在web-inf的servlet目录下 -->
        <jsp:forward page="/servlet/ServletToJSP" />
    </body>
</html>

(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
<%@page contentType="text/html;charset=gb2312" %> <html>
    <meta context="text/html;charset=gb2312">
    <head>
        <title>Servlet使用JSP</title>
    </head>
   
    <body bgcolor="gray">
        <h2>Servlet使用JSP的例子</h2>
        <h2>这个页面是被Servlet调用的</h2>
    </body>
</html>
  说明:以上相互调用也可以直接使用sendRedirect
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载