servlet跳转Jsp方法


servlet跳转Jsp方法


1、sendRedirect:
可以将页面跳转到任何路径,不局限于web应用中,跳转的过程中url地址变化,无法使用request.setAttribute来传递。


servlet内容:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.sendRedirect ( "index.jsp" );
}


Jsp页面:
<body>
	我叫Jsp,呵呵哒!
</body>



2、forward:
url地址不变,只能跳转到本web应用中的页面上。可以用request.setAttibute方法


servlet内容:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String name = "我是Jsp,Php哪去了?";
        request.setAttribute("id",name);//存值
        request.getRequestDispatcher("/index.jsp").forward(request,response);
}


Jsp页面:
<body>
	<% String id = (String)request.getAttribute("id");   %>	
	<%="Asp又哪去啦?"+id %>
</body>



你可能感兴趣的:(servlet跳转Jsp方法)