EL expressions improve JSP

content

EL expressions improve JSP

Disadvantages of JSP

EL expression

Common problems when using Maven in idea

Using maven in idea cannot create packages/classes

Configure web maven project in idea

Unable to create servlet in web created by maven

 Deploy tomcat plugin in maven

Walkthrough of EL Expressions


EL expressions improve JSP

Disadvantages of JSP

Since in the jsp page, both HTML tags and java code can be defined, resulting in the following problems

1. Writing trouble: especially complex pages

2. Difficulty reading

3. High complexity: operation needs to depend on various environments, JRE. JSP container (tomcat server).....

4. Occupies memory and disk: JSP will automatically generate .java and .class files to occupy disk, and running .class files to occupy memory

5. Difficulty debugging: After an error occurs, you need to find the automatically generated .java file for debugging

6. Unfavorable team writing: front-end and back-end staff

...

JSP has gradually withdrawn from the stage of history, replaced by html and Ajax

Evolution process:

It is best not to write java code directly in jsp

EL expression

Expression Language for simplifying java code inside JSP pages

Main function: get data

Syntax: ${expression}

For example: ${brands}: Get the data stored in the domain whose key is brands

Common problems when using Maven in idea

Using maven in idea cannot create packages/classes

If there is no source file, add one of the files as a source file to create a package or class (marked in blue in the project structure)

Configure web maven project in idea

Unable to create servlet in web created by maven

 Deploy tomcat plugin in maven

write in the pom.xml file

<build>
    <plugins>
<!--  tomcat插件    -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!--设置端口号(可以修改)-->
          <port>8080</port>
          <!--设置路径(可以修改)-->
           <path>/jsp-demo02</path>
        </configuration>
      </plugin></plugins>
  </build>

Walkthrough of EL Expressions

Create a com.web.ServletDemo1 class

package com.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //添加数据
        List<String> list=new ArrayList<>();
        list.add("zhangSan1");
        list.add("zhangSan2");
        list.add("zhangSan3");
        list.add("zhangSan4");
        list.add("zhangSan5");
        System.out.println(list);
        //2、存储到request域中,可以转发到jsp页面中从而使用EL表达式
        request.setAttribute("lists", list);
        //3、转发到el-demo.jsp
request.getRequestDispatcher("/el-demo.jsp").forward(request, response);


    }
}

Create el-demo.jsp in the web.app directory and write ${lists} in it

<%@ page language="java" contentType="text/html; charset=utf-8"
         isELIgnored="false"
         pageEncoding="utf-8" %>

<html>
<head>

    <meta charset="utf-8"/>
    <title>Insert title here</title>
</head>
<body>
${lists}
</body>
</html>

operation result

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/123014503