JavaWeb Road 03 - JSP

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_42267300/article/details/87468778

JSP Basics

JSP Overview

Full name JSP Java Server Page, is essentially a servlet, JSP is working to simplify the replacement of the emergence of Servlet. Although the Servlet to complete most of the work, but can not be embedded directly into HTML Servlet code output is too difficult and complicated deployment process. To overcome this difficulty, Sun has launched in early 1999 JSP1.0.

JSP includes many technologies, including Java Bean, custom labels (Custom Tags), EL expressions (Expression Language), JSTL standard tag library (Java Standard Tag Library), and so on.

A JSP file consists of three parts: java + HTML + JSP unique content.

JSP action

As a supplement to the Servlet, JSP in the process of generating the HTML code for a lot more convenient than Servlet and JSP no special deployment, only you need to be copied to the next server. Later JSP segment optimization and upgrading of its functions are more powerful.

In general, JSP role is to generate and display information on the content of the phase separation.

JSP workflow

When you first access the JSP file, the server load web.xml file.

It is of the Servlet jsp (servlet-name found by reflection org.apache.jasper.servlet. The JspServlet processing).

The server *. Jsp file is converted to produce the corresponding * _jsp.java file.

The latter is converted to the server * _jsp.class file operation result to generate a response to the server.

Then the response from the server to the browser, for resolution.

JSP life cycle

JSP is a Servlet, there will only be one instance running. Like with Servlet, JSP instance initialization, also called Servlet destroyed when init () method and destroy () method. In addition, JSP also has its own initialization method and destruction methods _jspInit () and _jspDestroy () , for example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!//注意这里是%!
	public void _jspInit() {
		//初始化时运行的代码
	}

	public void _jspDestory() {
		//销毁时运行的代码
	}
%>



JSP-specific content

JSP script

Here we describe three parts: the format of the Java program fragment format output expressions, statements format and method attributes.

Java program fragment

JSP script generation in service () method, you must use the "<%" and "%>" enclosed, otherwise it will be considered a template data. Internal write a Java program fragment, JSP script can appear anywhere in the JSP file, for example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        int num = 5;
        int result = 1;
        for (int i = 0 ; i < num ; i++) {
            result *= num;
        }
        out.println(num + "的" + num + "次方为" + result);
    %>
</body>
</html>

Results are as follows: Here Insert Picture Description
corresponding to the output to the client browser as HTML code:

<html>
<head>
    <title>Title</title>
</head>
<body>
    5的5次方为3125
</body>
</html>

Output expression

Generating output expressions are in service () method is used in the source program Out.println () method of output, the output similar to the Servlet. JSP may also use the "<% =" and "%>" output data of various types, including int, double, boolean, String, Object , for example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        int num = 5;
        int result = 1;
        for (int i = 0 ; i < num ; i++) {
            result *= num;
        }
    %>

    <%= num %><%= num %>次方为<%= result %>
</body>
</html>

Results are as follows:Here Insert Picture Description

Note: after the variable output can not have an output expression ";", call the object's output toString () method.


Statement methods and properties

