servlet的使用
生活随笔
收集整理的這篇文章主要介紹了
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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python的可变类型和不可变类型
- 下一篇: 初次使用MyEclipse || Ser