Java Servlet 是運(yùn)行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來(lái)自 Web 瀏覽器或其他 HTTP 客戶端的請(qǐng)求和 HTTP 服務(wù)器上的數(shù)據(jù)庫(kù)或應(yīng)用程序之間的中間層。
使用 Servlet,您可以收集來(lái)自網(wǎng)頁(yè)表單的用戶輸入,呈現(xiàn)來(lái)自數(shù)據(jù)庫(kù)或者其他源的記錄,還可以動(dòng)態(tài)創(chuàng)建網(wǎng)頁(yè)。
Servlet 表單數(shù)據(jù) 語(yǔ)法
很多情況下,需要傳遞一些信息,從瀏覽器到 Web 服務(wù)器,最終到后臺(tái)程序。瀏覽器使用兩種方法可將這些信息傳遞到 Web 服務(wù)器,分別為 GET 方法和 POST 方法。
Servlet 表單數(shù)據(jù) 示例
package cn.php.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloForm */@WebServlet("/HelloForm") public class HelloForm extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloForm() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = "使用 GET 方法讀取表單數(shù)據(jù)"; // 處理中文 String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8"); String docType = "<!DOCTYPE html> \n"; out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<ul>\n" +" <li><b>站點(diǎn)名</b>:"+ name + "\n" +" <li><b>網(wǎng)址</b>:"+ request.getParameter("url") + "\n" +"</ul>\n" +"</body></html>"); } // 處理 POST 方法請(qǐng)求的方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
熱門推薦
- 關(guān)于Form enctype="multipart/form-data"
- Python發(fā)送form-data請(qǐng)求及拼接form-data內(nèi)容的方法
- 關(guān)于Form enctype="multipart/form-data"解決方案
- 關(guān)于Form enctype="multipart/form-data"解決方案
- multipart/form-data和application/x-www-form-urlencoded的區(qū)別
- 淺析application/x-www-form-urlencoded和multipart/form-data的_PHP