JSP methods and properties can be declared (global variable), but not between the statement "<%" and "%>" nor in the "<% =" and "%>" Statement between. When using JSP and global variable declarations method further group of symbols "<%!" And "%>", for example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
	<%!
	    private int add(int firstNumber, int secondNumber) {
	        return firstNumber + secondNumber;
	    }
	
	    private int subtract(int firstNumber, int secondNumber) {
	        return firstNumber - secondNumber;
	    }
	
	    private int multiply(int firstNumber, int secondNumber) {
	        return firstNumber * secondNumber;
	    }
	
	    private double divide(int firstNumber, int secondNumber) {
	        return firstNumber * 1.0 / secondNumber;
	    }
	%>
	
	<%!
	    private String result = "";
	%>
	
    <form action="/xjm/demo1.jsp" method="post">
        <table>
            <tr>
                <td>firstNumber:</td>
                <td><input type="text" name="firstNumber" value="0" style="width: 100px;height: 20px"/></td>
            </tr>

            <tr>
                <td>secondNumber:</td>
                <td><input type="text" name="secondNumber" value="0" style="width: 100px;height: 20px"/></td>
            </tr>

            <tr align="center">
                <td><input type="submit" value="add" name="calculate" style="width:70px;height:20px"/></td>
                <td><input type="submit" value="subtract" name="calculate" style="width:70px;height:20px"/></td>
            </tr>

            <tr align="center">
                <td><input type="submit" value="multiply" name="calculate" style="width:70px;height:20px"/></td>
                <td><input type="submit" value="divide" name="calculate" style="width:70px;height:20px"/></td>
            </tr>

            <%
                String firstStr = request.getParameter("firstNumber");
                String secondStr = request.getParameter("secondNumber");
                String calculateStr = request.getParameter("calculate");

                if (firstStr != null && secondStr != null && calculateStr != null) {
                    int firstNumber = Integer.parseInt(firstStr);
                    int secondNumber = Integer.parseInt(secondStr);

                    if (calculateStr.equals("add")) {
                        result = firstNumber + " + " + secondNumber + " = " + add(firstNumber, secondNumber);
                    } else if (calculateStr.equals("subtract")) {
                        result = firstNumber + " - " + secondNumber + " = " + subtract(firstNumber, secondNumber);
                    } else if (calculateStr.equals("multiply")) {
                        result = firstNumber + " * " + secondNumber + " = " + multiply(firstNumber, secondNumber);
                    } else if (secondNumber == 0) {
            %>
            分母不能为零
            <%
                    } else {
                        result = firstNumber + " / " + secondNumber + " = " + divide(firstNumber, secondNumber);
                    }
                }
            %>

            <tr>
                <td align="center" colspan="2"><%= result %></td>
            </tr>
        </table>
    </form>
</body>
</html>

Results are as follows:
Here Insert Picture Description
Note:
1. There is an IF (firstStr = null && && calculateStr secondStr = null null =!!!) {} . It is to prevent the null pointer exception causes error 500
when 2. JSP in Java nested if statements in the HTML code required, if statement block must be used before and after "{" and "}." Similarly for loops, while loops, too, there will no longer see them here.

JSP comments

JSP comments Unlike HTML comments " <- - comment text!> , And Java annotations" " // comment text / * comment text * / / * comment text * / ", JSP comments in the following format:

<%--注释文本--%>

JSP directives

JSP directives (click Browse) to declare some properties such as JSP pages, such as coding, document type. JSP directive "<% @ start", "%>" from the end. JSP instruction format is as follows:

<%@ directive {attribute=value}* %>

Note An asterisk indicates there may be zero or more attributes. For example, instructions in the previous example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

The directive instruction page position, so that the instruction is a page of instructions. The command language and comprising two contentType properties. Common commands have page, taglib, include and so on.


JSP hidden objects

Servlet and JSP context data needs to be acquired servletContext objects, the object servletContext Servlet is effected by GetServletContext () Gets a method. The JSP is not defined getServletContext objects, but can be used directly. This is because the JSP is built getServletContext hidden objects ( the Implicit Object ) (Click to view) . JSP built nine hidden objects, such JSP Servlet more convenient to use than the more convenient.
JSP has a built-in hidden object out, request, response, config, session, application, page, pageContext, exception.


JSP behavior

JSP behavior ( JSP Actions ) (Click to view) is a built-in set of JSP tags, just write little markup code will be able to use the rich functionality provided by the JSP. JSP is an abstract behavior JSP function package, includes two, JSP custom behavior JSP standard behavior. JSP standard behavior in the following format:

<jsp:elements {attribute="value"}* />

A JSP behavior can be specified zero or more attribute pairs.


Guess you like

Origin blog.csdn.net/qq_42267300/article/details/87468778