JSP program operation principle, document structure and simple input and output instance analysis _jsp programming

Source: Internet
Author: User

This paper illustrates the operation principle, document structure and simple input and output of JSP program. Share to everyone for your reference. Specifically as follows:

Goal:

Master the document structure of Web application;
Master the operating principle of JSP;
Master the simple input and output of JSP.

Main content:

This paper introduces the document structure and operation principle of Web application through a simple example.
Introduce basic input and output through a simple registration function.

Implementation content: Client-side validation.

1. Document structure

Each application has a root directory, such as CH2, which can theoretically be placed anywhere, but it needs to be configured, simple, and placed directly in the WebApps directory where applications are automatically loaded.
In the root directory there will be a web-inf directory, the files in this directory can not be remote access, the main storage configuration files and class files, resource files.

The configuration file in Web-inf is Web.xml, and each Web application will have such a configuration file.
In Web-inf, there are two files for class files and resource files, and Lib and Classes,lib store class libraries in the form of compressed package jars, classes directly to the class file (containing the package information).
Paging files (including JSP files, HTML files, picture files) can be placed under the root directory (CH2), or the following subfolders (cannot be placed in Web-inf).

2. Operation mode

Access mode: http://192.168.0.222:8080/ch2/ch2.jsp
Prerequisite: Deploy the Web application to the server and start the server.

The following is an example of the access process with ch2.jsp:

1 The client sends the request through the browser;
2 The Web server receives the request and then transfers it to the application server;
3 The application server looks for files to be accessed by the client, assuming that the file being accessed is ch2.jsp, in two ways:
First access: The application server converts the JSP file into a Java file, compiles it into a class file, then loads the class, instantiates the object, and initializes it;
Subsequent access: JSP file corresponding to the page object already exists, directly to find this object;
4 The application server encapsulates the request information and then invokes 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 this result to the client;
7 The client browser parses the received HTML code into a Web page. This is the result we see.
The following are the contents of several files during the run.

Contents of source file ch2.jsp:

Dddddddddddddddddddddddd
<%= "FFFFFFFFFFFFFFFF"%>

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) t Hrows 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"); The 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 (visible through the browser's view source file feature):

Dddddddddddddddddddddddd
Ffffffffffffffff

3, stateless request response mode

A user sends a request through the client, through an address bar, hyperlink, button, or event request for a FORM element. Regardless of how the request is sent, the request information is encapsulated into a HttpServletRequest object, which the server invokes as a parameter to the Page object, which will respond to the client at the end of the process. After that, the HttpServletRequest object was deleted. If you send the request again, a new HttpServletRequest object is created, and the information from the last visit does not exist.
Therefore, the server does not save the information previously accessed by the client, called a stateless request response mode.
Next, we introduce the basic problem of JSP technology: input and output. First look at how to complete the input.

4. Input elements

The input is done through the form element. The commonly used form elements are as follows:

1) Form

To submit information, you first need a form form, and only the information within the form can be submitted.
Start Identification <form>
End Logo </form>
The main properties:
Action attribute: is the location of the target file, to submit to whom to deal with;
Method attribute: Request mode, GET and post
Note: The form cannot be nested.

2) single-line text box

Basic syntax Format:

<input type= "text" name= "username" value= "Please enter user name" >
Type= "Text" indicates that this is a single-line text box;
The name of the text box, which is important when the server requires a value based on the name;
Value gives the initial value.

3) Password box

Basic syntax Format:
<input type= "Password" name= "Userpass" >
Usage is essentially the same as a single-line text box.

4) Hidden fields

Basic syntax Format:
<input type= "hidden" name= "Userpass" >
Used to pass values between multiple pages, as is the use of a 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 name of a set of radio buttons should be consistent so that multiple options can be selected only one.
Note: The content displayed after the radio button has nothing to do with the radio button, just telling the user what the radio button means.

6) check box

Syntax format:
<input type= "checkbox" Name= "fav" value= "Music" >
<input type= "checkbox" Name= "fav" value= "Sports" >
The values of the same set of checkboxes should also be consistent and can be taken in a uniform manner.

