JSP program running principle, document structure and simple input/output instance analysis, jsp instance analysis

Source: Internet
Author: User

JSP program running principle, document structure and simple input/output instance analysis, jsp instance analysis

This document describes the JSP program running principle, document structure, and simple input and output. Share it with you for your reference. The details are as follows:

Objectives:

Master the document structure of Web applications;
Master the running principles of JSP;
Master the simple input and output of JSP.

Main content:

A simple example is provided to introduce the document structure and running principle of Web applications;
The basic input and output are introduced through a simple registration function.

Implementation content: client verification.

1. Document Structure

Each application has a root directory, such as ch2. Theoretically, it can be stored anywhere, but it needs to be configured. in a simple way, it is directly stored in 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. Running Mode

Access Method: http: // 192.168.0.222: 8080/ch2/ch2.jsp
Premise: deploy the Web application on the server and start the server.

The following uses ch2.jsp as an example to describe the access process:

1) the client sends a request through a browser;
2) The Web server receives the request and forwards it to the application server;
3) The application server looks for the files to be accessed by the customer. Assume that the files to be accessed are ch2.jsp. There are two situations:
First access: the application server converts JSP files to Java files, compiles them into class files, loads classes, instantiates objects, and initializes them;
Subsequent access: the Page Object corresponding to the JSP file already exists. You can find this object directly;
4) The application server encapsulates the request information and then calls the corresponding method;
5) The application server transmits 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:

Dddddddddddddddddddddddddd
<% = "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.getDefaultFactory().getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();  _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().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 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.getDefaultFactory();   response.setContentType("text/html");   pageContext = _jspxFactory.getPageContext(this, request, response,   null, true, 8192, true);   _jspx_page_context = pageContext;   application = pageContext.getServletContext();   config = pageContext.getServletConfig();   session = pageContext.getSession();   out = pageContext.getOut();   _jspx_out = out;   out.write("DDDDDDDDDDDDDDDDDDDDDDDD/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);  } }}

Information returned to the client (you can view the source file in the browser ):

Dddddddddddddddddddddddddd
Ffffffffffffff

3. Stateless Request Response Mode

A user sends a request through a client, and can send an event request 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 Elements

The input is completed through the form element. Common form elements are as follows:

1) form

To submit information, you first need a form. Only information in the form can be submitted.
Start identity <form>
End id </form>
Main attributes:
Action attribute: the location of the target file, to whom to submit for processing;
Method property: Request method, including get and post
Note: form cannot be nested.

2) Single Row text box

Basic syntax format:

<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. It is very important to set a value based on the name on the server;
Value indicates the initial value.

3) Password box

Basic syntax format:
<Input type = "password" name = "userpass">
The usage is basically the same as that of a single-line text box.

4) Hide the domain

Basic syntax format:
<Input type = "hidden" name = "userpass">
It is used to pass values between multiple pages, which is basically the same as the usage of the line text box.

5) radio button

Syntax format:
<Input type = "radio" name = "sex" value = "male"> male
<Input type = "radio" name = "sex" value = "female"> female
The names of a group of radio buttons should be consistent, so that only one of the multiple options can be 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 of the check boxes in the same group should also be consistent. You can set the values in a uniform way.

7) drop-down list

Syntax format:

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 </option>
</Select>

8) multi-line text fields

Syntax format:
<Textarea name = ""> sdsfsddddddddd </textarea>
To initialize a text field, you need to place the initial value between the start and end of the tag.
Note: This is different from the value assignment of other elements.

9) submit button

<Input type = "submit" value = "submit">
Generally, no name is required.

10) reset button

<Input type = "reset" value = "reset">
Generally, no name is required.

11) normal button

You can also click the common button to complete form submission. JavaScript code is required.

Syntax format:
<Input type = "button" value = "Submit" onClick = "... ">

5. Enter an instance: registration page

Refer to the Code register. 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 = "checkbox" 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 the submit button. the browser will send this request to the server based on the value of the action attribute in the form, we know that the server will call process. jsp. The following describes how to compile process. jsp to obtain user input information.

6. Obtain information

As mentioned above, 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.
The following two methods can be used to obtain request information:
GetParameter (element name)
GetParameterValues (element name)
The former is used to obtain the values of Single-value elements, such as text boxes, single-choice buttons, and password boxes. 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: Registration Information Display

Source file of process. 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 the value of the multi-value element 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: This method is not recommended for code (nested Java code and HTML code). This is only to make this program 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.

I hope this article will help you with JSP program design.

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.