JavaWeb_JSP01

1. What is the difference between JSP and HTML?

Answer: A JSP page usually consists of four basic elements: HTML tags, JSP comments, Java script elements (statements, Java program pieces, and Java expressions) and JSP tags (instruction tags, action tags, and custom tags)

  1. HTML runs on the client side and is interpreted and executed by the browser; JSP runs on the server side and requires a web container to provide a running environment.

  1. HTML focuses on static data display and generates static web pages; JSP focuses on interacting with servers and is mainly developed with dynamic web pages

  1. HTML loads faster than JSP.

The HTML page is a static page, that is, it is written by the user in advance and placed on the server, and sent by the Web server to the client;

A JSP page is an HTML page generated in real time by the JSP container executing the Java code part of the page, so it is a server-side dynamic page.

Java program: <% java code%>

Declaration of member variables and methods: <%! Variable or method definition %>

Java programs have the following characteristics:

① Call the method declared by the JSP page

② Manipulate the member variables declared by the JSP page

③ Declare local variables

④ Operate local variables

Java expression: <%= expression %>

① You cannot insert a statement between <%= and %>, that is, the end of the input content cannot end with a semicolon

②<%= is a complete symbol, there cannot be a space between <% and =

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

<jsp:include page="a.jsp"/><!--JSP action tag-->

<%!

int i=0;

int add(int x, int y){

return x+y;

}

//Data declaration and method declaration are here

%>

<%

//Java program piece

i++;

int result=add(1,2);

%>

The value of i is <%=i%> <%--Java expression--%>

2. What are the three types of JSP annotations?

