JSP Day 1

Source: Internet
Author: User
Tags html comment

JSP: Java Server Pages
Run on the server page developed in Java

HTML + JavaScript --- the page running on the client

Client ---- HTML ----> servlet ---> Dao ---> DB

Advantages and disadvantages of servlet:
Process business logic
Display page out. println ("<JavaScript> <CSS> <HTML> ");
No syntax check during writing
Debugging depends on Web containers
===" HTML + Javascript is nested in Java code
Solution:
Embed Java code = jsp in HTML + JavaScript code

JSP is essentially a servlet.

JSP running call process:
1. When the client first accesses, the Web Container translates the JSP into a Servlet and compiles it at the same time, and then calls the Method
_ Jspservice. In the _ jspservice method, the content is mainly out. Write ("static information ");
2. After the first access, the Web Container directly calls the _ jspservice method as long as the page content does not change.
3. When the content on the page changes after the first access, the Web Container will compile and then call the _ jspservice method.

Note:
During development, as long as it is a page (html/jsp), after the content is changed, you just need to refresh the page.

Syntax for how Java code is embedded into HTML: <%>
Declaration <%! %>
Translated as a member variable in Servlet
The code in the declaration must end;
Declaration variables on the same JSP page cannot be duplicated

Comment <% -- %> standard JSP comment
It only exists in the source code of JSP and is used for debugging by JSP programmers.
<! --> Standard HTML comment
Translated as static information in out. Write ().
<% // ** // *****/%> Pure Java Annotation
Translated as a comment inside the _ jspservice method, which is not included in the client source code

Expression <% = %>
Replace the current tag with the calculation result of the expression on the page.
The content in the expression must end with a letter;

Code segment <%>
Translated as information in the _ jspservice Method
The end must be a semicolon.
======================================
The preceding JSP syntax structure cannot be nested
======================================

Package com;
Import Java... -- command

Public class test {
//
Int I = 100; -- Declaration
Public void show (){
Int I = 20;
// Calculate I -- Comment
I + = 30; -- Expression
System. Out. println (I); -- code segment
}
}
Class {

}

 
Exercise:
Use the JSP syntax structure on the JSP page to display the 99 multiplication table

JSP ---> servlet ---> Dao ---> DB

JSP syntax --- command: <% @ %>
Page command: <% @ page property name = property value... %>
* 1) contenttype = "text/html; charset = GBK"
==>
Response. setcontenttype ("text/html; charset = GBK ");

2) pageencoding = "GBK"
==> Setcharacterencoding ("GBK ");

3) Language = "Java"
It can be left blank by default. APIs reserved for compatibility with other languages
4) Session = "True/false"
Sets whether to access the current JSP page and whether to create a session
The default value is true.
5) buffer
Set the buffer size when the current page outputs information. The default value is 8 KB.
If this parameter is set to none, the buffer is not applicable and each message is directly output to the client page.
6) autoflush
Set whether to automatically refresh information to the client page when the buffer is full
The default value is true, that is, automatic refresh
7) isthreadsafe = "True/false"
Sets whether the current page is under thread security control.
The default value is true, that is, thread security.
If this parameter is set to false, multiple threads on the client must access the page sequentially.
* 8) Import
Introduce a class path other than the lang package to the current page
* 9) errorpage
Specifies the page path to jump to if an exception occurs on the current JSP page
==="
Try {

} Catch (exception e ){
....
// Resp. sendredirect ("error. jsp ");
Req. setattribute ("exception", e );
Req. getrequestdispatcher ("error. jsp"). Forward (req, resp );
}

* 10 iserrorpage
Specifies the current error. jsp page, which is a special page for exception handling.
On this page, you can obtain the exception Object Information on the page before the exception jump
==>
Exception E = Req. getattribute ("exception ");

Include command: <% @ include file = "" %>
Introduce information on other JSP pages
Static introduction:
Source code is merged for all pages for unified compilation and execution.

Action: <JSP: Tag Name> </jsp: Tag Name>
Include action: <JSP: Include page = ""> </jsp: Include>
Dynamic introduction:
Compile and obtain the execution results of each page, and then merge the results to display
Param action: dependent on include action
<JSP: Include page = "foot. jsp">
<JSP: Param name = "copyname" value = "@ _ @"> </jsp: param>
</Jsp: Include>
Obtain parameters:
<% = Request. getparameter ("copyname") %>

Forward action:
<JSP: Forward page = "">
 
JSP built-in object
Request --- javax. servlet. http. httpservletrequest
Response --- javax. servlet. http. httpservletresponse
Exception --- java. Lang. throwable
Out --- javax. servlet. jsp. jspwriter
Session --- javax. servlet. http. httpsession
Application --- javax. servlet. servletcontext

Chinese garbled characters in web projects:
1 JSP must set the command header <% @ page contenttype = "text/html; charset = GBK" %>
2 add characterencodingfilter
 
HTTP: /localhost: 8088/myjsp/include/static/content. jsp
==> ../Basic/date. jsp
Http: // localhost: 8088/myjsp/basic/date. jsp

======================================
Note:
Comment out the code on the JSP page
Information, must use <% -- %>
======================================

 

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.