JSP technology: JSP overview, JSP basic syntax, JSP implicit object, JSP instruction, JSP action element, making two pages through JSP

Keywords: Java xml http

Chapter 6: JSP technology: JSP overview, JSP basic syntax, JSP implicit object, JSP instruction, JSP action element, making two pages through JSP
1.JSP overview:
1.1 what is jsp: its full English name is java server pages, java server page. It is a dynamic web page development technology based on servlet specification.

  • Java Server Page: you can write java code or html tags on jsp, which runs on the server.
  • JSP = java + HTML + some things of JSP itself.
    Why jsp technology is produced:
    In the early days when there was no jsp, it was troublesome to write data to the page by servlet, so jsp technology came into being,
    The biggest feature of jsp: it can write page html very simply.
   jsp In fact, its essence is servlet:  Can see jsp Inherited HttpJspBase inherit  HttpServlet. 

1.2 Write the first jsp file: jsp generate java Files, compiling into class File, exists tomcat Server work catalogue
    Note: modify jsp Default encoding format for: window---------->preferenes-----> search jsp -----> Select the code to modify

1.3 JSP Operating principle:

jsp-->java---> .class

     1.First access via browser jsp Time
     2.JSP The container generates the corresponding Servlet File, i.e.java file
     3.JSP The container will java Corresponding to file compilation class file
     4.JSP Container, the corresponding servlet object
     5.Generated by servlet Object to process the request sent by the browser and respond to the data to the browser.

2.JSP basic syntax
1.1 JSP script elements
There are three forms of writing java on jsp pages:
1. You can declare statements and methods
2. You can write java code segments
3. You can write expressions

  1. JSP Scriptlets: generated at the corresponding location_ Inside the jspService method.
    Java code fragments can be written in jsp pages. The basic syntax is: <% java code fragment% >
  2. JSP declaration statement: the corresponding location is generated in the member location.
    Variables and methods can be written in jsp: basic syntax:
    <%!
    Define variables or methods
    %>
  3. JSP expression: the corresponding position is generated in_ Inside the jspService method.
    On the jsp page, you can enter a resu lt. The basic syntax is: <% = expression% >, which is actually equivalent to out.print(), Note: semicolons cannot be written after expressions
1.2 JSP notes:	
     stay jsp Inside, you can write html,Can write java,Can write jsp Some of its own content.
     stay jsp There are three forms of notes:
     >>> 1. html notes:<!-- html notes-->: It exists everywhere.
     >>> 2.java Note: single line, multi line, document note: it exists jsp Page, also exists java In the file, the corresponding generated html It disappeared
     >>> 3. jsp notes: <%-- jsp notes-->: Only exist jsp Page, generating the corresponding java The file, it disappears.

3.JSP instruction: page instruction, include instruction, taglib instruction

1.1 page instructions
    page The purpose of the command is to set and jsp Page related information, such as settings jsp Page code, jsp Default language of the page, etc
    Basic syntax format:<%@ page attribute="Attribute value" attribute="Attribute value" attribute="Attribute value" %>
    For example:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
	     pageEncoding="UTF-8"%>
    Common attributes:
     language: default language java,That is, only java A language.
     pageEncoding: set up jsp In general, the coding format can be omitted contentType This property.
     session Attribute: value type is boolean,If the corresponding value is true´╝îstay jsp Page can use session Object, otherwise it cannot be used.
     import Properties: guided packages, and java If you want to use a class, you must import the package name of a class.
     ----
1.2 include Instruction:
     stay jsp The page usually needs to display the information of other pages, so you can use this include Command to include other pages.
     Basic syntax:<%@ include  file="Of included files url" %>
     Create two jsp Page: date.jsp Page, include.jsp Page.
     use jsp of include The inclusion of instruction implementation is also called static inclusion. The characteristics of static inclusion are as follows:
     1.Merge and output the contents of multiple pages, such as date.jsp and include.jsp Content merge output.
     2.Generate one corresponding to multiple pages java File, compiled, corresponding to one class File.

