日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

servlet的使用

發布時間:2025/4/16 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 servlet的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一個JavaEE代碼

package com.dym.servlet;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 MyServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.getWriter().write("this is my first servlet.");System.out.println("this is my first servlet.");}} <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><!-- 配置servlet --><!-- 配置servlet類路徑 --><servlet><servlet-name>my</servlet-name><servlet-class>com.dym.servlet.MyServlet</servlet-class></servlet><!-- 配置訪問方式 --><servlet-mapping><servlet-name>my</servlet-name><url-pattern>/my</url-pattern></servlet-mapping> </web-app>
package com.dym.servlet;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 MyServlet2 extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.getWriter().write("this is my second servlet"); // write("這里面不允許寫中文") System.out.println("MyServlet2.service()");} }

?


web.xml? ?使服務器能夠找到對應的servlet,最大的好處:安全性

web.xml 文件的配置是為了保護 servlet

servlet的使用:


1、 創建普通的 java 類并繼承 HttpServlet
2、 覆寫 service 方法
3、 在 service 方法中書寫邏輯代碼即可

4、 在 webRoot 下的 WEB-INF 文件夾下的 web.xml文件中配置 servlet

Servlet 的 web.xml 配置:

?

總結

以上是生活随笔為你收集整理的servlet的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。