"Head first Servlets and JSP" Note 24:include instructions and include Actions & param actions & foward actions

Source: Internet
Author: User
Tags autoload

Include directives and include actions

1. Sample Code

<%@ PageContentType= "Text/html;charset=utf-8"language= "Java" %><%@ PageImport= "java.io.*,java.util.*" %><HTML><Head>  <title>Auto Refresh Header Example</title></Head><Body><%--Insert the header file here--%> <%@ include  file= "header.jsp"%> <center>  <jsp:include  page= "footer.jsp" /> </Body></HTML>
 <  %@ page  contenttype  = "Text/html;charset=utf-8"   language  = "Java"  %  >  <  img  src  = "Images/header.jpg"   Alt  = "Shanghai Flower Harbor-Tulip"  />  <  p  >  I am header </ p  >  
<contentType= "Text/html;charset=utf-8"  language= "java"  %><src= "images/header.jpg"  alt = "Shanghai Flower Harbor-Tulip" /> < P > I am the header </p>

2. Source Code Analysis

Web Project Structure

Package these files into a war, deploy to the local Tomcat WebApps, and start Tomcat.

After the first access to index.jsp, the following files are generated

We observed that there was no header_jsp generated, as we could imagine, because we used the include directive instead of the include action.

Then see index_jsp Source code:

Try {  response.setcontenttype ( "Text/html;charset=utf-8");      PageContext = _jspxfactory.getpagecontext (this, request, response, NULL, True, 8192, true);      _jspx_page_context = PageContext;      application = Pagecontext.getservletcontext ();      Config = Pagecontext.getservletconfig ();      Session = Pagecontext.getsession ();      out = Pagecontext.getout (); _jspx_out =Out  ; Out.write ( "\ n");      Out.write ("\ n");      Out.write ("\ n");      Out.write ("); out.write ("\r\n"); Out.write ("<p> I am header </p>\r\n"); Out.write ("\ r \ n");      Out.write ("\ n");      Out.write ("\ n");      Out.write ("<center>\n");      Out.write ("//Set refresh, AutoLoad time as 5 seconds Response.setintheader ("Refresh", 60);    Get current time Calendar calendar = new GregorianCalendar ();    String am_pm;    int hour = Calendar.get (Calendar.hour);    int minute = Calendar.get (Calendar.minute);    int second = Calendar.get (Calendar.second);    if (Calendar.get (calendar.am_pm) = = 0) am_pm = "AM";    else am_pm = "PM";    String CT = hour+ ":" + Minute + ":" + second + "+ am_pm;        Out.println ("Crrent time:" + CT + "\ n");      Out.write ("\ n");      Out.write ("</center>\n");      Out.write ("\ n"); Out.write (' \ n '); org.apache.jasper.runtime.JspRuntimeLibrary.include (Request, response,       "footer.jsp", out, false ); Out.write ( "\ n");      Out.write ("\ n");      Out.write ("</body>\n");    Out.write ("

The bold distinction is the conversion code for the include directive and the include action.

Param action

Modify the index.jsp code above:

<%--Insert Footer file here--%><jsp:include page= "footer.jsp" >  <jsp:param name= "SubTitle" value= "We take the String out of SOAP. "/></jsp:include>

Modify Footer.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%><a href= "index.jsp" >home page I'm the footer! </a>

Effect

Foward Action

Garbled problem is not resolved, can only input Chinese.

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>   <(Request.getparameter ("UserName")null) {%  >        <page= "handleit.jsp"/>    <%}% >      <p> Hello! ${param.username}</p></body>

handleit.jsp

<%@ PageContentType= "Text/html;charset=utf-8"language= "Java" %><HTML><Body>    <% request.setcharacterencoding("Utf-8"); %>    <P>I'm sorry, but you need to log in again.</P>    <formAction= "hello.jsp">        <P>User name:<inputtype= "text"name= "UserName"></P>        <P><inputtype= "Submit"></P>    </form></Body></HTML>

"Head first Servlets and JSP" Note 24:include instructions and include Actions & param actions & foward actions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.