|
|
Posted on 1/3/2015 9:28:01 PM
|
|
|

(1). forward() method Use the javax.servlet.RequestDispatcher class RequestDispatcher rd = getServletContext().getRequestDispatcher("url"); rd.forward(requestVar,requestVar); Pass the entry parameters of the servlet Namely: getServletContext().getRequestDispatcher("url").forward(requestVar,responseVar);
There are 4 ways to turn server-side servlets: 1. getRequestDispatcher() for ServletContext 2. getNamedDispatcher() for ServletContext 3. getRequestDispatcher() for ServletRequest 4. ServletResponse's sendredirect() to steer. forward method, because these are more efficient. Only use the sendredirect() method of servletresponse when the forward method is not available.
(2).response.sendRedirect(url); In fact, it sends a special header to the browser, and then the browser turns to the specified page, so when using sendRedirect, you can see the address change in the browser's address bar. The new page cannot handle the pagecontext(request, response,... of the old page. Object. response.sendRedirect should be followed by a return;
(3). Automatic jump to HTML language <html> <head> <meta http-equiv="refresh" content="0;url="http://www.baidu.com"> 0 is the waiting time, if set to 5, it means that the jump starts after waiting for 5s </head> …………
(4). JavaScript jump A: <script languate="javascript"> window.location.replace("http://www.baidu.com"); </script> B: You can also directly assign a value to the window.location attribute, window.location="url", which is different from A in that it has a history C: <script languate="javascript"> document.location.href("http://www.google.com"); </script> For document, location is not an object, document.location=document.url The forward(), back(), and go() methods of the D:history object require a shaping entry parameter <a href="javascript:history.go(-1)" > go back to the previous step</a> Equivalent to <a href="javascript:history.back()" > return to the previous step</a> E: document.formName.action="test.jsp"; document.formName.submit(); JS commit method The first type: <script language="javascript" type="text/javascript"> window.location.href="login.jsp?backurl="+window.location.href; </script> The second type: <script language="javascript"> alert("return"); window.history.back(-1); </script> The third type: <script language="javascript"> window.navigate("top.jsp"); </script> The fourth type: <script language="JavaScript"> self.location='top.htm'; </script> The fifth type: <script language="javascript"> alert("Illegal access!") ); top.location='xx.jsp'; </script>
(5). <jsp:forward page="nextpage.jsp"/> As: <jsp:forward page="/servlet/login" /> <jsp:forward page="/servlet/login"> <jsp:param name="username" value="jsmith" /> </jsp:forward> <jsp:forward> tag passes a request object containing a user request from one JSP file to another. <jsp:forward>tag, will not be executed. You can use the <jsp:param> tag to transfer parameters and values to the object file, which must be a dynamic file that can handle parameters. If you are using unbuffered output, be careful when using <jsp:forward>. If the jsp file already has data before you use <jsp:forward>, then the file execution will be wrong. JSP page jump method collection 1. response.sendRedirct("Jump to page"); This method modifies the HEADER part of the HTTP protocol to issue a redirect command to the browser, so that the browser displays the content of the redirect web page. request cannot pass the value past. Execute all the code on the page before jumping to the page. Jump to the address bar to change. You can jump to the page response.sendRedirct(http://www.sun.com) on a different server. 2.response.setHeader(); This method, like response.sendRedirect, is done by modifying the HEADER part of the HTTP protocol. <% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn="/index.html"; response.setHeader("Location",newLocn); % > 3.<jsp:forward page="jump page"/> This method uses the mechanism of the server to output data to the buffer first, before sending the content of the buffer to the client, the original does not send, but instead sends the content of the page, if there is a lot of output before <jsp:forward>, the previous output has made the buffer full, will be automatically output to the client, then the statement will not work, this should be paid special attention. request can pass the value past. Jump directly to the page, the following code is not executed. The address bar remains unchanged after jumping. Unable to jump to a page on a different server. The picture is not an absolute path that cannot be displayed. 4. request.getRequestDispatcher("Jump to page"); request can pass the value past. Execute all the code on the page before jumping to the page. The jump address bar remains unchanged. You cannot jump to a page on another server <% RequestDispatcher rd = request.getRequestDispatcher("to.jsp"); rd.forward(request,response); %> There are 3 ways to redirect the output: RESPONSE. SETREDERECT("URL") This method modifies the HEADER part of the HTTP protocol to issue a redirect command to the browser, so that the browser displays the content of the redirected web page. response.sendRedirect("http://localhost:7001/index.html"); The following method can also change the HTTP HEADER property, which works the same as 1. <%response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn="/index.html"; response.setHeader("Location",newLocn); % > < JSP:FORWORD > is adopted This method uses the mechanism of the server to output data to the buffer first, before sending the buffer content to the client, the original does not send, but instead sends the content of the page, if there is a lot of output before <JSP:FORWORD>, the previous output has filled the buffer and will be automatically output to the client, then the statement will not work, this should be paid special attention. There are three ways to nest another JSP page within one JSP page: 1. By <%@ include file="header.jsp"%> this method can only achieve static page loading, and is most commonly used to realize the loading of general page header header.jsp and footer.jsp in a project. 2. By <%@ include page="tt.jsp"%> this method can realize dynamic page loading, but it needs to pass parameters to the page; 3. Load another jsp page to the page by <iframe src="jsp's path" >, which is an inline framework. If you use forward to pass the parameter, write it like this: <%String str="You are logged in!"; %> <jsp:forward page="index.jsp> <jsp:param name="name" value= <%=str%>/> </jsp:forward> |
Previous:JSP destroys the session session, invalidating user loginNext:Usage of the jstl tag <c:if>
|