JavaWeb Study ( Nine )—JSP principle /JSP Basic grammar /JSP Instructions /JSP label
01 JSP principle
JSP The full name is Java Server Pages, It and servle The technology is the same , All are SUN A company defined one that is used to develop dynamic web Technology of resources .
JSP The biggest thing about this technology characteristic lie in , Write jsp It’s like writing html, But it compares html for ,html Only static data can be provided to users , and Jsp Technology allows you to nesting java Code , For the user Provide dynamic data .
JSP principle : The browser sends a request to the server , No matter what resources are being accessed , In fact, they are all visiting Servlet, So when you visit a jsp When the page is , Actually, I’m also visiting a Servlet, Server executing jsp When , First turn on the jsp Translate into a Servlet, So we visit jsp when , It’s not actually an interview jsp, It’s a visit jsp The translated one Servlet. give an example : When we access through the browser index.jsp when , The server will first index.jsp Translate into a index_jsp.class, stay Tomcat Server’s work\Catalina\localhost\ Project name \org\apache\jsp You can see index_jsp.class Source code file for index_jsp.java,index_jsp.java. We can see ,index_jsp This class is Inherit org.apache.jasper.runtime.HttpJspBase This class of , By looking at Tomcat The source code of the server , It can be seen in apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime In the directory HttpJspBase The source code file for this class .HttpJspBase Class is inheritance HttpServlet Of , therefore HttpJspBase Class is a Servlet, and index_jsp It’s inheritance again HttpJspBase Class , therefore index_jsp Class is also a Servlet, therefore When the browser accesses the server index.jsp When the page is , It’s actually a visit to index_jsp This Servlet,index_jsp This Servlet Use _jspService This method handles the request .
**JSP How to output the page to the client ?** stay jsp Writing in the java Code and html The code will be translated into _jspService In the method, go through Out Object output .
JSP Eight people : see _jspService And the way you can see ,Web Server is calling jsp when , Will give Jsp Provide the following 8 individual java object
PageContext pageContext;// Scope
HttpSession session;// conversation
ServletContext application;// application , In fact, that is servletContext
ServletConfig config;// To configure
JspWriter out;// Output
Object page = this;// page
HttpServletRequest request;// request
HttpServletResponse response// Respond to
among page object ,request and response It’s done Instantiation , Others 5 Objects that are not instantiated are instantiated in the following way
pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
02 JSP Basic grammar
JSP yes JAVA On the application of , stay JSP in , be-all JAVA All statements can be used . its Extended grammar by :
<%-- assignment --%>
<%= Variable or expression %>
<%-- stay <% %> You can define variables in 、 Write sentences , Can't define method --%>
<%
Multiple lines java Code
%>
<%--
Statement :JSP A declaration can be used to define JSP Page converted to Servlet Static code block of a program 、 Member variables and methods .
--%>
<%!
java Code
%>
<!--HTML Style notes -->
<%
//JAVA A single line comment in
/*
JAVA Multi line comments in
*/
%>
<%--JSP My own notes --%>
matters needing attention :
- JSP In the script fragment Can only appear java Code , No other template elements can appear , JSP The engine is translating JSP On the page , Will JSP In the script fragment Java The code will be placed intact in Servlet Of _jspService In the method .
- JSP In the script fragment Java Code must be Follow strictly Java grammar , for example , After each execution statement Must use semicolon (;) end .
- In a JSP On the page There can be multiple script fragments , Between two or more script fragments You can embed text 、HTML Marks and other JSP Elements .
- HTML When viewing the source file in the browser, it is You can see it Of , and JAVA Annotation and JSP notes When you view the source file in the browser, it is Out of sight Of the content of the note , This is the difference between these three annotations .
03 JSP Instructions
JSP There are only three instructions , Namely page
include
taglib
- Commonly used Page Instructions
<%-- Set page text type --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- Set the page to refresh automatically , The default value is false--%>
<%@ page autoFlush="true" %>
<%-- Set up JSP programing language --%>
<%@ page language="java" %>
<%-- Jump to error page ( It is not recommended to use , Recommended in the XML Middle configuration )--%>
<%@ page errorPage="/error/404.jsp" %>
<%-- Guide pack --%>
<%@ page import="com.hooi.pojo.Student" %>
<%-- Set page encoding --%>
<%@ page pageEncoding="utf-8" %>
stay xml Middle configuration error page
<!-- If the error code is 404, Then go to error Under the document 404 Error page -->
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<!-- If the error code is 500, Then go to error Under the document 500 Error page -->
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
- Commonly used include Instructions
<%@ include file="{ Pages to include }"%>
Usually , There will be some common locations in the website that can be extracted , Like the head and tail of the site .
For testing purposes , We design the head and tail of the page as :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
<style>
div h2 {
margin: 0 auto;
text-align: center;
}
</style>
</head>
<div>
<% int i = 1; // Define a global variable %>
<h2>Header_Page</h2>
<h3>i=<%=i%></h3>
</div>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
<style>
div h2 {
margin: 0 auto;
text-align: center;
}
</style>
</head>
<div>
<% int i = 1;// Define a global variable with the same name in the header page %>
<h2>Footer_Page</h2>
<h3>i=<%=i%></h3>
</div>
How to use include Instructions contain pages ?
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> home page </title>
</head>
<body>
<%-- Insert page --%>
<h1>@include test </h1>
<%@ include file="common/header.jsp"%>
<h1>@include test </h1>
<%@ include file="common/footer.jsp"%>
</body>
</html>
The operation effect is as shown in the figure :( Here we will 500 The error page is passed through xml Configure for your own defined page .) because header.jsp and footer.jsp The variable with the same name is defined , So in use include When the command contains the page , There will be 500 error . From this we can know that : Use @include Instructions contain pages , Will be included in the page and their own page fusion into a servlet, In the same servlet Defining a variable with the same name will prompt 500 error .
-
taglib
Use jstl Or other tag libraries need to download the corresponding jar package , Then import .
Reuse taglib Instructions will be jar Package added as dependency
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Attribute specification : prefix: Name of tag library to be used tagdir: The local directory of the tag library uri : The network address of the corresponding library --%>
need Be careful Yes. :
- The project should be imported into the corresponding tag library jar package .
- Tomcat Also need to be placed in jar package
- In the code , Some special symbols require the use of escape characters ;
04 JSP label
grammar :jsp:xxx
JSP There are three common tags :
<jsp:include>
: Used to insert the output of another resource into the current JSP In the output of the page , This kind of JSP The introduction method of page execution is called dynamic introduction .
<jsp:forward>
: Used to forward a request to another resource .
<jsp:param>
: When using <jsp:include>
and <jsp:forward>
When tags introduce or forward requests to other resources , have access to jsp:param Tag passes parameters to this resource .
Test code :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>jsp:include test </h1>
<jsp:include page="common/header.jsp"/>
<jsp:forward page="index.jsp">
<jsp:param name="username" value="DOGIOOH"/>
<jsp:param name="age" value="24"/>
</jsp:forward>
<h1>jsp:include test </h1>
<jsp:include page="common/footer.jsp"/>
</body>
</html>
<html>
<head>
<title> home page </title>
</head>
<body>
<div>
test jsp:forward adopt request Get the input parameters ( Use request Object needs to be guided )
<h1>jsp:forward test </h1>
<h2>User:<%=request.getParameter("username")%></h2>
<h2>Age:<%=request.getParameter("age")%></h2>
</div>
</body>
</html>
test result :
First of all jsp:include Test results : because jsp:include Include… For dynamic , Will not include the page and their own page fusion into a servlet, Therefore, no error will be reported because the variable with the same name appears in the header and footstep page .
jsp:forward and jsp:param The test results are as follows :
jsp:useBean label
First we need to understand JavaBean It’s a specific way of writing Java class , It usually has the following characteristics :
- This Java Class must have a parameterless constructor
- Property must be privatized .
- The property of privatization must pass public Type of method exposed to other programs , And the naming of methods must also comply with certain naming norms .
Example :
public class Student {
private String name;
private int age;
private String id;
private String sex;
private boolean graduated;
public Student() {
}
public Student(String name, int age, String id, String sex, boolean graduated) {
this.name = name;
this.age = age;
this.id = id;
this.sex = sex;
this.graduated = graduated;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean isGraduated() {
return graduated;
}
public void setGraduated(boolean graduated) {
this.graduated = graduated;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", id='" + id + '\'' +
", sex='" + sex + '\'' +
", graduated=" + graduated +
'}';
}
}
<jsp:useBean>
The label is used to find the JavaBean object , If it exists, it will be returned directly JavaBean References to objects , If it doesn’t exist, instantiate a new JavaBean Object and store it in the specified domain scope with the specified name .
<jsp:useBean>
Examples of tag usage :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.hooi.pojo.Student" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--Java Code implementation : Premise , Need to guide package --%>
<% Student stu1 = new Student(); %>
<% stu1.setName("Jackson"); %>
<% stu1.setAge(18); %>
<% stu1.setSex("male"); %>
<% stu1.setId("0000001"); %>
<% stu1.setGraduated(false); %>
<%--jsp Tag implementation --%>
<jsp:useBean id="stu2" class="com.hooi.pojo.Student" scope="page"/>
<jsp:setProperty name="stu2" property="name" value="Curry"/>
<jsp:setProperty name="stu2" property="age" value="31"/>
<jsp:setProperty name="stu2" property="sex" value="male"/>
<jsp:setProperty name="stu2" property="id" value="0000002"/>
<jsp:setProperty name="stu2" property="graduated" value="true"/>
<%--=============================================================--%>
<%--Java Code to get properties --%>
<div>
<h1>Java Realization </h1>
<h2>User:<%=stu1.getName()%></h2>
<h2>Age:<%=stu1.getAge()%></h2>
<h2>Sex:<%=stu1.getSex()%></h2>
<h2>ID:<%=stu1.getId()%></h2>
<h2>Graduated:<%=stu1.isGraduated()%></h2>
</div>
<%--EL expression : Can't express objects --%>
<div>
<h1>EL Expression implementation </h1>
<h2>User:${stu2.name}</h2>
<h2>Age:${stu2.age}</h2>
<h2>Sex:${stu2.sex}</h2>
<h2>ID:${stu2.id}</h2>
<h2>Graduated:${stu2.graduated}</h2>
</div>
<%--jsp Tag implementation gets properties --%>
<div>
<h1>JSP Realization </h1>
<h2>
User:<jsp:getProperty name="stu2" property="name"/>
</h2>
<h2>
Age:<jsp:getProperty name="stu2" property="age"/>
</h2>
<h2>
Sex:<jsp:getProperty name="stu2" property="sex"/>
</h2>
<h2>
ID:<jsp:getProperty name="stu2" property="id"/>
</h2>
<h2>
Graduated:<jsp:getProperty name="stu2" property="graduated"/>
</h2>
</div>
</body>
</html>