7) drop down list

Syntax format:

Start identification: <select name= "SELECT" >
End Identity:</select>
Each option in the Drop-down box: <option value= "1" > Display information </option>
Sex dropdown box:
<select name= "Sex" >
<option value= "Men" > Men </option>
<option value= "female" > Female </option>
</select>

8 Multi-line Text field

Syntax format:
<textarea name= "" >sdsfsddddddddd</textarea>
To initialize a text field, you need to place the initial value between the start and end tags of the label.
Note: This is not the same as other elements being assigned through the Value property.

9) Submit button

<input type= "Submit" value= "submitted" >
You don't usually need a name.

10) Reset button

<input type= "reset" value= "reset" >
You don't usually need a name.

11) Normal button

You can also complete the form submission through the normal button, and you need to write JavaScript code.

Syntax format:
<input type= "button" value= "Submit" onclick= ... ">

5, input Example: registration page

Reference Code register.jsp:

<%@ page contenttype= "text/html;charset=gb2312"%> please register <br> <form method= "POST" name= "Fi1" process.jsp "> User id:<input type=" text name= "userid" ><br> Password: <input type= "password" name= "Userpass" ><br> Confirm Password: <input type= "password" name= "Userpass1" ><br> Sex: <input type= "Radio" name= "Sex" val Ue= "male" checked> male <input type= "Radio" name= "Sex" value= "female" > <br> hobby: <input type= "checkbox" Name= "F AV "value=" movement > Movement <input type= "checkbox" Name= "fav" value= "Music" > Music <input type= "checkbox" Name= "fav" Value= "Programming" > Programming <br> Education: <select name= "degree" > <option value= "undergraduate" > Undergraduate </option> & Lt;option value= "Master" > Master </option> <option value= "Specialist" > Specialist </option> <option value= "Doctor" > Ph. D </option> </select><br> Remarks: <textarea name= "comment" ></textarea><br> & Lt;input type= "Submit" value= "submitted"<input type= "reset" value= "reset" > </form>

 

This page can complete the user information submission, when the user input and choose to click the Submit button, the browser will send this request to the server, according to the value of the action attribute in form, we know that the server will call process.jsp for processing. Here's how to write process.jsp to get user input information.

6, access to information

Before the introduction of the principle of operation, said that the customer's request information, including input and selection of information, will be encapsulated in the HttpServletRequest object, so in the process.jsp only need to access the object, how to get this object?
A number of internal objects are provided in the JSP, one of which is the request, which can be used directly from the object. For internal objects, we can use them directly, without having to declare and instantiate them.
The request information can be obtained through the following two methods:
GetParameter (element name)
Getparametervalues (element name)
The former is used to get values for a single value element, such as a text box, radio button, password box, and so on. The latter is used to get values for multivalued elements, such as check boxes, and list boxes that allow multiple selections.

7, Example: registration information display

Source files for process.jsp:

<%@ page contenttype= "text/html;charset=gb2312"%>
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 the gender to convert
  sex = new String (sex.getbytes ("8859_1"));
  string[] fav = request.getparametervalues ("fav");
  This method is used to get the value of a multivalued element
  String degree = request.getparameter ("degree");
  String comment = request.getparameter ("comment");
  OUT.PRINTLN ("User id:" +userid);
%>
<br> Password:<%=userpass%>
<br> Confirm password:<%=userpass1%>
<br> sex: <%= Sex%>
<br> Hobbies:
<%
  if (fav!=null) for
  (String s:fav)
  {
   s=new string ( S.getbytes ("8859_1"));
   Out.print (s);
  }
%>
<br> Academic:<%=degree%>
<br> notes:<%=comment%>

Note: This type of code (Java code and HTML code nesting) is not advocated, just to make this program more complete. But the code that gets the value and the coded transformation needs to be mastered, and will later be used in the servlet, with little change.

8, Training: Complete the book add interface, and the user added information to show again.

I hope this article will help you with the 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.