JSP basic introduction

1.1 What is JSP

Concept:

  • JSP, i.e.,Java Server Pages.JavaServer pages, that is, pages running on the server side. It is made ofSunThe company advocates, many international large companies to participate in the establishment of a dynamic web technology.JSPThe technology is in traditional static web pagesHTMLInsert into fileJavaCode snippets andJSPA file formed after a label with a suffix named.jsp. useJSPThe development ofWebThe application is cross-platform and can be used inLinuxCan also run on other operating systems.
  • JSPThe essence of the document isServlet. Just,JSPwithServletThe difference is,JSPIt’s designed for data presentationServlet, it has a special way of writing. The ordinaryServletIs used to complete business logic processing. Due to theServletIs running in a singleton multithreaded environment, soJSPIt also runs in a singleton multithreaded environment.

The JSP 1.2 specification

Concept:

  • willJSPPage translation asServletThe process ofTomcatTo complete. inTomcatThere is a built-in one inJSPTranslation engine when first accessing theJSPPage when the translation engine willJSPPage translation asServletthe.javaFile, and compile it to.classFile to run.
  • SUNcompany-madeJavaEEThe specification contains two important sub-specifications:ServletThe specification, andJSPSpecification. Among themJSPThe specification is about how to putJSPPage translation asServlet. For example,JSPThe pageHTML.css.JavaScript, and the normal text will be translatedout.write()In the.
  • TomcatIn theJSPThe translation engine follows thisJSPSpecification.

1.3 the JSP comment

  • inJSPUsing annotations in a page, you can use the content to be annotatedThe < % -- -- % >Enclosed.
  • Of course, inJSPIt can also be used in the pageHTMLThe annotation<! -- -->. But the effect of their use is different.
    • (1) : HTMLComments will beJSPThe translation engine translates toServlettheout.write(); whileJSPComments will beJSPTranslation engine ignore, inServletYou can’t see it in.
    • (2) :When viewing the source code in the client browser,HTMLComments are viewable; butJSPComments are not viewable on the client side.
This is my JSP page<br> <! -- HTML --> <%-- JSP -->Copy the code

1.4 Java code block in JSP

  • JSPJava code block is also calledJSPSmall script, withThe < % % >Bracketed parts, which can be writtenJavaThe code. Writing in theJavaThe code in the code block will beJSPThe translation engine puts it intoServletthe_jspService()Method as methodJavaStatement appears. So, it must end with a semicolonJavaStatements.
  • It’s important to note that,JSPCode blocks can be placed inJSPAnywhere on the page, you can place as many as you want. But they’re all going to be placed in orderServletthe_jspService()Methods. That is, the order in which it is executedJSPThe order of occurrence on the page is consistent. For example,
The < %double a = 0;	
                int i = 5;
		System.out.println("Java snippet");
		System.out.println("Java snippet");
		System.out.println("Java snippet");
		System.out.println("Java snippet");   
                
                 a = d * i;
                system.out.println( "a = "+a); % >Copy the code
  • Be translated intoservletthe_jspService()Method, butJSPThe following cannot appear in a small script
    • (1) : declared variables cannot add permission access control characters
    • (2) : Methods cannot be defined
    • (3) : static statement blocks cannot be defined

1.5 JSP statement block

  • inJSPPage useThe < %! % >The enclosed part is called a declarative block. Declare the contents of the statement block to beJSPEngine translation toServletClass body, which is not contained in any method body.
  • In that case, inJSPYou can declare instance variables, instance methods, static methods, static code blocks, etc. In addition, these contents can beJSPtheJavaCode access in a code block. Because they are one and the sameServletClass code. However, it is still not recommendedJSPDeclare instance variables in the declaration statement block of becauseJSPIs run in a singleton multithreaded environment, instance variables will cause thread safety issues.
  • It should be noted that in theJSPDeclaration statement block, is unable to write ordinaryJavaThe statement. Otherwise, these statements will appear directly in theServletIn the class body of.
  • Declaration blocks can also be defined inJSPAny location on the page, and you can define as many as you want.

Such as:

The < %!// You can add access control characters
    privatel int amount = 3;
    // Methods can be defined
    public void showData(double data){
        system.out.print1n( "data = " + data);
    }
    // You can define the static statement block static {
    system.out.println( "= = = = = = = = = = = = = ="); } % >Copy the code

1.6 JSP expression block

  • inJSPPage use< % = % >The enclosed part is called an expression block. It can be inJSPOutputs variables, constants, and the values of the various expressions they compose in the page. Note that expressions, not statements, do not have semicolons. The expression will beJSPEngine translation to_jspService()methodsout.write()Method. For example,
The < %int count = 5; % >// The count value is displayed on the page
<%=count %>
Copy the code