Answer: <!--HTML comment-->, <%--JSP comment--%>, Java comment (//, /* */)

3. What is the format of the JSP expression? What does it do?

Answer: Java expression: <%= %>, the value of the expression is calculated by the web server, and the calculation result is sent to the client in the form of a string to be displayed as the content of the HTML page

4. Can a method be defined in a JSP program segment? Why?

Answer: The method cannot be defined, and the JSP code becomes a code segment in a method in the Servlet class when it is compiled.

5. What is a JSP statement? What is it for? What should be paid attention to when using it?

Answer: <%! %> is the declaration of JSP, you can declare method functions, attributes, and global variables in <%! %>. Do not declare local variables

basic knowledge:

①The essence of JSP is a Servlet. Before running, JSP will be translated into a .java file by the Tomcat server, and then the .java file will be compiled into a .class file. When we access jsp, it is the translated class that processes the request.

②<%%> is called a script fragment, and the code content written in it will be translated in the doservice method of the Servlet. Obviously, we can define local variables or call other methods in the doservice method, but not in the Service

Then define other method functions, that is, we can define local variables or call methods in <%%>, but we cannot define methods. There can be multiple script fragments in a jsp page, but the integrity of multiple script fragments must be ensured

③<%!%> is called a statement, and the content written in it will be directly translated in the Servlet class, because we can define methods, attributes and global variables in the Servlet class, so we can declare methods in <%!%> functions, properties, global variables

④<%=%> is called jsp expression, which is used to output declared variables or expressions to html web pages

⑤The code written directly in the jsp page <body></body> is called a template element, and will be output in out.write() in the Servlet's doservice method. So you can use out.write() or out.println() to output content to the page in JSP

6. What is the format of URL passing value? How to get parameters?

Answer: URL? Parameter name 1=value 1¶meter name 2=value 2&......

request.getParameter();

The role of the URL:

HTTP is an application layer protocol, a stateless protocol based on request/response mode

The Web page itself cannot pass information to the next page. If the next page needs to know the value in the page, unless it passes through the server

Transferring data between web pages is an important function of web programs (through the server)

URL pass value:

Just give some parameters after the URL

URL? Parameter name 1 = value 1 & parameter name 2 = value 2 &  …

Function: the latter page can obtain one or more parameter values ​​from the previous page

https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=jsp

http://localhost:8080/Prj04/page.jsp? m=3&n=5

Get URL parameters

String variable=request.getParameter("parameter name");

Returns the value of the corresponding parameter passed by the URL, and the result is of type String

Note: If the URL does not pass parameters or the parameter name is wrongly written, the return result is null, so when writing a program, it is usually necessary to judge whether it is null, otherwise an exception may be thrown

Precautions for passing URL values:

limitation:

The data passed by value can only be a string, and there are certain restrictions on the data type

The value of the transmitted data will be seen in the browser address bar. For data with strict confidentiality requirements (such as passwords), the URL method should not be used to transfer values.

Advantage:

Simplicity and diversity of platform support (no browser does not support URL), many programs still use URL to pass values ​​more conveniently

JSP directive tag

Commonly used JSP instructions include page and include instructions

<%@page attribute 1="" attribute 2=""...%>

The main attributes of the page directive are contentType, import, language and pageEncoding

contentType: The attribute defines a value to determine the MIME type of the response (MIME type is the type of way to set a certain file to be opened with a corresponding application program),

Such as contentType="application/msword;charset=UTF-8", the browser uses the word application to open the user request

Common MIME types are text/html, text/plain (normal text), application/pdf, application/msword, image/jpeg, image/png, image/gif, application/vnd.ms-powerpoint

import: The role of the attribute is to introduce the class in the package into the JSP page

language: The attribute is used to specify the scripting language used by the JSP page, currently java

pageEncoding: charset in contentType refers to the encoding of the webpage content seen when the server sends it to the client's browser; pageEncoding refers to the encoding used when the JSP file itself is stored

include directive: <%@include file="URL of the file"%> (static embedding)

JSP action tag

include、forward、param、useBean、getProperty和setProperty

<jsp: ></jsp>或<jsp: />

<jsp:include page="URL of the file"/> Function: Embed other files into the current JSP page (dynamic embedding)

<jsp:forward page="URL of the file"/> Function: stop the execution of the current JSP page from where the tag appears, and turn to execute the JSP page specified by the page attribute value in the forward action tag

The action tag param cannot be used independently, but it can be used as a sub-tag of the include and forword action tags, which pass parameters to the corresponding page in the form of "name-value" pairs

<jsp: parent tag page="receives the URL of the parameter page">

<jsp:param name="parameter name" value="parameter value">

</jsp:parent tag>

Parent tags are: include, forword

The page that receives parameters can use the built-in object request to call the getParameter("parameter name") method to obtain the parameter value passed by the action tag param

7. What are the instructions and actions of JSP?

Answer: Instructions: include, page

Actions: include, forward, param, useBean, getProperty, and setProperty

8. What are the functions of the import and contentType attributes of the page command?

Answer: import: The function of the attribute is to import the class in the package of the JSP page; contentType: The attribute defines a value to determine the MIME type of the response

9. What is the format and function of the include command? What should be paid attention to when using it?

Answer: <%@include file="URL of the file"%>,

The function of the include instruction tag is to statically embed JSP files, HTML files or other files into the current JSP web page

Static embedding means "include first and then process", and the embedding of files is completed during the compilation phase. That is to merge the current JSP page and the file to be embedded into a new JSP file,

Then the JSP engine converts the new page into a Java file for processing and running

10. What is the format and function of the include action? What should be paid attention to when using it?

Answer: <jsp:include page="URL of the file"/>

The function of the include action tag is to dynamically embed JSP files, HTML webpage files or other file texts into the current JSP webpage

Dynamic embedding is "processing first and then including", and the embedding of files is completed during the running phase. That is, when the JSP page is translated into a Java file, the two pages are not merged, but the file introduced in the include action tag is processed when the bytecode file of the Java file is loaded and executed.

Compared with static embedding, dynamic embedding is slightly slower but more flexible

11. What is the difference between the include action and the instruction?

Answer: There are different embedding methods, one is dynamic embedding and the other is static embedding

12. What is the format and function of the forward action?

Answer: <jsp:forward page="the URL of the file"/> Function: stop the continued execution of the current JSP page from where the tag appears, and turn to execute the JSP page specified by the page attribute value in the forward action tag

13. How do include and forward actions pass parameters?

The action tag param cannot be used independently, but it can be used as a sub-tag of the include and forword action tags, which pass parameters to the corresponding page in the form of "name-value" pairs

<jsp: parent tag page="receives the URL of the parameter page">

<jsp:param name="parameter name" value="parameter value">

</jsp:parent tag>

Parent tags are: include, forword

Guess you like

Origin blog.csdn.net/weixin_47940048/article/details/129597519