Java beginners learning guide [day32] ---Servlet interaction & JSP principle and application

One, Servlet interaction

1. Three responsibilities of Servlet

  1. Receive request parameters (encapsulation);

  2. Call the business method to process the business (to call the business service layer);

  3. Respond to requests (jump pages or other Servlets);

2. Two ways to jump [Key points]

Request forwarding (forward) Redirect
Visit WEB-INF ×
Pass parameters ×
Access to the Internet ×
Repeated questions Have no
Path change no Have
Code req.getRequestDispatcher(path).forward(req, resp); resp.sendRedirect(path)

Two, JSP learning

1. Basic understanding of JSP

JSP (java server page): Java server-side (dynamic) web pages, JSP is the technology used to make dynamic web pages in Java; lock industry can write JAVA code or HTML code in JSP

Note: JSP is Servlet, the server will make JSP into a Servlet class

2. JSP compilation principle and grammar

Compilation process:

Insert picture description here

grammar:

  • Two kinds of notes:
<!-- 这是HTML注释,在前端浏览器可以看到 -->
<%-- 这是JSP注释,给程序员看的,前端浏览器是看不到的 --%>
  • java code writing
<%
	这里写Java代码
%>

<%=表达式(输出数据到页面上)  %>
<%=new java.util.Date().toLocaleString() %>

<%! 定义成员变量 %>

3. Three major instructions

  • page : Page instructions: some configuration of the current page
  • include: Contains instructions: other pages can be included
  • taglib: Tag instructions (learn tomorrow)

4. Page command

format:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" ... >

language="java": The language is only java, meaningless

contentType="text/html; charset=UTF-8": The output page type, which is equivalent to writing in the Servlet:resp.setContentType("text/html;charset=utf-8")

import : Guide package

errorPage: If something goes wrong, point to a page

isErrorPage: Is this page an error page

  • If the value is true, we can see the error message on the page when we report 500 later

5. Include instructions

You can include the content of one page in another page

Note: Generally, the included jsp file will be named jspf (jsp fragment)

<%@ include file="/include/head.jspf" %>

6. Four scopes

Scope objects are containers. Data is placed in that scope and can only be retrieved in the corresponding scope. There is no containment relationship. The size relationship only refers to the survival time of the stored object.

Insert picture description here

  • application : Living to the death of the project, it can generally be used to calculate how many people are currently visiting our website
    • Note: appcationjustServletContext
    • Get the context path in JAVA code getContextPath()
    • Get the current real path getRealPath("/")
  • session : A session, live until the browser is closed, generally used to save logged in users
    • By default, only live for half an hour, and your browser hasn’t touched it for half an hour
  • request : One request (most used)
  • pageContext: The current page

7. Nine built-in objects

Built-in objects class
application ServletContext Global object
session HttpSession Session object
request HttpServletRequest Request object
pageContext PageContext Current page object
response HttpServletResponse Response object
out JspWriter Output object
page this Current object
config ServletConfig Configuration object
exception Exception object

Guess you like

Origin blog.csdn.net/WLK0423/article/details/110368750