Recently started learning java.
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!--page tag, Java language, java.util.* package, UTF-8 Encoding--
Have the following three sentences to write relative paths
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<base href= "<%=basePath%>" >
Details are as follows:
<base href= "..." > is the root path used to indicate the relative path of the current page.
For example, there is a connection inside the page, and the complete path should be http://localhost/myblog/authen/login.do
Where http://server/is the basic path of the server, MyBlog is the name of the current application, then my root path should be so http://localhost/myblog/.
With this <base ... > after the connection of my page content, I do not want to write the full path, I just write authen/login.do on it. The server will automatically assemble the path specified by <base ...> and the relative path within the page to form the full path.
If there is no such <base...>, then the link of my page must write the full path, otherwise the server will not be found.
Request.getschema () can return the protocol used by the current page, which is "http" in the example above
Request.getservername () can return the name of the server on which the current page is located, or "localhost" in the example above
Request.getserverport () can return the port used by the server on which the current page is located, typically 80
Request.getcontextpath () can return the name of the application in which the current page is located, which is the myblog in the example above
These four together, is the relative path of the current page.
-----------------------------------------------------------
Here are some basic syntax:
1. Instruction <%@ Directive%>
JSP directives are the engine of the JSP. They do not directly produce any visual output, but indicate what the engine needs to do with the rest of the JSP pages. The instruction is marked by <%@?%>. The main two directives are page and include. Directive Taglib is not discussed in this article, but it is used when creating custom labels in JSP1.1.
The instruction page can be found at the top of almost all JSP pages. Although not required, you can define things like where to look for Java class support.
<%@ page import= "Java.util.Date"%>
Indicates where a network user is directed when a Java runtime problem occurs:
<%@ page errorpage= "errorpage.jsp"%>
Whether information needs to be managed at the user session level, which is likely to span multiple Web pages (this is described in more detail in the section on JavaBeans):
<%@ page session= "true"%>
The directive "include" can divide your content into more manageable elements, such as elements that include a normal page header or footer. The included page can be a fixed HTML page or more JSP content:
<%@ include file= "filename.jsp"%>
2. Declaration <%! Statement%>
JSP declarations allow you to define page-level variables to hold information or to define the support methods that the remaining JSP pages might require. If you find yourself writing too much code, it is often best to write to a separate Java class. The declaration is defined by <%!?%>. You must end the variable declaration with a semicolon, and any Java statement that the content must be valid: <%! int i=0; %>
3. Expression <%= expression-%>
The result of evaluating an expression is converted to a string and included directly in the output page through an expression in the JSP. The JSP string is marked by the <%=?%> label, and is not included in the semicolon unless it is a referenced partial string.
<%= I%>
<%= "Hello"%>
4. Code snippet/script segment <% code snippet%>
The JSP code snippet or script fragment is embedded in the "<%?%>" tag. This Java code runs when the Web server responds to the request. There may be purely HTML or XML code around the script fragment, where code snippets can allow you to create conditional execution code, or just call another piece of code. For example, the following code combination uses expressions and script fragments to display the string "Hello" in the H1, H2, H3, and H4 tags. Script fragments are not limited to one line of source code:
<% for (inti=1; i<=4; i++) {%>
<H<%=i%>>Hello</H<%=i%>>
<%}%>
5. Comment <%--Comment--%>
Java Learning Note 1