Detailed description of JSP dynamic web page program design

Source: Internet
Author: User

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:

 
 
  1. <%!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.

 
 
  1. <%@page attribute1="value1" attribute2="value2" ...%>   
  2. <jsp:directive.page attribute1="value1"...>    

For example:

 
 
  1. <%@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

 
 
  1. <%@include file="relativeURLspec"%> 
  2. <jsp:directive.include file="relativeURLspec">  

For example:

 
 
  1. <%@include file="Hello.html">   

Taglib command:

 
 
  1. <%@taglib uri="tagLibraryURI" prefic="tagPrefix"%> 
  2. <jsp:directive.taglib uri="tagLibraryURI" prefic="tagPrefix">  

JSP operation elements:

 
 
  1. <jsp:include>   
  2. <jsp:forward>   
  3. <jsp:param>   
  4. <jsp:useBean>   
  5. <jsp:setProperty>   
  6. <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:

 
 
  1. String getProtocol () gets the protocol and version used for communication, for example, HTTP/1.1)
  2. String getScheme () obtain the protocol name in the request, for example, HTTP)
  3. String getPathInfo () gets additional information between the JSP path in the request and the query String.
  4. String getContextPath ()
  5. String getRequestURI ()
  6. String getServletPath ()
  7. String getRealPath (String path)

Obtain host and port information:

 
 
  1. String getServerName () gets the name of the server responding to the request.
  2. Int getServerPort () gets the server host port number that responds to the request.
  3. String getLocalName () gets the server host name that responds to the request.
  4. String getLocalAddr () gets the server address of the Response Request.
  5. Int getLocalPort () gets the server port that responds to the request.
  6. String getRemoteAddr () gets the IP address of the client sending the request.
  7. String getRemoteHost () gets the client host name that sends the request.
  8. 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:

 
 
  1. String getParameter (String name) obtains the parameter value sent from the client to the server.
  2. Enumeration getParameterNames () returns a set of all parameters in the request.
  3. String [] getParameterValues (String name) obtains all values of the specified parameter in the request.

For example:

 
 
  1. Http: // localhost: 8080/liuxl/jsp/querystring. jsp? Str = JSP
  2. 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.

 
 
  1. If (request. getParameter ("val ")! = Null) // determines whether the string is null.
  2. {
  3. Num = Integer. parseInt (request. getParameter ("val"); // converts a string to an Integer.
  4. }
  5. Else
  6. {
  7. Num = 0;
  8. }
  9.  
  10. If (request. getParameter ("val ")! = Null) // determines whether the string is null.
  11. {
  12. Num = Integer. parseInt (request. getParameter ("val"); // converts a string to an Integer.
  13. }
  14. Else
  15. {
  16. Num = 0;
  17. }

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:

 
 
  1. <form name="" action="" method="">   
  2. ...   
  3. </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:

 
 
  1. Text Box: <input name = "" type = "text" value = "" size = "" maxlength = "">
  2. Password box: <input name = "" type = "password" value = "" size = "" maxlength = "">
  3. Submit button: <input name = "" type = "submit" value = "">
  4. Reset button: <input name = "" type = "reset" value = "">
  5. 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 .)

 
 
  1. E. g: <input name = "radiobutton" type = "radio" value = "1" checked> Option 1
  2. <Input name = "radiobutton" type = "radio" value = "2"> Option 2

When button 1 is selected, the submit URL is as follows :...? Radiobutton = 1...

 
 
  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 ))

 
 
  1. e.g:<input name="checkbox" type="checkbox" value="swim">swim   
  2. <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.

 
 
  1. <select name="" size="" multiple>   
  2.      <option value="" selected>option</option>   
  3.          ...   
  4.      <option value="">option</option>   
  5. </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.

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.