Learn the basics of everyday notes <day12>jsp

Source: Internet
Author: User

1.JspBasic1.1JSP Introduction

The role of Servlets: techniques for developing dynamic resources in the Java language

The role of JSP: the technology of developing dynamic resources in the Java language (+html language)

JSP is the servlet

Features of 1.2JSP

1) The operation of the JSP must be given to the Tomcat server

Tomcat's work directory: the Tomcat server holds temporary files for the JSP runtime

2) JSP page can write HTML code, also can write Java code

(HTML pages cannot write Java code, and JSP pages can write Java code)

1.3 Experience JSP page function

Requirements: Display the current time to the browser

1<%@ Page Language="Java"Import="java.util.*,java.text.*"pageencoding="Utf-8"%>2 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">3 <HTML>4   <Head> 5     <title>First JSP page</title>  6   </Head>7   <Body>8<%9         //Write Java codeTen         //Get current Time One SimpleDateFormat SDF= NewSimpleDateFormat (); A         Stringcurdate=Sdf.format (New Date()); -         //output content to browser -         //response.getwriter (). Write (""); the Out.write ("the current time is 2:"+curdate); -%> -   </Body> - </HTML>
1.4jsp Execution Process

Execution process

1) access to 01.hello.jsp page,tomcat scan to jsp file, in %tomcat %/work translating JSP files into java source files

(01.hello.jsp-_01_hello_jsp.java) (translation)

2)thetomcat Server compiles the Java source files into a class bytecode file (compile)

(_01_hello_jsp.java-_01_hello_jsp.class)

3)Tomcat Server constructs _01_hello_jsp class object

4)thetomcat Server calls the _01_hello_jsp class inside the method, and the return content is displayed to the browser.

Attention:

1)jsp file modified or jsp Temporary files are deleted, to re-walk translation (1) and compile (2 ) of the process

1.5jsp and servlet relationships

      

JSP translation of java files:

Public final class _01_hello_jsp extends Org.apache.jasper.runtime.HttpJspBase

Implements Org.apache.jasper.runtime.JspSourceDependent {

Httpjspbase class: Public abstract class Org.apache.jasper.runtime.HttpJs PBase

Extends Javax.servlet.http.HttpServlet implements Javax.servlet.jsp.HttpJspPage {

Conclusion: JSP is a servlet program!!!

Servlet technology can be used in JSP programs

JSP technology is not all applicable to the servlet program!

The life cycle of a servlet:

1) Construction method (1th visit)

2) Init method (1th visit)

3) Service method

4) Destroy method

The life cycle of a JSP

1) Translation: Jsp->java file

2) Compile: Java file->class file (servlet program)

3) Construction method (1th visit)

4) Init method (1th visit): _jspinit ()

5) Service method: _jspservice ()

6) Destroy method: _jspdestroy ()

2.jsp Syntax 2.1jsp templates

The HTML code in the JSP page is the template for the JSP

2.2jsp-expression

Syntax: <%= variable or expression%>

Effect: The value of an output variable to the browser or the result of an expression calculation

Attention:

1) The principle of expression is to translate into out.print ("variable"), write the content to the browser by this method

2) No semicolon is required after expression

Script for 2.3jsp

Syntax: <%java code%>

Role: Executing Java code

Note: The principle is that the Java Code in the script is copied intact into the _jspservice method.

Statement of the 2.4JSP

Syntax: <%! variable or method%>

Function: A variable or method that declares a JSP

Note: variables are translated into member variables and methods are translated into member methods

2.5jsp annotations

Syntax: <%!--jsp Comment--%>

Note: HTML annotations are translated and executed, and JSP annotations cannot be translated and executed

3.jsp of the three instructions 3.1include instructions

Role: Used in the current page to include other pages

Syntax: <% @include file= "common/header.jsp"%>

Note: 1) The principle is to translate the contents of the included pages into the included pages, merge into a translation of a Java source file, in the compilation run, this contains called static inclusion (source code contains)

2) If a static inclusion is used, the global HTML tag is not required in the included page. such as (Html,head,body)

3.2page instruction

Role: Tell the Tomcat server how to translate the JSP file

<%@ Page Language="Java"   --tell the server what dynamic language to use to translate the JSP file import="java.util.*" --tell the server what package the Java file uses to import the package, with multiple packages separated by commas pageencoding="Utf-8"  --tell the server what encoding to use to translate JSP files (into Java files) ContentType="text/html; Charset=utf-8"Server Send browser data type and content encoding note: In the development tool, only need to set pageencoding in the future to solve the Chinese garbled problem errorpage="error.jsp"Iserrorpage="false"Buffer="8kb"Session="true"iselignored="false"%>
3.3taglib instruction

    

Learn the basics of everyday notes <day12>jsp

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.