JSP specification

JSP specification introduction

1. From one of the JAVAEE specifications

2. The JSP specification defines the development process of how to develop JSP files instead of response objects and write processing results to the response body

3. The JSP specification defines how the Http server should call and manage JSP files

The drawbacks of the response object

1. It is suitable for writing processing results with a small amount of data to the response body

2. If there are too many processing results, using response objects will increase the difficulty of development

Advantages of JSP files

1. JSP files are a substitute for responding objects in the process of Internet communication .

2. Reduce the development workload of writing the processing results into the response body and reduce the difficulty of processing results maintenance

3. When developing a JSP file, you can directly write the processing result to the JSP file without handwriting the out.print command.

4. When the Http server calls the JSP file, it automatically writes all the content written in the JSP file to the response body through the output stream according to the requirements of the JSP specification

The difference between HTML files and JSP files

1. Different types of resource files

HTML files are static resource files , and their related commands need to be compiled and executed in the browser.

The JSP file is a dynamic resource file , and its related commands need to be compiled and executed on the server side

2. Different call forms

 If the browser accesses the HTML file, the Http server directly passes an output stream at this time

 Write all the content in the HTML file to the response body

 If the browser accesses the JSP file. At this time, the Http server operates the JSP file according to the JSP specification Edit---->Compile----->Call

JSP file operation principle diagram

 

1. According to the JSP specification, Tomcat will [edit] the accessed JSP file as a java file. This Java file is the Servlet interface implementation class

2. According to JSP specification, Tomcat calls JVM (javac one_jsp.java) to [compile] this java file into class type

3. Tomcat is responsible for generating instance objects of this class file according to the JSP specification . This instance object is a Servelt interface instance object

4. Tomcat calls the _jspService method in the class file through the instance object according to the JSP specification . The _jspService method is responsible for writing the content of the JSP file into the response body at runtime.

Http server [Edit] and [Compile] JSP file location:

  Standard answer: I see this evidence under [work]

 C:\Users\[Login windows system user role name]\.IntelliJIdea2018.3\system\tomcat\[website work space]\work\Catalina\localhost\[website name]\org\apache\jsp

Execution mark

Command format:

 <% int a =10; %> declare local variables

<% boolean flag = 30 >= 40; %> Expressions in Java (mathematical expressions, relational expressions, logical expressions)

<%

if (judgment condition) {  

  }else{   

  }

  while(){   

    }

%> write control statements

Command function: notify the Http server to distinguish the Java command in the JSP file from other ordinary execution results

Output tag

Command format:

<%=java variable name%>

<%=java expression%>

Command function: Instruct Tomcat to write [variable value] in the output tag or [expression calculation result] in the output tag to the response body

JSP built-in objects

Built-in object of JSP file: request

    Type: HTTPServletRequest

    effect:

        Read the relevant information in the request package when the JSP file is running

        Realize data sharing with Servlet in the process of request forwarding

JSP file built-in object: session

    Type: HttpSession

    Function: When the JSP file is running, the session scope object of the current user can be pointed to by the session to add shared data or read shared data

ServletContext application global scope object

Both Servlet and JSP in the same website can realize data sharing through the global scope object of the current website.

Built-in object in JSP file: application

Let's give a case of joint call of Servlet and JSP

1. First create an entity class of the student object

public class Student {
    private Integer sId;
    private String sname;

    public Student() {
    }

    public Student(Integer sId, String sname) {
        this.sId = sId;
        this.sname = sname;
    }

    public Integer getsId() {
        return sId;
    }

    public void setsId(Integer sId) {
        this.sId = sId;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}

2. Create a Servlet interface implementation class, share data to the JSP file through the request scope object, and output

public class OneServlet extends HttpServlet {
    //业务处理,得到处理结果---查询学员信息
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student s1=new Student(100,"zhangsan");
        Student s2=new Student(100,"zhangsan");
        Student s3=new Student(100,"zhangsan");
        List<Student> studentList=new ArrayList();
        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);

        //将共享数据添加到请求作用域对象中
        request.setAttribute("key",studentList);
        //通过请求转发的方式调用stu_show.jsp
        //并将资源报告的请求对象和响应对象通过Tomcat交给stu_show.jsp使用
        request.getRequestDispatcher("/stu_show.jsp").forward(request,response);
    }
}

3. Obtain shared data from the requesting object and traverse the loop

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //从请求作用域对象中获取共享数据
    List<Student> studentList=(List)request.getAttribute("key");
    //循环遍历
%>
<table border="2px" align="center">
    <tr>
        <td>学生编号</td>
        <td>学生姓名</td>
    </tr>
    <%
        for(Student student:studentList){
    %>
    <tr>
        <td><%=student.getsId()%></td>
        <td><%=student.getSname()%></td>
    </tr>
    <%
        }
    %>
</table>

Results of the

Guess you like

Origin blog.csdn.net/qq_45796208/article/details/108824163
jsp