In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what Jsp says in Web", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what does Jsp say in Web?"
Web basic understanding Edition-JspJsp
Jsp is called Java Server Pages, which is the dynamic page in our JavaWeb.
Jsp can render data in the form of HTML pages and is a HTML that can be embedded in Java code.
Jsp is essentially a Servlet. Servlet can do anything JSP can do.
Jsp must be run on the server and cannot be opened directly using a browser.
Jsp is the technical standard of Web web pages, the main syntax components include: instructions, html template elements, script fragments (small scripts), expressions, declarations, comments, suffix is * .jsp.
Jsp is mainly responsible for displaying and obtaining data.
Jsp is a dynamic page, html is a static page
Dynamic page static page operation principle after the server parses, displays the data in the browser directly in the browser parses the maintenance cost is low, can modify the background data, then affects the data in the page is high, must overwrite the modified page database, can connect the database not to connect the database, the access speed is slow, the fast writing code can write the java code, cannot write the java code.
Matters needing attention
1. The jsp page is a page similar to html. Jsp is directly stored in the WebContent directory, and when accessing jsp like html, it is the same as accessing html.
2. The default coding set of jsp is iso-8859-1, and the default encoding of jsp is modified to UTF-8.
Operation principle of JSP
In fact, when Tomcat runs JSP, it does not directly display the JSP page we wrote, but converts the JSP page into a Java class, which is actually a Servlet.
Let's find that directory. For Eclipse, under: workspace, right-click the Tomcat service and click Browse Deployment Location....
In the work directory. Work\ Catalina\ localhost\ day07_jsp\ org\ apache\ jsp folder
Open the index_jsp.java file to see the contents: it is found that the generated class inherits from the HttpJspBase class. This is a jsp file to generate Servlet program to inherit the base class! And this HttpJspBase class inherits from the HttpServlet class.
So when we access a xxx.jsp file and translate it into a java file, the full name is the xxx_jsp.java file.
Web.xml in the conf directory under tomcat configures the mapping information of JSP.
Visit the same jsp file later
If the file is not changed, it will not be translated and compiled
If the file changes, it will be translated and compiled
Basic syntax of Jsp
Instruction
Syntax format:
Three main instructions:, |
Template element
Html&css&js&jQuery et al.
Code script snippet
Format:
Purpose: in the _ jspService () method, write the java code.
Expression.
Format:
What it does: displays data to the page, which is the same as out.print ().
Statement
Format:
Role: in the translated class helloworld_jsp this Servlet class, write java code.
Annotation
Java: single-line comments: / /, multi-line comments: / * * /
Html:
Jsp:
JSP comments Java comments HTML comments JSP page visible Java code invisible browser invisible Jsp common instructions
Grammatical format
Page instruction
Attribute
Language: language, the value is java and only java.
ContentType: set the browser code in accordance with response.setContentType ().
PageEncoding: sets the encoded character set of the Jsp page.
Import: guide package
IsErrorPage: sets whether the current page is an error page. The default value is "false".
True: sets the current page to an error page. You can use the exception built-in object to catch exceptions.
False: sets that the current page is not an error page and cannot use the exception built-in object to catch exceptions.
ErrorPage: sets the target page to jump to when the current page is wrong. Errors need to be caught in _ jspService ().
Include directive: static include
Purpose: to include the target file in the current file.
Features: included files will not be translated & compiled. (include first, then translate)
Taglib instruction
Attribute
Prefix is used to specify the prefix name by which we use JSTL.
The uri is equivalent to the unique identity of the library, because the JSTL consists of several different libraries, and use this attribute to specify which library to import.
Function: introduce tag library.
Jsp Action label
Unlike HTML tags, JSP action tags are parsed by browsers, while JSP action tags require a server (Tomcat) to run.
Forward action label
Function: used for forwarding operations on the page
Forward the child tag; set the request parameters when forwarding, and get the request parameters on the target page through request.getParameter ().
Note: if the forwarding action tag does not need to set request parameters, the tag starts and ends inside the tag, and nothing is allowed to be written (including spaces).
Dynamically include action tags
Function: dynamically include other pages into the current page.
Features: the included files will be translated and compiled at the same time. (translate first, then include)
Essence: when using dynamic inclusion, Tomcat adds the following code to the generated Servlet:
Org.apache.jasper.runtime.JspRuntimeLibrary.include (request, response, "target.jsp", out, false) The difference between dynamic inclusion and static inclusion @ include instruction jsp:include tag characteristics static include the basic form of dynamic inclusion syntax include the timing of actions during translation whether or not to generate java files during translation do not generate merge mode code replication merge run results included content files actual content page output code conflict is it possible to compile files included in times 1 + 1 The scope of application includes pure static content (CSS HTML,JS) Or not very time-consuming. Or a large number of java code contains parameters that need to be passed. Contains a lot of java code, operations, time-consuming operations. Nine implicit objects of Jsp
The JSP container provides Java objects for each page that developers can use directly without explicitly declaring them.
PageContext
Type: PageContext
Definition: represents a page region object, which is used to represent the entire JSP page.
Function:
Page area object
The "big brother" of the nine implicit objects can call the other eight implicit objects directly.
Get method in Servlet: none.
Type: HttpServletRequest
Definition: a request message sent to the server by the browser on behalf of the browser. The object is created by the server and finally sent as parameters to the doGet () and doPost () methods.
Whenever a client requests a JSP page, the JSP engine creates a new request object to represent the request. The request object provides a series of methods to get HTTP header information, cookies,HTTP methods, and so on.
Role (see request object in Servlet for details)
Get request parameters
Get url address parameters
Request forwarding
Save data to the request domain (get data & remove data)
Get request header information
Get method in Servlet: directly used in doGet () or doPost ().
Session
Type: HttpSession
Definition: represents a session between the browser and the server.
Action
The session object is used to track sessions between client requests.
Session domain object
Application
How to get it in Servlet: request.getSession ()
Type: ServletContext
Definition: Servlet context, which represents the current web application.
When the Web container starts, it creates a unique corresponding ServletContext object for each Web application, meaning Servlet context, which represents the current Web application.
Action
Get the context path of the project (project name with /): getContextPath ()
Get the local real path mapped by the virtual path: getRealPath (String path)
Get global initialization parameters for WEB applications (basically not used)
4. Get Web application initialization parameters: application.getInitParameter ("ParamName")
How to get it in Servlet: get it using the this.getServletContext () method.
Page
Type: Object
Function: this, the current class object.
Response
Type: HttpServletResponse
Definition: represents the response message that the server sends to the browser. The object is created by the server and finally sent as parameters to the doGet () and doPost () methods.
Function:
Response data to the page (response body), including text, Html, and so on.
Redirect
Set response header information
Get the method in Servlet: directly used in doGet () or doPost ()
Config
Type: ServletConfig
Definition: represents the configuration information of the current Servlet, and each Servlet has a unique corresponding ServletConfig object.
Function:
Get Servlet name: getServletName ()
Get the global context ServletContext object: getServletContext ()
Get the Servlet initialization parameter: getInitParameter (String) / getInitParameterNames ().
How to get it in Servlet: this.getServletConfig ()
Out
Type: JspWriter
Definition: represents the output stream of the current page.
Function: similar to the PrintWriter function in Servlet, response data to the page, the response data can be pages, page fragments, strings, and so on.
How to get it in Servlet: none
Exception
Type: Throwable
Definition: an exception object that represents the current page.
Function: capture the exception information in the processing page.
How to get in Servlet: new Throwable ()
The nine built-in objects are all objects that we can use directly in [code script] or [expression script].
Jsp four domain objects
Domain objects in the program are mainly responsible for data exchange between different web resources, such as data exchange between servlet and jsp.
Domain object analysis
Each domain object maintains a Map, a common method of the domain object.
Set properties to the domain: void setAttribute (String key, Object value)
Get the specified property from the domain: Object getAttribute (String key)
Remove the specified attribute from the domain: void removeAttribute (String key)
Domain object validity
PageContext: the shared data in the current page is valid, leaving the current page is invalid.
Each page has its own unique pageContext object.
Notice that there is no such object in servlet.
Request: the shared data is valid in the current request.
Current request: forward, directly access a page as the current request.
Not in the current request: redirect, open the page and click on the hyperlink on the page is not the current request.
Session: shared data is valid in the scope of a session.
Current session: the current browser is not closed & if the browser is not changed, it is the current session.
Only care about whether the browser is closed, not about the server shutdown and restart.
Different browsers do not share sessions.
Application: it is valid to share data while the server is running.
Server shutdown destroy
Domain object scope start time end time pageContext current JSP page load leave page request same request received response session same session start session end session application current Web application Web application load Web application unload
The above is all the content of this article "what does Jsp say in Web?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.