Servlet03 11.練習 快捷鍵-可以快速地在訪問的文件件切換 ctrl+alt+向左箭頭:回到上次訪問的位置 ctrl+alt+向右箭頭:回到下一步訪問的位置 11.1CatServlet 首先創建項目servlet,配置好Tomcat,添加web應用支持。在web目錄下麵的WEB-INF目 ...
Servlet03
11.練習
快捷鍵-可以快速地在訪問的文件件切換
ctrl+alt+向左箭頭:回到上次訪問的位置
ctrl+alt+向右箭頭:回到下一步訪問的位置
11.1CatServlet
首先創建項目servlet,配置好Tomcat,添加web應用支持。在web目錄下麵的WEB-INF目錄下創建lib目錄,在Tomcat安裝包的lib目錄中找到servlet-api,並添加到idea的web目錄下麵的WEB-INF/lib目錄,選擇引用為庫文件
在src目錄下創建目錄,編寫CatServlet類:
package servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class CatServlet implements Servlet {
public int count = 0;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
count++;//每次訪問service方法,count累加
System.out.println("該servlet訪問次數=" + count);
//獲取請求的方式
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String method = httpServletRequest.getMethod();
if (method.equals("GET")) {
doGet();
} else if (method.equals("POST")) {
doPost();
}
}
public void doGet() {
System.out.println("CatServlet doGet()...");
}
public void doPost() {
System.out.println("CatServlet doPost()...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
在web.xml文件中進行Servlet配置:
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>CatServlet</servlet-name>
<servlet-class>servlet.CatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CatServlet</servlet-name>
<url-pattern>/cat</url-pattern>
</servlet-mapping>
</web-app>
在WEB-INF目錄下編寫一個html,用於測試
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/cat" method="get">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
點擊啟動Tomcat,在瀏覽器中輸入地址:http://localhost:8080/servlet/login.html
點擊login按鈕,成功跳轉到CatServlet中:
後臺輸出:(如果表單方式改變了,仍然調用的是舊的訪問方法,點擊保存html,並刷新瀏覽器頁面即可解決)
11.2DogServlet
在11.1的項目框架上進行編程
首先在src目錄下創建DogServlet類
package servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DogServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
getCount++;
System.out.println("getCount=" + getCount);
System.out.println("DogServlet-訪問doGet()");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
postCount++;
System.out.println("postCount=" + postCount);
System.out.println("DogServlet-訪問doPost()");
}
}
註意:這裡沒有重寫service方法,因此當瀏覽器請求DogServlet時,會去父類HttpServlet類中去找調用的service方法,在HttpServlet類中的service方法會根據請求類型如get、post去調用當前運行類型(即DogServlet)的doPost、doGet方法,因此在自己創建的Servlet中不用判斷請求方式
在web.xml文件中配置該Servlet:
<servlet>
<servlet-name>DogServlet</servlet-name>
<servlet-class>servlet.DogServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DogServlet</servlet-name>
<url-pattern>/dog</url-pattern>
</servlet-mapping>
修改之前的login.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/dog" method="post">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
點擊redeploy Tomcat,在瀏覽器輸入地址http://localhost:8080/servlet/login.html
,點擊login按鈕,成功跳轉到DogServlet
將login.html文件中表單提交方式反覆該為post或者get,多次訪問,後臺輸出如下:
(如果表單方式改變了,仍然調用的是舊的訪問方法,點擊保存html,並刷新瀏覽器頁面即可解決)
11.3PigServlet
在11.1的項目框架上進行編程
在src目錄下編寫PigServlet
package servlet;
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;
@WebServlet(urlPatterns = {"/pig1", "/pig2"},loadOnStartup = 1)
public class PigServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
getCount++;
System.out.println("PigServlet-訪問doGet()-get訪問次數=" + getCount);
System.out.println("客戶端地址="+req.getRemoteAddr());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
postCount++;
System.out.println("PigServlet-訪問doPost()-post訪問次數=" + postCount);
System.out.println("客戶端地址="+req.getRemoteAddr());
}
}
修改login.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/pig1" method="get">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
點擊redeploy Tomcat,在瀏覽器輸入地址http://localhost:8080/servlet/login.html
,點擊login按鈕,成功跳轉到PigServlet
將login.html文件中表單提交方式反覆改為post或者get,多次訪問,後臺輸出如下:
(如果表單方式改變了,仍然調用的是舊的訪問方法,點擊保存html,並刷新瀏覽器頁面即可解決)