Java EE Basics jsp

Source: Internet
Author: User
Tags html comment tomcat server

Software 152 Tang Wei

First, the basic principle of JSP and the relationship with Servlet

Before the JSP was present, we visited the Web site as a servlet, which returned the HTML code. Just like this:

      Out.Write"\ n");Out.Write"\ n");Out.Write"Out.Write"Out.Write "<title></title>\n"); out. write ( "out. write ( "<body>\n"); out. write ( "\ t"); out. print ( "<p>hello world</>"); out. write ( "\ n"); out. write ( "</body>\n"); out. write ( "      

All the HTML code is exported to the browser in this way, the way the HTML code is coupled in Java code, the direct result of the front-end programmer can not directly participate in the coding work, the workload of the back-end programmer is increasing, this is inefficient, is inevitable to be eliminated.
In the unwillingness to suffer, we invented JSP, which is a way of coupling Java code in HTML code, similar to this:

<title></title>  <body> //输出一个字符串,具体语法,下面介绍 <p><%="hello,world"%></p> </body>

This is a JSP page, in fact, JSP is the servlet's draft file, why do you say so? Each JSP page corresponds to a servlet instance, and at compile time, the compiler reads the JSP page into the servlet instance. Let's look at the code for the servlet instance corresponding to this JSP:

      Out.write ("\ n");Out.write ("\ n");Out.write ("Out.write ( "out.write ( "<title></title>\n"); out.write (out.write ( "<body>\n"); out.write ( "\t\t<p>"); //<p> out.print ( "Hello, World "); //<%= "Hello,world"%> out.write (  "</p>\n"); //</p> out.write ( "</ Body>\n "); out.write ( "     

      for the normal HTML page of the JSP content of the label, directly as a string output, and for the JSP syntax part, get the servlet in the execution of the result output. For the entire process, we only need to know that all the content in the JSP page will be read by a servlet in the compiler compile phase, and the HTML code in it is returned as a string, for the JSP syntax, after execution. In essence, although the user is requesting a JSP page, it is the servlet that returns the result to the user.
      Then someone would ask, since they are all using the servlet to return the results, there is a JSP and no time, where is the efficiency? We need to be clear that before the JSP, all the HTML code in the servlet is written by the programmer, with the JSP page (equal to the template), the compiler helps us to read the JSP into the servlet work, we only need to care about the layout of HTML elements. The above is the relationship between JSP and servlet, I do not know whether I understand, but in order to better understand the content behind, it is recommended that you feel good.

Second, basic syntax for JSPs
      before you introduce the basic syntax of a JSP, I'd like to show you how each folder on our Tomcat server works. (Suppose you use the Tomcat server)

      This is the basic file on the Tomcat 9 server. We pick a few frequently used, the first WebApps, this directory is all your Web application, that is, the total folder of the site. The second is the work directory, which is stored in the corresponding servlet class for all the JSP files used in each Web application, and we have said that each JSP file will have a corresponding servlet class that they are stored in. Includes source code. Java and compiled. class files, in fact, many people think that JSP seems to have nothing to do with object-oriented, it is not, because each servlet is a Java class, otherwise how to execute java script. (See for a moment, the following will continue to be introduced) the third directory is the Conf directory, It holds an important file, Web. XML, which is a server configuration file that defines the default page (index.jsp,default.jsp, and so on) for your website, which is the default access page for any page you can enter directly. Some of the other directories, and so on when used to say it.
      now to introduce the basic syntax of the JSP, there will be three methods in each servlet class, _jspinit (), _jspdestroy (), _jspservice (). The first method is used to initialize the servlet without our concern, and the second method is used to destroy the Servlet method, which we don't care about for the time being. The focus is the third method, this is the JSP page all the content is read the destination, this method is mainly used in response to user requests, return HTML page back, remember this method, we will use later. The first JSP syntax to be introduced is the comment.

<%--这是jsp注释--%><!--这是html注释-->

The syntax of the comment is much like the HTML comment syntax, a small detail, the HTML comment is visible in the source code, and the JSP comment you do not see in the source code, that is, the JSP comments are not returned to the browser.
The second syntax , an output expression. <%= expression%>

<html>  <head> <title></title> </head> <body> <%="hello,world"%> </body></html>

Can be a constant expression or a variable expression. It can also be a return value for a function.

A third syntax , a JSP declaration. <%! Statement Content%>

<html> <head> <title ></title> </ head> <body> <%! public int ID; public intshowid () {return this.id; }%> </body></ HTML>             

Let's open the Servlet class and see

PublicFinalClassindex_jspExtendsOrg.Apache.Jasper.Runtime.HttpjspbaseImplementsOrg.Apache.Jasper.Runtime.Jspsourcedependent,Org.Apache.Jasper.Runtime.Jspsourceimports {Where do instance variables and instance methods come from?Publicint id;PublicIntShowid(){ReturnThis.id; }How to respond to requestsPublicvoid_jspservice(Final Javax.servlet.http.HttpServletRequest request,Final Javax.servlet.http.HttpServletResponse response)Throws Java.io.IOException, Javax.servlet.ServletException {...try {response.setcontenttype ("Text/html;charset=utf-8"); PageContext = _jspxfactory.getpagecontext (This, request, response,Nulltrue, 8192, true); _jspx_page_ context = PageContext; application = Pagecontext.getservletcontext (); Config = Pagecontext.getservletconfig (); Session = Pagecontext.getsession (); out = Pagecontext.getout (); _jspx_out = out; Out.write ("  " "<title></ Title>\n "); Out.write ( " "<body>\n"); Out.write ( "\t\t") out.write ( "\ n"); Out.write (" </body>\n "); Out.write ( "       

      from the servlet source code above, we can also see that any variable or method declared in a JSP will become a member of the corresponding instance of the Servlet class. Let's take an intuitive feel from an example.

 <html> Span class= "Hljs-tag" ><head> < Title></title> </head> <body> <%! public int ID;%> <%=id++%> </ body></HTML>    

After this piece of code executes, the value of the page output is added one at a time each refresh. This is because the ID is a member variable corresponding to the servlet instance, and the instance is not destroyed and the ID is saved. It is equivalent to you in the _jspservice () method output the value of the ID, the ID plus 1, as long as the JSP page is not modified, the corresponding instance will not recompile the build, the ID will not be reset because of the refresh.

The fourth syntax is a JSP script. We can use the Java for loop in the JSP page, if,else judgment, etc., as long as the Java syntax allows, JSP page can be written. Let's look at an example:

<html> < head> <title></title> </head> << Span class= "Hljs-name" >body> <%for (int a=0;a<10;a++) {%> <p>walker</p> << Span class= "Hljs-name" >%}%> </body> </HTML>          

This syntax may be used frequently in our actual projects, such as I want to enumerate all the user information in the database, we can use the loop output, the basic format of the front-end to you, you just have to replace the corresponding position with the variable. Like this:

<html> <head> <title ></title> </ head> <body> <%foreach (String name in <%=returnlist ()%>) {% > <p>name</p> <%}%> </< Span class= "Hljs-name" >body></HTML>     

Suppose a returnlist method is written in the background to return information about everyone in the database. You'll find that writing saves a lot of code, so you don't have as many P tags as you have.

Java EE Basics jsp

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.