当前位置:网站首页>JSP entry notes

JSP entry notes

2022-06-23 05:08:00 Don't want to be a programmer

JSP

1、 What is? JSP

Java Server Pages:Java Server side page , And also Servlet equally , For dynamic Web technology
The biggest feature :

  • Write JSP It's like writing HTML
  • difference :HTML Only static data is provided to users 、jsp Pages can embed JAVA Code , Provide users with dynamic data

2、JSP principle

Ideas :JSP How did it happen

  • There are no problems at the code level
  • Work inside the server
    tomcat There is one of them. work Catalog ;
    IDEA Use Tomcat Of will be in IDEA Of Tomcat To create a work Catalog

Browser sends request to server , No matter what resources are accessed , In fact, they are all visiting Servlet!
JSP It will eventually be transformed into a Java class !
JSP It's essentially one Servlet

// initialization 
public void_jspInit(){
    }
// The destruction 
public void_jspDestory(){
    }
//JSPService
public void _jspService(.HttpServletRequest request,HttpServletResponse response){
    }
  1. Judge the request
  2. Built in some objects  Insert picture description here
  3. All of the above can be found in jsp Use in !

stay JSP On the page :
As long as it is JAVA The code will be output intact ;
If it is HTML Code , It will be converted to :<out.write("<html>\r\n")> This format is output to the front end !

3、JSP Basic grammar

Every language has its own grammar ,JAVA There is ,.
JSP expression

 <%--JSP expression 
  effect : Used to convert the output of the program , Output to client 
 <%=  Variables or expressions %>
 --%>
 <%= new java.util.Date()%>

JSP Script

<%
    int sum = 0;
    for(int i = 1;i<=100;i++){
    
        sum+=i;
    }
    System.out.println("<h1>Sum="+sum+"</h1>");
%>

JSP Statement

<%!
static{
    
    System.out.println(" Statement ");
}
private int globalVar = 0;
%>

JSP Statement : Will be compiled into JSP Generate Java In the two-point class ! Other , Will be generated to _jspService In the method !

<%@ page import="java.util.*"%>
<%@ page isErrorPage="true" %> // This is an error page 
<%@ page errorPage="error.jsp" %> // If it is an error page, go to error.jsp

web.xml Add the configuration error page  Insert picture description here

4、9 Large built-in objects

  • PageContext To save things
  • Request To save things
  • Response
  • Session To save things
  • Application【ServletContext】 To save things
  • config【ServletConfig】
  • out
  • page
  • exception

JSP Page code

<%
    pageContext.setAttribute("name","jjj");// The saved data is only valid in one page 
    request.setAttribute("name2","jjj2");// The saved data is only valid in one request , Request forwarding will carry this data 
    session.setAttribute("name1","jjj1");// The saved data is valid only in one session , From open browser to close browser 
    application.setAttribute("name3","jjj3");// The saved data is only valid in the server , From turning on the server to turning off the server 
%>
<% pageContext.setAttribute("hello1","hello1",PageContext.SESSION_SCOPE);%>// Equate to 
<%--<% session.setAttribute("hello1","hello1");%>--%>

request: Client sends request to server , Data generated , It is useless after reading with the user , such as : Journalism , It's useless for the user to read !
session: Client sends request to server , Data generated , After the user runs out, there are still , such as : The shopping cart ;
application: Client sends request to server , Data generated , One user ran out , Other users may also use , Such as chat data

5、JSP label 、JSTL label 、EL expression

EL expression :(${ })

  • get data
  • Perform an operation
  • obtain web Common objects for development
<jsp:forward page=" index.jsp">
	<jsp:param name="value1" value="value1"></jsp:param>
	<jsp:param name="value2" value="value2"></jsp:param>
</jsp:forward>`// Jump to a page 
// Take out the parameters 
<%= request.getParam("value1")%>
<%= request.getParam("value2")%>

JSTL label
Rookie course study

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

JSTL The tag library is to make up for HTML The lack of labels ; It customizes many labels .

Core tags ( Master part ):
Format label :
xml label

Use steps :

  • Introduce the corresponding taglib
  • Use the method
  • stay Tomcat We also need to introduce jstl package , Otherwise, an error will be reported :JSTL Parse error (Tomcat Of lib in )
<form action="coreif.jsp" method="get">
	<input type="text" name="value1" value="${param.value1}">
// Determine if the submitted user is an administrator , Login succeeded 
<c:if test="${param.value1=='value1'}" var="isAdmin">
	<c:out value=" The administrator welcomes you !">
</c:if>
// Self closing label 
<c:out value="$(idAdmin)"/>
<c:set var="score" value="85"/>
<:choose>
	<c:when test="${score>90}">
	 You have to do well 
	</c:when>
	<c:when test="${score>=80}">
	 You have to do well 
	</c:when>
	<c:when test="${score>=70}">
	 Your grades are average 
	</c:when>
	<c:when test="${score<=60}">
	 You have to fail 
	</c:when>
</c:choose>

//var  Every time you traverse the variables 
//items  Object to traverse 
//begin  Where to start 
//end  Where to end 
//step  step 
<c:forEach var="***" items = ${
    list}>
	<c:out value="${people}"/><br>
</c:foreach>
<c:forEach var="" items="" begin="" end="" step=""></forEach>
原网站

版权声明
本文为[Don't want to be a programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230136591849.html