Servlet 处理多种请求应用
生活随笔
收集整理的這篇文章主要介紹了
Servlet 处理多种请求应用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè) Servlet 如何處理多種請(qǐng)求?
1.使用后綴匹配模式
2.在 service 方法中獲取請(qǐng)求資源路徑,分析資源路徑來決定作何處理
示例代碼如下:
package priv.lwx.javaex.web_demo_02.web.servlet;import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 使用Servlet處理多種請(qǐng)求** @author liaowenxiong* @date 2021/12/23 12:00*/public class ServletDemo02 extends HttpServlet {public void service(HttpServletRequest request, HttpServletResponse response) {String uri = request.getRequestURI();String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));System.out.println("uri: " + uri);System.out.println("path: " + path);if (path.equals("/list")) {System.out.println("list...");} else if (path.equals("/del")) {System.out.println("del...");} else if (path.equals("/add")) {System.out.println("add...");}} }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_4_0.xsd"version="4.0"><servlet><servlet-name>demo02</servlet-name><servlet-class>priv.lwx.javaex.web_demo_02.web.servlet.ServletDemo02</servlet-class></servlet><servlet-mapping><servlet-name>demo02</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping> </web-app>總結(jié)
以上是生活随笔為你收集整理的Servlet 处理多种请求应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用JDBC连接数据库(MySQL)的源
- 下一篇: 至少 7 年安全支持,Fairphone