1. 程式人生 > >jsp-servlet(1)環境搭建(Tomcat和myeclipse)和基本概念

jsp-servlet(1)環境搭建(Tomcat和myeclipse)和基本概念

1 Tomcat安裝

下載並解壓;

點選bin目錄下的start.bat檔案啟動(這裡可能會報錯,initinternal failed ,檢查8080埠是不是被佔用了,然後重新啟動);

訪問localhost:8080即可轉到下面的官方網站,因為I:\apache-tomcat-8.5.37\webapps\ROOT下面部署了該網站的內容。

Apache Tomcat/8.5.37

 

2 myeclipse安裝

https://blog.csdn.net/qq_41928258/article/details/80055331

破解時出現問題:點選crack.bat一閃而過。

解決方法:1)jdk.11版本太高,安裝jdk1.8   2)把破解的檔案複製到MyEclipse的安裝根目錄下,再點選crack.bat

(這兩步可能只需要做第二步,如果不行,就先做第一步。)

 

3 在myeclipse2017中啟動MyEclipse Tomcat8.5(自帶的)

在控制視窗點選執行按鈕,在瀏覽器輸入localhost:8080就可以開啟apache tomcat網站。

另外:

Windows-preferences-server-runtime environment-add-選擇Tomcat的安裝目錄

 

4 在myeclipse中新建web專案

1)new web project

2)new HTML頁面

3)釋出專案

4)啟動服務,訪問

 

注:可能出現的問題:

 /hello專案中index.jsp被我刪掉了:

 

/hellotest專案中的index.jsp被保留著:

上述問題的原因不明。

------更新-----

 是因為在我的hello專案中,根目錄下只有hello.html和index.jsp檔案;在hello/WEB-INF/web.xml的welcome語句中,是這幾句話,所以當不輸入http://localhost:8080/hello/hello.html或者http://localhost:8080/hello/index.jsp,它預設值就找不到檔案,所以。會報錯

<welcome-file
>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file>

 

 

 

5 servlet概述

視訊來源http://www.sxt.cn/jsp-servlet/servlet.html

1)servlet是什麼

a)概念:是一個java類

b)作用:是一個伺服器端的小程式,處理使用者請求。

c)為什麼要有Servlet:因為傳統的Java是不能處理網路應用程式的,所以引入了新的API,該Servlet類能響應網路請求。

 

2)servlet的實現

a)實現Servlet介面。

b)繼承GenericServlet類

b)繼承HttpServlet類

 

3)編寫Servlet,實現一個Servlet介面

a)新建一個web 專案,並在SRC新建一個package,以及一個class,注意命名規範

package cn.piggy.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.rowset.serial.SerialException;

public class HelloServlet implements Servlet{
public void destrory() {

}

public ServletConfig getServletConfig() {
return null;
}

public String getServletInfo() {
return null;
}

public void init(ServletConfig config) throws ServletException{

}

//處理請求
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{
res.getOutputStream().print("Hello servlet!");
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}


}

  

 

b)在web.xml中部署Servlet

c)釋出

 

6 Servlet執行週期

因此:返回404錯誤時,需要檢查路徑是否輸對,或者web.xml中存在該檔案。

 

7 Servlet生命週期