jspÒ³ÃæÌøת·½·¨´óÈ«
JSPÖеÄÌøת£º
(1). forward()·½·¨
ʹÓõ½javax.servlet.RequestDispatcherÀà
RequestDispatcher rd = getServletContext().getRequestDispatcher("url");
rd.forward(requestVar,requestVar); //´«µÝservletµÄÈë¿Ú²ÎÊý
/*forwardº¯ÊýÒѾ°ÑÔÒ³ÃæµÄrequest,response¶ÔÏó´«ÈëеÄÒ³Ã棬Òò´ËÕâоÉÒ³ÃæÓµÓÐÏàͬµÄrequest,response¶ÔÏó¡£request.getparameter("var")¾Í¿ÉÒԵõ½ÏàÓ¦µÄÖµ.*/
/*forward()ÊÇÖ±½ÓÔÚserver×öµÄ£¬ä¯ÀÀÆ÷²¢²»ÖªµÀ£¬Ò²²»ºÍä¯ÀÀÆ÷´ò½»µÀ£¬ä¯ÀÀÆ÷µÄµØÖ·²»±ä»¯¡£*/
¼´£º
getServletContext().getRequestDispatcher("url").forward(requestVar,responseVar);
·þÎñÆ÷¶ËServlet½øÐÐתÏòµÄ4ÖÖ·½·¨£º
1.ServletContextµÄgetRequestDispatcher()
2.ServletContextµÄgetNamedDispatcher()
3.ServletRequestµÄgetRequestDispatcher()
4.ServletResponseµÄsendredirect()½øÐÐתÏò¡£
forward·½·¨£¬ÒòΪÕâЩÕâÑù±È½Ï¸ßЧ¡£Ö»ÓÐÔÚforward·½·¨²»ÄÜʹÓÃʱ£¬ÔÙʹÓÃservletresponseµÄsendredirect()·½·¨¡£
(2).response.sendRedirect(url);
ÆäʵÊÇÏòä¯ÀÀÆ÷·¢ËÍÒ»¸öÌØÊâµÄHeader£¬È»ºóÓÉä¯ÀÀÆ÷À´×öתÏò£¬×ªµ½Ö¸¶¨µÄÒ³Ã棬ËùÒÔÓÃsendRedirectʱ£¬ä¯ÀÀÆ÷µÄµØÖ·À¸ÉÏ¿ÉÒÔ¿´µ½µØÖ·µÄ±ä»¯¡£ÐÂÒ³Ãæ²»ÄÜ´¦Àí¾ÉÒ³ÃæµÄpagecontext(request,response,...)¶ÔÏó.
ÔÚresponse.sendRedirectÖ®ºó£¬Ó¦¸Ã½ô¸úÒ»¾äreturn;
(3).HTMLÓïÑÔµÄ×Ô¶¯Ìøת
<html>
<head>
<meta http-equiv="refresh" content="0;url="http://www.baidu.com">
0Êǵȴýʱ¼ä£¬Èç¹ûÉèÖÃΪ5±íʾµÈ´ý5sºó¿ªÊ¼Ìøת
</head>
…………
(4).JavaScriptÌøת
A: <script languate="javascript">
&
Ïà¹ØÎĵµ£º
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ......
//ѧϰ±Ê¼Ç, ¿ÉÄÜÓÐЩµØ·½Àí½âÓÐÎó, »á±í´ï²»Çå³þ
v512_servlet+jsp
01 ÔÚÐÞ¸ÄÍêCookieºó,Ò»¶¨Òª¼ÓÉÏresponse.addCookie(c);
CookieÖÐÖ»Äܱ£´æ×Öĸ»òÊý×ÖÐÅÏ¢;
02 req.getSession()Óëreq.getSession(boolean create)
ÎÞ²ÎÊýµÄ == ÓвÎÊýµÄ²ÎÊýÎ ......
<body>
<%
Cookie[] cookies = request.getCookies() ;
Cookie cookie = null ;
if(cookies!=null){
cookie = cookies[0] ;
out.print("ÉϴηÃÎʵÄipÊÇ£º " + request.getRemoteAddr() +"<br>") ;&n ......
String url="http://www.baidu.com";
ActionForward gotourl = new ActionForward(url);
gotourl.setPath(url);
gotourl.setRedirect(true);
return gotourl;
1.RequestDispatcher.forward()
¡¡¡¡ ÊÇÔÚ·þÎñÆ÷¶ËÆð×÷ÓÃ,µ±Ê¹ÓÃforward()ʱ,Servlet engine´ ......