A Java Servlet is a program that runs on a Web server or application server as an intermediary layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server.
Using Servlets, you can collect user input from web forms, render records from a database or other sources, and dynamically create web pages.
Servlet form data syntax
In many cases, some information needs to be passed from the browser to the web server and ultimately to the background program. The browser uses two methods to pass this information to the web server, the GET method and the POST method.
Servlet form data example
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?方法請求的方法???? ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{???????? ????doGet(request,?response);???? ????}}