Javaweb project framework construction-preparation

Javaweb project framework construction-preparation

Preface:
It has been almost two years since I started learning Java in my sophomore year and it is now my senior year. However, because I have been playing around before and have not studied it seriously, I have only started to learn it again now. It was also a coincidence that I saw Teacher Huang Yong's "Architecture Adventure", so I started learning to write Java Web framework.

1. Development Tools
The book "Architecture Adventure" mentions that IntelliJ IDEA (IDEA for short) is the best Java development tool in the industry, but it has two versions. A community version costs no money, and it is an open source personal version, but it has many functions. It is not comprehensive. The other is the full version, which is a paid enterprise version and has comprehensive functions. So based on the purpose of using the best one, I resolutely downloaded the paid version. During the installation process, I also discovered a small bug in this development tool. Because the paid version of this tool has a 30-day trial period, we You can modify the computer system time to six months later before installation. For example, if it is December 2016, we can change it to June 2017. In this way, after the installation is completed, its trial period will be until July 2017, and then it will be closed first. The software will be OK if you change the system time back. Its trial period is still July 2017.

The subsequent installation process will be relatively simple. One thing to remind you is that it is best not to change too much. The postponement time is within one year. If the installation exceeds one year, an error will be reported.

2. Make a small project.
Before starting formal work, first make a Hello World (it seems that all development is inseparable from this step). The first is to get familiar with Maven, and the second is to review Servlet and JSP. start! ! !

1. Create a Maven project
Maven is a project management and build automation tool. However, for programmers, its project construction function is more important. But for using IDEA as a development tool, there is no need to download and configure Maven separately, because IDEA integrates it by default. Creating a Maven project using IDEA is very simple:
  a. Click File/New/Project on the toolbar in the upper right corner.
  b. Select Maven in the pop-up box, and then Next.
  c. In the next window, these three are required and very important. GroupId is recommended as an inversion of the website domain name to ensure uniqueness, similar to Java's package name; ArtifactId is the module name, so you can name it yourself.
  d. Continue to Next, enter Project name, and then Finish.
At this point, the Maven project is created.

2. Configure Maven
to open the Maven configuration file pom.xml, and then perform a series of configurations. After the configuration is completed, it will look like:

Copy code
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">             
   <modelVersion>4.0.0</modelVersion>    
   <groupId>org.smart4j</groupId>    
   <artifactId>chapter1</artifactId>    
   <version>1.0.0</version>
   <properties>    
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <build>    
     <plugins>        
       <!-- Compile -->        
       <plugin>            
          <groupId>org.apache.maven.plugins</groupId>            
          <artifactId> maven-compiler-plugin</artifactId>            
          <version>3.3</version>            
          <configuration>                
            <source>1.8</source>                
            <target>1.8</target>            
          </configuration>        
       </plugin>        
       <!-- Test -->        
       <plugin>            
         <groupId>org.apache.maven.plugins</groupId>             
         <artifactId>maven-surefire-plugin</artifactId>            
         <version>2.18.1</version>            
         <configuration>                
           <skipTests>true</skipTests>            
         </configuration>        
       </plugin>        
       <!-- Tomcat -->        
       <plugin>            
         <groupId>org.apache.tomcat.maven</groupId>            
         <artifactId>tomcat7-maven-plugin</artifactId>             
         <version>2.2</version>            
         <configuration>                
            <path>/${project.artifactId}</path>            
         </configuration>        
       </plugin>    
     </plugins>
   </build>

   <packaging>war</packaging>  
  
   <dependencies>        
     <!-- Servlet -->        
     <dependency>            
       <groupId>javax.servlet</groupId>            
       <artifactId>javax.servlet-api</artifactId>            
       <version>3.1.0</version>            
       <scope>provided</scope>        
     </dependency>        
     <!-- JSP -->        
     <dependency>            
       <groupId>javax.servlet.jsp</groupId>            
       <artifactId>jsp-api</artifactId>            
       <version>2.2</version>            
       <scope>provided</scope>        
     </dependency>        
     <!-- JSTL -->        
     <dependency>            
       <groupId>javax.servlet</groupId>            
       <artifactId>jstl</artifactId>            
       <version>1.2</version>            
       <scope>runtime</scope>        
     </dependency>    
   </dependencies>  
</project>
Copy code

The first is to configure the encoding format to UTF-8:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>

Then configure JDK to 1.8 (this depends on the individual, because the JDK version in my computer is 1.8) and Tomcat to 7:

Copy code
<build>    
   <plugins>        
   <!-- Compile -->        
   <plugin>            
      <groupId>org.apache.maven.plugins</groupId>            
      <artifactId> maven-compiler-plugin</artifactId>            
      <version>3.3</version>            
      <configuration>                
         <source>1.8</source>                
         <target>1.8</target>            
         </configuration>        
   </plugin>        
   <!-- Test -->        
   <plugin>            
      <groupId>org.apache.maven.plugins</groupId>             
      <artifactId>maven-surefire-plugin</artifactId>            
      <version>2.18.1</version>            
      <configuration>                
         <skipTests>true</skipTests>            
      </configuration>        
   </plugin>        
   <!-- Tomcat -->        
   <plugin>            
      <groupId>org.apache.tomcat.maven</groupId>            
      <artifactId>tomcat7-maven-plugin</artifactId>             
      <version>2.2</version>            
      <configuration>                
         <path>/${project.artifactId}</path>            
      </configuration>        
   </plugin>    
 </plugins>
</build>
Copy code

Then configure the packaging type as war package:

<packaging>war</packaging>

Finally, configure Servlet, JSP, and JSTL (Maven coordinates must be provided. The subsequent Servlet and JSP have corresponding jar packages because Tomcat comes with them, so the scope is set to provided; while JSTL is required at runtime, not during compilation, so scope is set to runtime):

Copy code
<dependencies>        
   <!-- Servlet -->        
   <dependency>            
      <groupId>javax.servlet</groupId>            
      <artifactId>javax.servlet-api</artifactId>            
      <version>3.1.0</version>            
      <scope>provided</scope>        
   </dependency>        
   <!-- JSP -->        
   <dependency>            
      <groupId>javax.servlet.jsp</groupId>            
      <artifactId>jsp-api</artifactId>            
      <version>2.2</version>            
      <scope>provided</scope>        
   </dependency>        
   <!-- JSTL -->        
   <dependency>            
      <groupId>javax.servlet</groupId>            
      <artifactId>jstl</artifactId>            
      <version>1.2</version>            
      <scope>runtime</scope>        
   </dependency>    
</dependencies>
Copy code

At this point, the Maven configuration is completed (it is indeed much simpler than the configuration of Eclipse and the like).


3. It only takes 3 steps to convert to a Java Web project
, which is easy to do. a. Create a new webapp directory under the main directory.
b. Create a new WEB-INF directory under the webapp directory.
c. Create a new web.xml in the WEB-INF directory.
At this time, there will be a prompt in the lower right corner of the screen:

Then it means that the web project has been created.

4. Write the Servlet class and JSP page.
Create a new package and package name in the java directory under the main directory, and then create a new Servlet (HelloServlet). This Servlet is responsible for page jumps and converts the current time into a standard format and passes it to the page. (One thing here is that when I followed the steps of Teacher Huang Yong, the path when jumping to the Servlet page was "/WEB-INF/jsp/hello.jsp", but the page kept reporting an error message 404. Later I searched for information and found it. The page cannot be written in the "/WEB-INF" directory, so it can be displayed as follows):

Copy code
package org.smart4j.chapter1;
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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet{    
      @Override    
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        
       DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
       String currentTime = dateFormat.format(new Date());        
       req.setAttribute("currentTime", currentTime);        
       req.getRequestDispatcher("/jsp/hello.jsp").forward(req, resp);   
   }
}
Copy code

Then create a new JSP page responsible for display, which receives the parameters passed by the Servlet and displays them through JSTL expressions:

Copy code
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
  <title>Hello</title>
 </head>
 <body>
  <h1>Hello!</h1>
  <h2>当前时间为:${currentTime}</h2>
 </body>
</html>
Copy code

5. Configure Tomcat.
After writing the page and Servlet, you need to configure a server so that what you write can be displayed. You must configure the Tomcat server here:
  a. Click "Edit Configurations..." in the toolbar in the upper right corner (this is a drop-down box).
  b. Click the "+" button in the upper left corner and select "Tomcat Server/Local".
  c.Name a name yourself, and then uncheck After launch.
  d. Click the "Configure..." button on the right side of the Application server to configure Tomcat (this will automatically help you find Tomcat on your computer).
  e. Switch to the Deployment tab, click the "+" button on the right, select the "Artifact..." option, select "Project name: war exploded" in the pop-up box, click OK, and then enter the project name in the Application context.
  f. Switch back to the Server tab, select the "Update resources" option in the On frame deactivation drop-down box, and click OK.
At this point, Tomcat configuration is complete.

6. Upload to git

Guess you like

Origin blog.csdn.net/listener_life/article/details/79962929
Recommended