4.JSP implicit object
1.1 overview of implicit objects:
What is implicit object: in jsp pages, jsp provides some objects that can be used directly without creating them ourselves.
Nine common implicit objects of jsp:
request: HttpServletRequest, request object, which is also a domain object. The scope is one request
response:HttpServletResponse, response object
session: HttpSession, session object, which is also a domain object. The scope is a session
application:ServletContext, a context object, is also a domain object, and its scope is the whole project
config:ServletConfig, configuration object
out:JspWriter is actually similar to PrintWriter.
page:Object, which represents the page object of the current jsp
Pagecontext: pagecontext, JSP container
exception:Throwable, exception object, used to capture exception information.
The exception object can only be used if the current jsp page has the property isErrorPage="true".

1.2 out object
The out object outputs the content to the web page, and finally outputs it through the reponse buffer.

1.3 pageContext object
The first function: get the other eight implicit objects

1. Get request object: getRequest();
2. Get the response object: getResponse();
3. Get the output object: getOut();

The second function: operate the other three domain objects: request, session and application
pageContext is also a domain object: the scope is in the current jsp page.
Methods to manipulate the other three domain objects:
1. Stored value: setAttribute(String key,Object obj,int scope);
APPLICATION_SCOPE: corresponds to application
REQUEST_SCOPE: corresponds to request
SESSION_SCOPE: corresponds to session
PAGE_SCOPE: corresponds to its own pageContext
2. Value: getAttribute(String key,int scope);

Conclusion: in the pageContext domain object, the findAttribute method is used to obtain the value, and the global search is carried out,
Search range: pageContext - > request - > session - > Application
First, find the object with small scope. If a value is obtained, stop looking down. If there is no corresponding value, continue looking down.
1.4 exception object

It is used to capture exception information.
Two properties are usually used:
>>> errorPage:Property, the page handling exception information, that is, if an exception occurs on the current page, jump to the page handling exception information
    For example:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8" errorPage="error.jsp"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 	<%
 		int a=3;
 		int b=0;
 	%>
 	Output:<%=(a/b) %>

isErrorPage: the value corresponding to the property is true or false. If it is true, the exception object can be used on the current page
For example:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>

 	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 	<html>
 	<head>
 	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 	<title>Insert title here</title>
 	</head>
 	<body>
 		Output exception information:  <%=exception.getMessage() %>
 	</body>
 	</html>

5.JSP action element
1.1 jsp:include action element: dynamic include
Dynamic inclusion: merge the contents of other resource pages into the current page to be included, and output the last piece.
Basic syntax format: < jsp: include page = "url of included page" flush = "true or false" > jsp: include
Dynamic inclusion features: merge and output the contents of multiple pages, generate multiple java files from multiple jsp pages, and compile them into multiple class files
Static inclusion features: the contents of multiple pages are combined and output, multiple jsp pages finally generate a java file and compile a class file.

1.2 <jsp:forward>Action element: request forwarding.
     Its function is to realize request forwarding, and the effect is equivalent to RequestDispathcer Object forward method.
     Basic syntax format:<jsp:forward page="Forwarding page url address"></jsp:forward>
     Forwarding page url Address: you do not need to carry the item name, because the forwarding action is an internal behavior of the server.

6. Stage case: smart book city JSP page
1. Realize Homepage
Notes: 1. You can't write jsp related content on html pages.
2.${pageContext.request.contextPath}: the function is to obtain the path of the project name, such as: / chapter06
Steps for designing homepage:
1. Create an index.jsp page, where you can jump to the home page
<jsp:forward page="" ></jsp:forward>
2. Modify all html files under the client directory to jsp, so as to be the jsp knowledge point.
3. Separate several jsp files from the home page under the client directory. The separated jsp files are universal and can be used on other pages,
The purpose of separation is to improve the reusability of code.
Separate the index.jsp page:
Part I: navigation menu, corresponding to head.jsp
Part II: search bar, corresponding to menu_search.jsp
Part III: bottom information, corresponding to foot.jsp
Introduce a separate file on the index.jsp page: <% @ include file = ""% >

2. Realize the registration page (refer to the design of the home page)

Posted by Sondar on Tue, 16 Nov 2021 19:06:43 -0800