JSP basic syntax

Source: Internet
Author: User

Keep a record of the points of knowledge that you have reviewed, and it will look convenient later.

Instructions

Three instructions in JSP, Page,include,taglib

   page is used to define pages related information, such as the following code:

Import= "java.util.*,java.io.*" contenttype= "text/html; Charset=utf-8 "%>

Language defines the language used for the page;

The tool class used in the Import reference page;

ContentType defines the encoding format of the page;

Pageencoding defines the encoding format of the JSP.

  The inlcude directive is used to refer to other JSP pages; thetaglib directive is used to define tags, which are more commonly used in struts.

Comments

There are several comments in the JSP:

1 HTML version:

<!-- -

This kind of comment is visible when you look at the HTML code.

2 JSP version:

<% --  --%>

3 Java Edition:

// single-line comment ———————————————— shortcut key ctrl+/ /* */
Label

How to use JSP statements in HTML, there are several tags in the JSP:

1 defining variables, function labels

    <%!          = "Xingoo"; // declaring variables        int Add (int A,int b) {// Declare function            return a +b;        }     %>

The content defined under this tag is used as a variable and a method, and other parts of the JSP can be arbitrarily referenced.

2 JSP Body

    <%        out.println ("s=" +s+ "<br>");            Out.println (");        Out.println ("Not so simple <br>");         %>

This label is the primary logical processing area.

3 JSP output

    S=<%=s%><br> <%--expression does not end with a semicolon--%>    1+2=<%=add

This can directly output the value of the variable and the return value of the method.

Built-in objects in a JSP

There are some built-in objects in the JSP that can be used directly:

1 out Content

2 Request object, get information about the request

3 Response Answer object, set the content and information of the response

4 Session Sessions Object

5 Application

6 Page

7 PageContext

8 exception

9 Config

Let's talk about the first three, the rest of the back.

Out Object

The Out object can output information directly to the page, and the contents of the out are placed in the buffer and the buffer size is 8,192 bytes before the output to the page.

The following are common methods for out objects:

Buffer size: <%=out.getbuffersize ()%>byte<br> <%Out.println ("S=" +s+ "<br>"); Out.println ("); Out.println ("Not So simple <br>");        Out.flush (); //out.clear ();Out.clearbuffer (); Out.println ("You can find a chat <br>"); %>Buffer Size:<%=out.getbuffersize ()%>byte<br>buffer remaining size:<%=out.getremaining ()%>byte<br>whether to automatically empty:<%=out.isautoflush ()%>

The GetBufferSize () method is used to obtain the size of the current buffer;

The println () method is used to print content to the page;

The flush () method refreshes the buffer, brushes the contents of the buffer into the page, and empties the buffer;

The clear () method clears the buffer, and if the method is called after flush, an error is applied.

The Clearbuffer () method clears the buffer and does not error if flush.

The Getremaining () method is used to obtain the current remaining buffer size;

The Isautoflush () method is used to determine whether the buffer is automatically emptied;

Reference code:

<%@ page language= "Java"Import= "java.util.*,java.io.*" contenttype= "text/html; Charset=utf-8 "%> <%--languages used by language scriptsImportLoad class file ContentType encoding method--%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Buffer Size:<%=out.getbuffersize ()%>byte<br> <!--client Visible--<%--client not visible--%> <%!String S= "Xingoo";//declaring Variables        intAddintAintb) {//declaring functions            returnA +b; }    %>s=<%=s%><br> <%--expression does not end with a semicolon--%> 1+2=<%=add%><br>//single-line comment ———————————————— shortcut key ctrl+/        /*Multiline Comment ____________ shortcut key ctrl+shift+/*/Out.println ("S=" +s+ "<br>"); Out.println ("); Out.println ("Not So simple <br>");        Out.flush (); //out.clear ();Out.clearbuffer (); Out.println ("You can find a chat <br>"); %>Buffer Size:<%=out.getbuffersize ()%>byte<br>buffer remaining size:<%=out.getremaining ()%>byte<br>whether to automatically empty:<%=out.isautoflush ()%></body>View Code

Operation Result:

Request built-in objects

The request object, which is typically used for processing requests. For example, when a form is submitted, the contents of the form are extracted through the request object.

For example, the following registration form:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Read<input type= "checkbox" Name= "Favorite" value= "Music"/>Music<input type= "checkbox" Name= "Favorite" value= "movie"/>movie<input type= "checkbox" Name= "Favorite" value= "Game"/>Game</td> </tr> <tr> <td colspan= "2" ><input type= "Submit" Valu E= "Submit"/></td> </tr> </table> </form> <br> <br> &L T;a href= "Jsprequest.jsp?username=lisi" >url test request (en) </a><br> <a href= "jsprequest.jsp? Username= John Doe ">url test request (en) </a></body>View Code

You can use Setcharacterencoding ("") in a JSP to set the encoding format and get the contents of the specified name by GetParameter or Getparametervalues.

<%request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem//Unable to resolve URL pass in Chinese charactersRequest.setattribute ("Password", "12300"); %>User name:<%=request.getparameter ("username")%><br>Hobbies:<%if(Request.getparametervalues ("favorite")! =NULL) {string[] Favorites= Request.getparametervalues ("Favorite");  for(inti=0; i<favorites.length; i++) {out.println (Favorites[i]+ "&nbsp;&nbsp;"); }    }    %><br>Password:<%=request.getattribute ("password")%><br>MIME type:<%=request.getcontenttype ()%><br>protocol type:<%=request.getprotocol ()%><br>requested server name:<%=request.getservername ()%><br>Server port number:<%=request.getserverport ()%><br>character encoding:<%=request.getcharacterencoding ()%><br> <!--only Post can get to the number of bytes, and if it is get it will display-1--Length of request (bytes):<%=request.getcontentlength ()%><br>Client IP:&LT;%=REQUEST.GETREMOTEADDR ()%><br>the true path of the request:<%=request.getrealpath ("jsprequest.jsp")%><br>Context Path:<%=request.getcontextpath ()%><br>

The code for the sample is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem//Unable to resolve URL pass in Chinese charactersRequest.setattribute ("Password", "12300"); %>User name:<%=request.getparameter ("username")%><br>Hobbies:<%if(Request.getparametervalues ("favorite")! =NULL) {string[] Favorites= Request.getparametervalues ("Favorite");  for(inti=0; i<favorites.length; i++) {out.println (Favorites[i]+ "&nbsp;&nbsp;"); }    }    %><br>Password:<%=request.getattribute ("password")%><br>MIME type:<%=request.getcontenttype ()%><br>protocol type:<%=request.getprotocol ()%><br>requested server name:<%=request.getservername ()%><br>Server port number:<%=request.getserverport ()%><br>character encoding:<%=request.getcharacterencoding ()%><br> <!--only Post can get to the number of bytes, and if it is get it will display-1--Length of request (bytes):<%=request.getcontentlength ()%><br>Client IP:&LT;%=REQUEST.GETREMOTEADDR ()%><br>the true path of the request:<%=request.getrealpath ("jsprequest.jsp")%><br>Context Path:<%=request.getcontextpath ()%><br></body>View Code

Request Result:

  It is important to note that only the Post method can obtain the requested length.

Response Built-in objects

If you want to simulate the process of response, you can directly create a JSP as follows, and then request the page directly via http:

<%@ page language= "Java"Import= "java.io.*" contenttype= "text/html; Charset=utf-8 "%><%//set the encoding formatResponse.setcontenttype ("Text/html;charset=utf-8"); Out.println ("); //Out.flush ();//force output to ensure that the contents of the out output are before the output stream object of the responsePrintWriter outer=Response.getwriter (); Outer.println ("Response output Stream output object");//Print before out//response.sendredirect ("reg.jsp");//request redirect, need to comment out the previous flush method%>

Note that response has a PrintWriter object that, by default, prints the contents before the out-of-built object;

If you use Out.flush to force a flush buffer, you can print before printwriter.

Redirect and forward

Redirection is the method of response :

Response.sendredirect ("jsprequest.jsp"); // the new request, the data is gone.

Redirects are equivalent to a new request, but none of the data that was requested at the beginning. The URL address bar will also become the redirected address.

Forwarding is the method of request :

Request.getrequestdispatcher ("jsprequest.jsp"). Forward (Request,response); // server-side behavior

It transmits the data in the request, and the URL address bar does not change, as the user sees it as if it had not been forwarded.

Reference

"1" MU class network: http://www.imooc.com/learn/166

JSP basic Syntax

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.