Java Web project creation and some instructions

Development tool: IntelliJ IDEA 2021.1.1

Development environment: JDK 1.8

Technology selection: servlet+jsp+mysql

Technology to help the development process: jstl JSP tag library maven jar package management lombok simplified code

Database creation

CREATE DATABASE shop CHARACTER SET utf8mb4;

CREATE TABLE client_TB(
clientId INT,clientName VARCHAR(20),birthday DATE,sex VARCHAR(2),
PRIMARY KEY (clientId) ,
CHECK (sex IN('male','female')));

CREATE TABLE product(
productId INT ,productName VARCHAR(20),price FLOAT,stock INT ,
PRIMARY KEY(productId)
);

CREATE TABLE order_TB(
orderId INT ,clientId INT,orderDate DATE,papyment FLOAT, stats VARCHAR(5),
PRIMARY KEY (orderId)
);

CREATE TABLE orderProduct(
orderId INT , productId INT,quantity INT,
PRIMARY KEY (orderId,productId)
);

Physical foreign key:

It refers to using FOREIGN KE on data to constrain data

Logical foreign key:

Use logic in the Service layer to get the constraint effect of foreign keys

Logical foreign key used in this project: because the physical foreign key is very inconvenient and troublesome in the actual work of distributed database or database and table division, it is easy to maintain the data in the database by using logical foreign key

1: Create Maven project File → New Project → select Maven project → check javawebapp skeleton

2: Import POM XML file to import the coordinates of the technology required for this project

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>



    </dependencies>

3: Use Tomcat provided with IDEA to test the project just created

TIPS: in every development process, always remember to go back and test

Add Configuration in the upper right corner of IDEA → upper left corner of pop-up window + → select Tomcat Service → Local

The packaged project needs to be run on the server, so the subsequent project operation will be automatically packaged and run on the Tomcat server downloaded locally

Carry out key configuration and Description:

Server

Configure...: select the Tomcat path to download locally

  • It is best to download / select version 8

    1. Due to the technical update, Tomcat is not javax after version 9 Servlet API, but Jakarta The use of servlet API will bring a lot of trouble to the path. Some need Servlet Technology, but it has not been updated to the latest Jakarta Servlet, and then your project will be a lot of red!

    2. 8.0.x version is used by many people and stable: not the latest, but the most stable

URL: when starting the project, the corresponding URL entered in the browser will see your project

http://localhost:80/ Here you can change it to your favorite name/

JRE: use the JRE under the corresponding environment variable, because the local file of Tomcat will call the JRE environment variable on your computer

HTTP port: the port occupied by the server when running

  • The port is sometimes occupied

  • 1: Through the windows command line netstat -ano|findstr port number

  • 2: Return to the first line to find the PID

  • 3: End PID process through command or task manager

Configuration completed → run → enter URL in the browser → the default pop-up is the index in the webapp directory jsp

4: Creation of logical architecture package

Deployment

+No.: add a newly packaged project

+No. → Artifact...: name selection of packaging project

-No.: remove the package project that has been added

1: cn.edu.bnuz.oreder_system this is a domain name written in reverse, which conflicts with the real url at startup

2: controller layer: the control layer of page Jump. This project stores the package of servlet files

3: dao layer: DAO(Date Access Object) JDBC interacts with the database

4: service layer: logical processing of data

5: utils package: implementation of some tool classes: for example, jdbcutil Class: simplify jdbc connection and release

6: entity package: store the Client, Product and Order objects of this project

7: resources package: storage of configuration files: for example, the configuration files of this database connection

8: webapp: storage of front-end pages

9: WEB-INF: web.xml storage (non modifiable)

10: target: storage of the project after packaging

Process of data in Java Web project:

data→view→controller→service→dao→ DataBase→file

JSP 1 → servlet: here is JSP 1 JSP and on the web Set a xxservlet mapping path on XML to / servlet

demo_web/jsp1.jsp → demo_web/servlet

jsp page Jump:

No value transfer:

<%--Used el expression--%>
${pageContext.request.contextPath}/servlet


Value transfer (no space between all values after?:

<%--Used el expression--%>
${pageContext.request.contextPath}/servlet?key1=value1[&key2=value2.....]

Jsp1 → servlet obtains value from jsp1 → jsp2:

servlet get value

request.getParameter("Parameter name");  Get the parameter value according to the parameter name (note that only parameters with one value can be obtained)

request.getxxxx has a lot to learn!!!

servlet pass value

type value=xxx;
type2 value2=xxx;
request.setAttribure("key",value)
request.setAttribure("key2",value2)

servlet page Jump

//forward
request.getRequestDispatcher("xx.jsp").forward(request,response)
//redirect
respond.sendRedirect(req.getContextPath()+"/servlet");

request.getRequestDispatcher() and response Sendredirect() difference
google!!!

github source code
https://github.com/zslkdjaw/study_notes/tree/servlet-jsp-mysql-orderSystem

Application development of order system design pattern based on Database

code implementation

summary

Tags: Java Front-end intellij-idea

Posted by wint.thaw on Sun, 15 May 2022 04:23:34 +0300