JSP pages include Java program snippets Scriptlet and JSP tags in traditional webpage HTML files. When the Servlet/JSP Container receives a request from the client, it first executes the program fragment and then returns the execution result to the client in HTML format.
Basic Syntax:
Note:
1. Comments displayed on the client: <! -- Comment [<% = expression %>] --> (you can add a dynamic expression to the annotation)
2. special comments for developers: <% -- comment -- %> or <%/** this is a comment **/%>
Statement:
- <%!declaration; [declaration;]+...%>
The declared variable is a class variable. This means that if n users simultaneously execute this JSP page, they will share this variable. It will cause thread synchronization troubles. Not recommended)
Script:
<% Java code %>
The variables defined in this class are local variables, and there is no multi-thread synchronization problem, which is thread-safe.
Expression:
<% = Expression %> ';' cannot be used as the end symbol of the expression.
JSP command element:
It only tells the JSP Engine how to compile the JSP page.
Page command: Set attributes and related functions of the entire JSP page.
- <%@page attribute1="value1" attribute2="value2" ...%>
- <jsp:directive.page attribute1="value1"...>
For example:
- <%@page contentType="text/html; charset=utf-8" language="java" import="java.net.*"%>
Include command: Used to instruct the JSP Container to insert a file containing text or code During JSP compilation. The inclusion process is static. Note: Avoid using
- <%@include file="relativeURLspec"%>
- <jsp:directive.include file="relativeURLspec">
For example:
- <%@include file="Hello.html">
Taglib command:
- <%@taglib uri="tagLibraryURI" prefic="tagPrefix"%>
- <jsp:directive.taglib uri="tagLibraryURI" prefic="tagPrefix">
JSP operation elements:
- <jsp:include>
- <jsp:forward>
- <jsp:param>
- <jsp:useBean>
- <jsp:setProperty>
- <jsp:getProperty>
Submission of request object and network information:
JSP provides a request object to receive data sent from the client.
◆ URL Syntax: protocol: // host: port/virtualPath? QueryString
◆ Protocol: indicates the protocol used to declare the low-level mechanism for transmitting information between remote machines, such as http ftp and https ).
◆ Host: The name or IP address of the remote machine to which the request is sent.
◆ Port: indicates the port number of the server to listen to the request.
◆ VirtualPath: contains a group of identifiers separated by a slash. The server maps it to a physical path and a JSP location.
◆ QuerString: a query string. It is a list of paired names and values and is passed as a JSP parameter to the JSP page for processing it. If multiple parameters need to be passed, the names and values of the pairs are separated.
Method provided by the request object:
Obtain the path and protocol information:
- String getProtocol () gets the protocol and version used for communication, for example, HTTP/1.1)
- String getScheme () obtain the protocol name in the request, for example, HTTP)
- String getPathInfo () gets additional information between the JSP path in the request and the query String.
- String getContextPath ()
- String getRequestURI ()
- String getServletPath ()
- String getRealPath (String path)
Obtain host and port information:
- String getServerName () gets the name of the server responding to the request.
- Int getServerPort () gets the server host port number that responds to the request.
- String getLocalName () gets the server host name that responds to the request.
- String getLocalAddr () gets the server address of the Response Request.
- Int getLocalPort () gets the server port that responds to the request.
- String getRemoteAddr () gets the IP address of the client sending the request.
- String getRemoteHost () gets the client host name that sends the request.
- Int getRemotePort () gets the client host port that sends the request.
Obtain the query string:
String getQueryString () gets the parameter String passed by the get () method, that is, in the URL? .
Get the variable value in the query string:
- String getParameter (String name) obtains the parameter value sent from the client to the server.
- Enumeration getParameterNames () returns a set of all parameters in the request.
- String [] getParameterValues (String name) obtains all values of the specified parameter in the request.
For example:
- Http: // localhost: 8080/liuxl/jsp/querystring. jsp? Str = JSP
- String s = request. getParameter ("str"); get the value of the variable str. The value of s after execution is "JSP ".
Note: The Return Value of the getParameter () method can only be a string. If val = 100 is passed, type conversion is required.
- If (request. getParameter ("val ")! = Null) // determines whether the string is null.
- {
- Num = Integer. parseInt (request. getParameter ("val"); // converts a string to an Integer.
- }
- Else
- {
- Num = 0;
- }
-
- If (request. getParameter ("val ")! = Null) // determines whether the string is null.
- {
- Num = Integer. parseInt (request. getParameter ("val"); // converts a string to an Integer.
- }
- Else
- {
- Num = 0;
- }
Form and Its Application in information transmission:
Form:
Is an area that can contain form elements. HTML provides a variety of graphical user interface component elements such as text boxes, password boxes, and drop-down menus for forms ), these form elements allow users to input information in the form, which can be used to form content, as fields that users can enter, and can be submitted to a JSP for processing.
Format:
- <form name="" action="" method="">
- ...
- </form>
Where: name is used to specify the form name. Action specifies the action to be executed when a form is submitted. Method specifies the methods used to pass requests to jsp get, POST, PUT, and common POST ).
1. <input> tags are often used for input or submission.
Syntax: <input type = "" name = "" value = ">
Description: type allows you to specify the input types of text (text box), password (password box), checkbox (check box), radio (single choice button), image (image domain), and reset (reset button), submit (submit button )). Name is the name of the form element. Value is the initial value of the form element.
For example:
- Text Box: <input name = "" type = "text" value = "" size = "" maxlength = "">
- Password box: <input name = "" type = "password" value = "" size = "" maxlength = "">
- Submit button: <input name = "" type = "submit" value = "">
- Reset button: <input name = "" type = "reset" value = "">
- Single button: <input name = "" type = "radio" value = "" checked>
Select an option from a group of options. checked is an optional attribute. If this attribute exists, it indicates that the button is selected when it is displayed. Otherwise, it is not selected .)
- E. g: <input name = "radiobutton" type = "radio" value = "1" checked> Option 1
- <Input name = "radiobutton" type = "radio" value = "2"> Option 2
When button 1 is selected, the submit URL is as follows :...? Radiobutton = 1...
- Check box: <input name = "" type = "checkbox" value = "" checked>
Used to select multiple options from a set of options. checked is the same as above. Similar to single-choice buttons, check boxes are often used in groups. Unlike single-choice buttons, multiple selected check boxes are submitted, so their names can be the same (processed by array ), it can also be different (Processing Based on different variables ))
- e.g:<input name="checkbox" type="checkbox" value="swim">swim
- <input name="checkbox" type="checkbox" value="run">run
If both are selected during submission, the URL format is as follows :...? Checkbox = swim & checkbox = run...
2. The <textarea> label declares a region where a user can input multiple lines of text.
Syntax: <textarea name = "" rows = "" cols = ""> text </textarea>
(Cols is the number of characters in the text field width), rows is the number of lines in the text field height)
3. The <select> label declares an optional list. You can select one or more options.
- <select name="" size="" multiple>
- <option value="" selected>option</option>
- ...
- <option value="">option</option>
- </select>
Description: name is the name, size is the number of options displayed in the height of the List area), and multiple is an optional attribute. If yes, multiple options can be selected. Selected is an optional attribute.