pow.xml => 標記了我們用了哪些dependencies
java => 存放我們Servlet的地方,昨天我們做的HelloServlet就是用了裡面的資料
WEB-INF => 裡面的資源不會被列入應用程式根目錄存取的地方,就是放一些不希望瀏覽器直接存取的東西的地方
lib => 就是放我們要用的jar file的地方
昨天的Servlet在今天我注意到我沒點擊Helle Servlet連結進去,後來才發現有404錯誤
後來才發現如果import的是jakarta你的Tomcat版本就要選擇10以上的才可以使用
我們去java資料夾下面創建一個資料夾(exercise) => 建立Servlet檔案 => 產生一個Servlet
package exercise;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "testServlet", value = "/testServlet")
public class testServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把Getrequest我們要做的事情放進去裡面
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
把我們要寫的code寫進去
所有html程式碼都要包在html裡面
因此我們println了html標籤和body標籤(網頁身體的部分)
package exercise;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "testServlet", value = "/testServlet")
public class testServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把Getrequest我們要做的事情放進去裡面
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p>我們自己寫的servlet!!</p>");
out.println("</body>");
out.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
去找到我們webapp目錄中的web.xml打開
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
// 寫程式碼的地方
</web-app>
在寫程式碼的地方
寫上servlet + tab => 就會形成一個標籤
在servlet標籤裡面建立
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>exercise.testServlet</servlet-class>
</servlet>
</web-app>
接著servlet-name那逼會顯示需要mapping
就加上servlet-mapping的標籤,在裡面建立
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>exercise.testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
接著再去我們的網址後面加上"/test" => 前往我們的servlet
package exercise;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "testServlet", value = "/testServlet")
public class testServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
System.out.println("呼叫init方法");
}
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.service(request, response);
System.out.println("呼叫service方法");
}
@Override
public void destroy(){
super.destroy();
System.out.println("呼叫destroy方法!!!!");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把Getrequest我們要做的事情放進去裡面
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p>我們自己寫的servlet!!</p>");
out.println("</body>");
out.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
在HTTP中,客戶端可以向伺服器發送的請求有很多種: