Java Foundation--jsp

Source: Internet
Author: User

First, the Origin of JSP

In many dynamic Web pages, most of the content is unchanged, only the local content needs to be dynamically generated and changed. For example, the number of times a news is viewed is changed dynamically, and the code that the Servlet program returns to the client is all dynamically created by the Java program. The disadvantage of Servlet, the processing interface is difficult. JSP is developed on the basis of servlet, which makes up the defects of servlet in interface processing.

In short, JSP =html+java fragment +jsp tag +javascript, it is powerful, can be combined with JavaBean

In addition, Jsp+javabeen+servlet constitutes the MVC pattern.

JSP is run on the server side, that is, we create a on the desktop. JSP file, it can not be executed oh.

We'll paste it into the WebApps file on tomcat (or you can create a. jsp file directly inside).

Next, we're going to start Tomcat. Then access in the browser: http://localhost:8080/myweb/Demo.jsp.

And then something magical happened. As you can see in Tomcat's WebApps, help us automatically generate two files.

second, the introduction of JSPExcerpt from Baidu: JSP full name is Java Server Pages, the Chinese name is Java Servers page, it is simply a simplified servlet design, it is advocated by Sun Microsystems Company, many companies involved in the establishment of a dynamic Web technology standards. JSP technology is a bit like ASP technology, it is in the traditional Web page HTML (standard Common Markup Language subset) file (*.htm,*.html) inserted Java program segment (scriptlet) and JSP tag (tag), thereby forming a JSP file, suffix named (*.jsp). Web applications developed with JSP are cross-platform and can run on Linux and other operating systems. It implements the Java extension in HTML syntax (in <%,%> form). JSP, like a servlet, is executed on the server side. Usually returned to the client is an HTML text, so the client can browse as long as there is a browser. JSP technology uses the Java programming language to write tags and scriptlets of class XML to encapsulate the processing logic that produces dynamic Web pages. Web pages can also access the application logic of resources that exist on the server through tags and scriptlets. JSP separates the Web page logic from the display of the web design, supports reusable component-based design, and makes the development of Web-based applications quick and easy. JSP (JavaServer pages) is a dynamic page technology whose main purpose is to separate the presentation logic from the servlet. Java servlet is the technical foundation of JSP, and the development of a large Web application requires Java servlet and JSP mates to complete. JSP has the simple and easy-to-use Java technology, completely object-oriented, platform-independent and safe and reliable, mainly for the internet all the characteristics. three, the simplest JSP program
<%     String userName= "Zhangsan"; %>       <script>      alert (  <% userName%></script>

Note: The process of accessing the JSP

If this is the first time the server is accessed, it is translated into a corresponding Java file (Servlet). It is then compiled into a. class file and loaded into memory.

In the case of later access, the in-memory JSP instance is called directly, so the first access is slow and later access is faster.

Four, 3 kinds of JSP annotations

1.

<%        //        * this */%>        

2.

<%--  // Such comment content will not be sent to the client    

3.

<!--<% Out.print ("I won't do what the boss told me to do");  %>   and   // content here is sent to the client, but the browser does not display

Five, JSP script elements (3 kinds)

1.

1) Code snippet <%%> it to strictly adhere to the Java language Specification, need to guide the package <%%> and   <%%> between the Java code can be accessed, equivalent to write in a  <%%>.

2.

2) Declaration <%!xxxxxxx%>   // Note <%! must not be in the middle of space <%!  Public String test () {return "OK:";} %>

3.

3) expression <%= xxx  %>    // Note <%= in the middle must not space, after the semicolon <%=string str= "Hee hee";%> Example: <label><%= str%></label>  // and the following notation equivalent <label><% out.print (str ); %></label>

Attached: MyEclipse generated JSP page, commentary:

(In the actual application, we can delete it if we don't need it)

<%@ page language= "Java"Import= "java.util.*" pageencoding= "Utf-8"%>//Explanation: Page directive, import used to guide the package, pageencoding refers to the current page encoding method<%String Path= Request.getcontextpath ();//Explanation: ContextPath represents the name of the Web App/shop-adminString basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; //Top to bottom justification://http//localhost//8080//Shop-admin//The result of the last generation above:->http://localhost: 8080/shop-admin/%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >//Explanation: statement type declaration//Commentary: <base href= "http://localhost: 8080/shop-admin/"><title>my JSP ' test.jsp ' starting page</title> <body> <meta http-equiv= "Pragma" con Tent= "No-cache" >//Explanation: Do not cache<meta http-equiv= "Cache-control" content= "No-cache" >//Explanation: Do not cache<meta http-equiv= "Expires" content= "0" >//Explanation: Do not cache<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >//Explanation: Keywords for Web bots to search for<meta http-equiv= "description" content= "This is my page" > <!--<link rel= "stylesheet" type= "text /css "href=" Styles.css "> </body> 

Vi. JSP version of the calculator (code example)

<%//Receive ParametersString num1 =request.getparameter ("NUM1"); String num2=request.getparameter ("num2"); String Flag=request.getparameter ("Flag"); intN_num1=0; intN_num2=0; intResult=0; if(num1!=NULL&&num2!=NULL&&flag!=NULL){    //Calculationn_num1=Integer.parseint (NUM1); N_num2=Integer.parseint (num2); if(Flag.equals ("+") {result=n_num1+n_num2; }Else  if(Flag.equals ("-") {result=n_num1-n_num2; }Else  if(Flag.equals ("*") {result=n_num1*n_num2; }Else  if(Flag.equals ("/") {result=n_num1/n_num2; } out.print ("); }            //Output Results%> <form action= "calcui.jsp" Name= "Form1" method= "Get" >Please enter the first number:<input type= "text" name= "NUM1" Value= ' <%=request.getparameter ("Num1") ==null? ": Request.getparameter (" NUM1 ") %> '/> <br/> <select name= "Flag" > <option value= ' + ' <%= "+". Equals (flag)? " Selected ":" "%>>+</option> <option value= '-' <%= '-". Equals (flag)? " Selected ":" "%>>-</option> <option value= ' * ' <%=" * ". Equals (flag)?" Selected ":" "%> >*</option> <option value= '/' <%="/". Equals (flag)?" Selected ":" "%>>/</option> </select> <br/>Please enter a second number:<input type= "text" name= "num2" Value= ' <%=request.getparameter ("num2")%> '/> <input type = "Submit" value= "calculation"/> </form>

VII. MVC pattern

M->mode model layer, Javabeen (e.g. Userdao,userinfo)

V->view view layer, HTML, JSP

C->controller control layer, by servlet

Java Foundation--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.