一 tomcat的安裝部署
1.?安裝tomcat
web程序運(yùn)行,需要tomcat環(huán)境。提前安裝好tomcat
2.配置tomcat環(huán)境
點(diǎn)擊 windows ->preference ,在彈出窗口的左側(cè)選中 server -> Runtime enviroment 。
點(diǎn)擊Add ,根據(jù)安裝的tomcat版本選擇 tomcat的類(lèi)型后,點(diǎn)擊finish 按鈕
?
?
?3.配置tomcat server
點(diǎn)擊鏈接創(chuàng)建tomcat服務(wù)器
?
?
將web程序添加到右側(cè)列表
?
??
?三 創(chuàng)建一個(gè)簡(jiǎn)單的基于maven的 java web項(xiàng)目
1 打開(kāi)Eclipse
2.新建基于maven的web項(xiàng)目
1)點(diǎn)擊菜單 File -> New ->?Maven Project,然后點(diǎn)擊Next。
2)在"Select Project"頁(yè)面中,設(shè)置項(xiàng)目路徑,然后點(diǎn)擊 Next。
? ?3)? 在"Select Archetype"頁(yè)面中,選擇"maven-archetype-webapp",然后點(diǎn)擊Next。
4.)在"New Maven Project"頁(yè)面中,輸入Group Id和Artifact Id 后點(diǎn)擊Finish。
group id 輸入:cn.nfu.caohongxing
Aritifact id 輸入:book-mananger-system
Version中輸入:1.0.0
?錯(cuò)誤的處理:?
?有的同學(xué),點(diǎn)擊finish時(shí),會(huì)出現(xiàn)下面的錯(cuò)誤,只需要點(diǎn)擊右鍵菜單 run as -> Maven install
?出現(xiàn)上面的錯(cuò)誤
5) 在"src/main"文件夾下,創(chuàng)建一個(gè)名為"java"的文件夾。這將是您的Java源代碼目錄。
?
6)?在"java"文件夾下,創(chuàng)建一個(gè)Java類(lèi)文件mvnServerlet.java,并在該類(lèi)中編寫(xiě)以下代碼:
?
?輸入代碼
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class mvnServerlet extends HttpServlet {
? ? private static final long serialVersionUID = 1L;
? ? protected void doGet(HttpServletRequest request, HttpServletResponse response)
? ? ? ? ? ? throws ServletException, IOException {
? ? ? ? response.getWriter().append("Hello, World!");
? ? }
}
此時(shí)javax.servlet 會(huì)飄紅,原因時(shí)包沒(méi)有導(dǎo)入
7).在pom.xml中添加 javax.servlet的外部依賴(lài)
? <dependency>
? <groupId>javax.servlet</groupId>
? <artifactId>javax.servlet-api</artifactId>
? <version>4.0.1</version>
? <scope>provided</scope>
? </dependency>
?為了讓設(shè)置生效,需要重啟eclipse
如果還提示出錯(cuò),更新maven
8)?編輯項(xiàng)目的"src/main/webapp"文件夾下index.jsp,并在該文件中編寫(xiě)以下代碼:
<html> <body> <h2>Hello World! I am ?comming</h2> </body> </html>
9). 在項(xiàng)目的"src/main/webapp/WEB-INF文件夾下編輯 web.xml,例如以下代碼:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>MyWebApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvnServerlet</servlet-name>
<servlet-class>mvnServerlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvnServerlet</servlet-name>
<url-pattern>/mvnServerlet</url-pattern>
</servlet-mapping>
</web-app>
10) 編譯打包項(xiàng)目,右鍵點(diǎn)擊"pom.xml"文件,選擇"Run As -> Maven install",等待編譯和打包完成。
點(diǎn)擊 windows -> show view ,在彈出窗口中選擇”server“
點(diǎn)擊右鍵 start??啟動(dòng)tomcat?
?等待Tomcat啟動(dòng)后,在瀏覽器中訪問(wèn):http://localhost:8166/book-manager-system/
"Hello, World! i am comming"。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-767826.html
以上就是一個(gè)簡(jiǎn)單的完整的基于maven的web項(xiàng)目的創(chuàng)建過(guò)程。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-767826.html
到了這里,關(guān)于eclipse創(chuàng)建一個(gè)基于maven的web項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!