INPUT标识符用来定义表单中的输入域,如文本输入域和按钮。图8.2显示了input标识符的基本语法的结构。 TYPE属性作为必要属性指定所使用输入域类型(本部分将在下小节讲述)。而同是必要属性的NAM属性则是指定相应域提供给服务器时的名称。注意,你应当很小心地命名各个输入域。我建议你要避免使用任何一个特殊字符(下划线除外)并且参数的首字符要使用字母。特别注意不要使用+、&或%这些符号。它们在使用“application/x-www-form-rulencoded”编码方式时都具有特殊的意义。 注意,Microsoft
Internet Explorer和Netscape
Navigator还定义了一些扩展的属性。但在这里,我们将主要讲解那些在HTML规范中定义的标准属性。 input type NAME=parameter
name [additional attributes]> 图8.2
HTML语言input标识符基本语法结构
button类型 通过使用按钮类型(button input
tpye),你可以创建一个可以被表单用户单击的按钮,但单击后并不会提交表单或是生置表单。表8.3列出了按钮类型的属性。 表8.3
按钮的属性 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 属性 是否必需 描述 ───────────────────────────────── NAME 是 按钮的名称 VALUE
是 按钮上的标签
───────────────────────────────── 你可能要问,既然一个按钮既不能提交也不能清空表单,那么有什么用呢?好的,除非你使用JavaScript执行一些运作,这个问题的答案是确实毫无用处。尽管如此,图8.3是一个HTML源代码,它向用户介绍含有多个按钮的实例。图8.4显示了在实际运行中这些按钮的情况。
This is a simple servlet that will echo survey
information * that was entered into an HTML form. */
public class EchoSurvey extends HttpServlet { /** *
Performs the HTTP POST operation * * @param req The request
from the client * @param resp The response from the
servlet */
public void doPost(HttpServletRequest
req, HttpServletResponse resp) throws ServletException,
java.io.IOException { // Set the content type of the
response resp.setContentType("text/html");
// Create a PrintWriter to write the response java.io.PrintWriter
out = new java.io.PrintWriter(resp.getOutputStream());
// Print a standard
header out.println(""); out.println("
// Get the
name String name = req.getParameter("name"); out.println("Name=" +
name + " ");
// Get the email address String email =
req.getParameter("email"); out.println("Email=" + email +
" ");
// Get the age String age =
req.getParameter("age"); out.println("Age=" + age + " ");
// Get the operating system. There could be more than one //
value String values[] =
req.getParameterValues("os"); out.print("Operating Systems="); if
(values != null) { for (int i = 0; i < values.length; i++)
{ if (i > 0) out.print(",
"); out.print(values[i]); } } out.println(" ");
// Get the 'more information' flag String more =
req.getParameter("more"); out.println("More information=" + more +
" ");
// Get the comments String comments =
req.getParameter("comments"); out.println("Comments: "); out.println("
");
//
Comment lines are separated by a carriage return/line feed // pair -
convert them to an HTML line break
out.println(toHTML(comments)); out.println("");
out.println("");
//
Wrap
up out.println(""); out.println(""); out.flush(); }
/** *
Initialize the servlet. This is called once when
the * servlet is loaded. It is guaranteed to complete before any *
requests are made to the servlet * * @param cfg Servlet
configuration information */
public void init(ServletConfig cfg) throws
ServletException { super.init(cfg); }
/** *
Destroy the servlet. This is called once when the
servlet * is unloaded. */
public void destroy() { super.destroy(); }
/** *
Convert any carriage return/line feed pairs
into * an HTML line break command ( ) * * @param line
Line to convert * @return line converted line */ private
String toHTML(String line) { String s = "";
if (line == null) { return null; }
// Cache the length of the line int lineLen =
line.length();
// Our current position in the source line int
curPos = 0;
// Loop through the line and find all of the carriage // return
characters (0x0D). If found, convert it into // an HTML line break
command ( ). If the following // character is a line feed (0x0A)
throw it away while (true) {
// Make sure we don't run off the end of the line if (curPos
>= lineLen) { curPos = 0; break; }
int
index = line.indexOf(0x0D, curPos);
// No more characters found if (index == -1)
{ break; }
// Add any data preceding the carriage return if (index >
curPos) { s += line.substring(curPos, index); }
// Add the line break command s += " ";
// Adjust our position curPos = index + 1;
// If the
next character is a line feed, skip it if (curPos < lineLen)
{ if (line.charAt(curPos) == 0x0A)
{ curPos++; } } }
// Be sure to add anything after the last carriage return //
found if (curPos > 0) { s +=
line.substring(curPos); } return s; } } 图8.2
servlet程序EchoSurvey代码清单