Objective: To understand the document structure of Web applications, the operating principles of JSP, and the simple input and output of JSP. Main Content: l introduces the structure and running principle of Web application documents through a simple example; l introduces basic input and output through a simple registration function. Next course content: client verification. 1. Each application in the document structure has a root directory, such as CH2. Theoretically, it can be placed anywhere, but it needs to be configured. A simple method is put directly under the webapps directory, applications in this directory will be automatically loaded. There is a WEB-INF directory under the root directory, the files in this directory cannot be remotely accessed, mainly stores configuration files and class files, resource files. The configuration file in the WEB-INF is Web. XML, and each web application has such a configuration file. There are two files in the WEB-INF used to store class files and resource files, lib and classes, lib stored in the form of a compressed package jar library, classes directly store class files (including package information ). Page files (including JSP files, HTML files, image files) can be placed under the root directory (CH2), or the following subfolders (not in the WEB-INF. 2. Run Mode Access: http: // 192.168.0.222: 8080/CH2/ch2.jsp. Prerequisites: deploy the web application on the server and start the server. The following uses ch2.jsp as an example to introduce the access process: 1) the client sends a request through a browser; 2) the Web server receives the request and transfers it to the application server; 3) the application server looks for the files to be accessed by the customer. Assume that the accessed file is ch2.jsp. There are two situations: first access: the application server converts the JSP file into a Java file; compile the file into a class file, load the class, instantiate the object, and initialize it. Then visit: The Page Object corresponding to the JSP file already exists, and you can find the object directly. 4) the Application Server encapsulates the request information and then calls the corresponding method. 5) the application server passes the method execution result (in response to the customer's content) to the Web server. 6) the Web server sends the result to the client. 7) the client browser parses the received HTML code into a webpage. This is what we see. The following are the file content during the running process. Contents of the source file ch2.jsp: dddddddddddddddddddddddd <% = "ffffffffffffff" %> the converted file ch2_jsp.java: Package Org. apache. JSP; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; public final class ch2_jsp extends Org. apache. jasper. runtime. httpjspbase implements Org. apache. jasper. runtime. jspsourcedependent {Private Static Java. util. LIST _ jspx_dependants; private javax. el. expressionfactory _ el _ Expressionfactory; private Org. apache. annotationprocessor _ jsp_annotationprocessor; public object getdependants () {return _ jspx_dependants;} public void _ jspinit () {_ el_expressionfactory = jspfactory. getdefafactory Factory (). getjspapplicationcontext (getservletconfig (). getservletcontext ()). getexpressionfactory (); _ jsp_annotationprocessor = (Org. apache. annotationprocessor) getservletconfig (). getservl Etcontext (). getattribute (Org. apache. annotationprocessor. class. getname ();} public void _ jspdestroy () {} public void _ jspservice (httpservletrequest request, httpservletresponse response) throws Java. io. ioexception, servletexception {jspfactory _ jspxfactory = NULL; pagecontext = NULL; httpsession session = NULL; servletcontext application = NULL; servletconfig Config = NULL; jspwriter Out = NULL; object page = This; jspwriter _ jspx_out = NULL; pagecontext _ jspx_page_context = NULL; try {_ jspxfactory = jspfactory. getdefafactory Factory (); response. setcontenttype ("text/html"); pagecontext = _ jspxfactory. getpagecontext (this, request, response, null, true, 8192, true); _ jspx_page_context = pagecontext; application = pagecontext. getservletcontext (); Config = pagecontext. getservletconfi G (); Session = pagecontext. getsession (); out = pagecontext. getout (); _ jspx_out = out; out. write ("dddddddddddddddddddddddddddd/R/N"); out. print ("ffffffffffffffff");} catch (throwable t) {If (! (T instanceof skippageexception) {out = _ jspx_out; If (OUT! = NULL & out. getbuffersize ()! = 0) Try {out. clearbuffer ();} catch (Java. Io. ioexception e) {} If (_ jspx_page_context! = NULL) _ jspx_page_context.handlepageexception (t) ;}} finally {If (_ jspxfactory! = NULL) _ jspxfactory. releasepagecontext (_ jspx_page_context) ;}} the information returned to the client (you can view the source file function in the browser): ddddddddddddddddddddddddffffffffffffff 3. Stateless request response mode the user sends a request through the client, you can send Event requests through the address bar, hyperlink, button, or form element. No matter how requests are sent, these request information will be encapsulated into an httpservletrequest object. The server will use this object as a parameter to call the Page Object. After this method is executed, it will respond to the client, then the httpservletrequest object is deleted. If you send the request again, a new httpservletrequest object is created, and the information of the last access does not exist. Therefore, the server does not save the information previously accessed by the client, which is called the stateless request response mode. Next we will introduce the basic question of JSP technology: input and output. First, let's see how to complete the input. 4. input element input is completed through the form element. Common form elements are as follows: 1) to submit information for a form, a form is required first. Only information in the form can be submitted. Main attribute of the Start identity <form> end identity </form>: Action attribute: the location of the target file, to whom to submit for processing; Method attribute: Request Method, get and post. Note: form cannot be nested. 2) basic syntax format of the single-line text box: <input type = "text" name = "username" value = "Enter the user name"> type = "text" indicates that this is a single-line text box; name indicates the name of the text box, which is very important. On the server, you need to set the value based on the name; value indicates the initial value. 3) basic syntax format of the password box: <input type = "password" name = "userpass"> the usage is basically the same as that of a single-line text box. 4) basic syntax format of hidden fields: <input type = "hidden" name = "userpass"> it is used to pass values between multiple pages, which is basically the same as the usage of the text box on the current line. 5) radio button syntax format: <input type = "radio" name = "sex" value = "male"> male <input type = "radio" name = "sex" value = "female"> female the names of Single-choice buttons should be consistent, this ensures that only one option is selected. Note: The content displayed after the single-choice button has no relationship with the single-choice button. It only tells the user what the single-choice button represents. 6) check box syntax format: <input type = "checkbox" name = "fav" value = "Music"> <input type = "checkbox" name = "fav" value = "Sports"> the values should also be consistent, you can use a uniform value. 7) syntax format of the drop-down list: Start ID: <select name = "select"> end ID: </SELECT> each option in the drop-down box: <option value = "1"> displayed information </option> gender drop-down list: <select name = "sex"> <option value = "male"> male </option> <option value = "female"> female </option> </SELECT> 8) syntax format of multi-line text fields: <textarea name = ""> sdsfsddddddddd </textarea> to initialize the text fields, you must place the initial values between the start and end tags of the tag. Note: This is different from the value assignment of other elements. 9) The submit button <input type = "Submit" value = "Submit"> generally, no name is required. 10) the reset button <input type = "reset" value = "reset"> usually does not require a name. 11) normal buttons can also be used to submit forms. JavaScript code is required. Syntax format: <input type = "button" value = "Submit" onclick = "…"> 5. Enter an instance: refer to the Code register on the registration page. JSP: <% @ page contenttype = "text/html; charset = gb2312 "%> register <br> <form method =" Post "name =" fi1 "Action =" process. JSP "> User ID: <input type =" text "name =" userid "> <br> password: <input type = "password" name = "userpass"> <br> confirm the password: <input type = "password" name = "userpass1"> <br> gender: <input type = "radio" name = "sex" value = "male" Checked> male <input type = "radio" name = "sex" value = "female"> female <BR> Hobbies: <input type = "checkb Ox "name =" fav "value =" "> motion <input type =" checkbox "name =" fav "value =" Music "> music <input type =" checkbox" name = "fav" value = "programming"> programming <br> education level: <select name = "degree"> <option value = ""> Bachelor's Degree </option> <option value = ""> Master's Degree </option> <option value = "Emy "> specialist </option> <option value =" "> PhD </option> </SELECT> <br> remarks: <textarea name = "comment"> </textarea> <br> <input type = "Submit" value = "Submit"> <input type = "reset" value = "Reset "> </Form> This page can complete the submission of user information. After the user enters and selects the page, click Submit. the browser sends the request to the server, based on the value of the action attribute in form, we know that the server will call process. JSP. The following describes how to compile process. jsp to obtain user input information. 6. When getting information, we mentioned earlier that the customer's request information, including the input and selection information, will be encapsulated in the httpservletrequest object, so in process. in JSP, you only need to access this object. How can you get this object? Several internal objects are provided in JSP, one of which is request. You can directly use this object. For internal objects, we can use them directly without the need to declare and instantiate them. To obtain request information, you can use the following two methods: getparameter (element name) getparametervalues (element name). The former is used to obtain the value of a single-value element, such as a text box, single-choice button, and password box. The latter is used to obtain the value of multi-value elements, such as check boxes and list lists that allow multiple selections. 7. instance: the registration information displays process. source file of JSP: <% @ page contenttype = "text/html; charset = gb2312" %> the registration information is as follows: <% string userid = request. getparameter ("userid"); string userpass = request. getparameter ("userpass"); string userpass1 = request. getparameter ("userpass1"); string sex = request. getparameter ("sex"); // encode and convert gender sex = new string (sex. getbytes ("8859_1"); string [] fav = request. getparametervalues ("fav"); // This method is used to obtain multi-value elements. String degree = request. getparameter ("degree"); string comment = request. getparameter ("comment"); out. println ("User ID:" + userid); %> <br> password: <% = userpass %> <br> Confirm Password: <% = userpass1 %> <br> gender: <% = sex %> <br> Hobbies: <% IF (fav! = NULL) for (string S: fav) {S = new string (S. getbytes ("8859_1"); out. print (s) ;}%> <br> Education: <% = degree %> <br> remarks: <% = comment %> note: code in this way (nesting of Java code and HTML code) is not recommended. This is just to make this program more complete. But the code for getting the value and encoding and conversion needs to be mastered and will be used in the servlet in the future. There is no change. 8. Practical Training: complete the page for adding a book and display the information you have added. Reference material: Basic tutorial on Java Web Programming