?? servletexample.java
字號:
//Servlet小程序結構示意
import Javax.servlet.*;//從Java擴展包中導入標準的Servlet API
import Javax.servlet.http.*;//基于HTTP協議,從擴展包中導入HTTP API
import Java.io.*;//一般需要通過Writer或OutputStream向響應中寫入信息,所以導入Java中的IO API
public class ServletDemo extends HttpServlet{//用HttpServlet來專門處理HTTP通信協議
public void init(ServletConfig config) throws ServletException{
super.init(config);
//在Servlet運行之前進行一些初始化的設置
}
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{//Servlet程序默認的入口方法
String clientHostName=req.getRemoteHost();
String clientIPAddress=req.getRemoteAddr();
//使用HttpServletRequest對象req來讀取與客戶端請求相關的信息
res.setContentType("text/html");
//通過HttpServletResponse對象res來設置一些與發往客戶端響應相關的信息
PrintWriter out=res.getWriter();//取得一個PrintWriter對象out來向客戶端寫入字符格式的信息
out.println("<html>");//向客戶端輸入信息
out.println("<head><title>");
out.println("Servlet演示程序");
out.println("</title></head>");
out.println("<body>");
out.println("<p align=center>這是一個Servlet測試程序");
out.println("<p align=center><h1>Hello,Java</h1>");
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Get"請求進行處理
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Post"請求進行處理
}
public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Put"請求進行處理
}
public void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Delete"請求進行處理
}
public void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Options"請求進行處理
}
public void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
//對客戶端的"Trace"請求進行處理
}
public void destroy(){
//在Servlet卸載之前進行一些處理工作
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -