Java Tutorial/JSP/Form Input Data
Содержание
Form with Error Checking
index.jsp
<HTML>
<HEAD><TITLE>Implicit Object Project</TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="StreamResponse.jsp">
<H2> Streaming file demo </H2>
<TABLE>
<TR>
<TD> Enter File name: </TD>
<TD> <INPUT TYPE="text" SIZE=60 NAME="File"> </TD>
</TR>
<TR>
<TD> Enter optional MIME type: </TD>
<TD> <INPUT TYPE="text" SIZE=30 NAME="MIME type"> </TD>
</TR>
</TABLE>
<P>
<INPUT TYPE=submit VALUE="Fetch">
<INPUT TYPE=reset VALUE="Reset">
</FORM>
</BODY>
Pass Form Value to Bean
Jsp code
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jsp:useBean id="cb" scope="session" class="beans.MyBean" />
<jsp:setProperty name="cb" property="*" />
<%
cb.processRequest(request);
%>
<body bgcolor=<%= cb.getColor1() %>>
<font size=6 color=<%= cb.getColor2() %>>
<P>
<% if (cb.getHint()==true) { %>
<P> Hint #1: Vampires prey at night!
<P> Hint #2: Nancy without the n.
<% } %>
<% if (cb.getSuccess()==true) { %>
<P> CONGRATULATIONS!!
<% if (cb.getHintTaken()==true) { %>
<P> ( although I know you cheated and peeked into the hints)
<% } %>
<% } %>
Total attempts so far: <%= cb.getAttempts() %>
<form method=POST action=index.jsp>
Color #1: <input type=text name= color1 size=16>
<br>
Color #2: <input type=text name= color2 size=16>
<P>
<input type=submit name=action value="Submit">
<input type=submit name=action value="Hint">
</form>
</font>
</body>
</html>
Read Hidden Control in Same File
<HTML>
<HEAD>
<TITLE>Reading Hidden Controls</TITLE>
</HEAD>
<BODY>
<H1>Reading Hidden Controls</H1>
<%
String text = "";
if(request.getParameter("TEXT1") != null) {
out.println("The hidden text is:" + request.getParameter("TEXT1"));
text = request.getParameter("TEXT1");
}
%>
<FORM ACTION="basic.jsp" METHOD="POST">
<INPUT TYPE="TEXT" NAME="TEXT1">
<INPUT TYPE="HIDDEN" NAME="HIDDEN"
VALUE="<%= text%>">
<INPUT TYPE="SUBMIT" VALUE="Set Hidden Text">
</FORM>
</BODY>
</HTML>
Use For Each to Loop Through all Values Passed in by Form
index.jsp
<html>
<head>
<title>Page Data Example</title>
</head>
<body>
<table border="1">
<form method="POST" action="params2.jsp">
<tr>
<td width="33%">
<b>First Name</b>
</td>
<td width="73%">
<input type="text" name="first" size="40" />
</td>
</tr>
<tr>
<td width="33%">
<b>Last Name</b>
</td>
<td width="73%">
<input type="text" name="last" size="40" />
</td>
</tr>
<tr>
<td width="33%">
<b>Address</b>
</td>
<td width="73%">
<input type="text" name="address" size="40" />
</td>
</tr>
<tr>
<td width="33%">
<b>City</b>
</td>
<td width="73%">
<input type="text" name="city" size="20" />
</td>
</tr>
<tr>
<td width="33%">
<b>State</b>
</td>
<td width="73%">
<input type="text" name="state" size="20" />
</td>
</tr>
<tr>
<td width="33%">
<b>ZIP</b>
</td>
<td width="73%">
<input type="text" name="zip" size="20" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" name="action" />
<input type="reset" value="Reset" name="action" />
</td>
</tr>
</form>
</table>
</body>
</html>
Use Loop to Read Form Controls
index.jsp
<HTML>
<HEAD>
<TITLE>Submitting Check Boxes</TITLE>
</HEAD>
<BODY>
<H1>Submitting Check Boxes</H1>
<FORM action="basic.jsp" method="post">
<INPUT TYPE="CHECKBOX" NAME="checks" VALUE="check1" CHECKED>
Checkbox 1
<BR>
<INPUT TYPE="checkbox" NAME="checks" VALUE="check2">
Checkbox 2
<BR>
<INPUT TYPE="checkbox" NAME="checks" VALUE="check3">
Checkbox 3
<BR>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>