Servlet開發得需要JSP等技術的輔助,我們先來看一下Servlet與表單的應用。
Servlet程序開發---一個實例
由於Servlet本身也存在著HttpServletRequest 和HttpServletResponse對象的聲明,所以既可以使用Servlet接受用戶所提交的內容
我們來以一個實例說明一下:
項目如下:

先做一個表單的頁面
input.html <html> <head> <title>WEB開發</title> </head> <body> <form action="../InputServlet" method="post"> 關於action路徑問題下面會講到 輸入內容:<input type="text" name="info"> <input type="submit" value="提交"> </form> </body> </html>
做好表單頁面後再來做Servlet頁面
InputServlet.java
package ServletDemo
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InputServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{ //覆寫doGet()方法
String info=req.getParameter("info");//接受請求參數
PrintWriter out=resp.getWriter(); //准備輸出
out.println("<html>");
out.println("<head><title>WEB開發</title></head>");
out.println("<body>");
out.println("<h1>"+info+"</h1>");
out.println("</body>");
out.println("</html>");
out.close();//關閉輸出
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{ //處理POST請求
this.doGet(req, resp);//同一種方法體處理
}
}