Java/JSP/Form
Содержание
JSP form and Java beans
//info.html
<html>
<head>
<title>Request More Information</title>
</head>
<body>
<h1>More Information</h1>
Please use this basic form to select the course
that you would like further information on.
<form method="get" action="moreInformationRequestWithBean.jsp">
<br><input type="radio" name="courses" value="Java Programming"> Java Programming
<br><input type="radio" name="courses" value="Java Web Development"> Java Web Development
<br><input type="radio" name="courses" value="J2EE Development"> J2EE Development
<br><input type="radio" name="courses" value="XML Introduction"> XML Introduction
<br><input type="radio" name="courses" value="XML Schema"> XML Schema
<br><input type="radio" name="courses" value="Web Services"> Web Services
<p>First name: <input type="text" name="firstName">
<br>Last name: <input type="text" name="lastName">
<br>Email: <input type="text" name="email">
<p><input type="submit" name="Submit">
</form>
</body>
</html>
<!-- moreInformationRequestWithBean.jsp -->
<%@ page import="com.jexp.*"%>
<jsp:useBean id="infoRequest" scope="session"
type="MoreInfoRequest"/>
<jsp:setProperty name="infoRequest" property="*"/>
<html>
<head>
<title>Thankyou for your request</title>
</head>
<body>
<h1>Thankyou for your request</h1>
Thankyou for your request for more information.
It will be sent to you shortly.
<p>Click to view your request.
</body>
</html>
// listing 8.4
package com.jexp;
public class MoreInfoRequest
{
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCourses() {
return courses;
}
public void setCourses(String courses) {
this.courses = courses;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
private String firstName;
private String lastName;
private String email;
private String courses;
}
JSP form parameters with default values
<html>
<head><title>Create Person</title></head>
<body>
<h1>Enter your details</h1>
<form action="displayDetails.jsp" method="post">
<table>
<tr><td>First name:</td> <td><input type="text" name="firstName" /></td></tr>
<tr><td>Last name:</td> <td><input type="text" name="lastName" /></td></tr>
<tr><td>Age:</td> <td><input type="text" name="age" /></td></tr>
</table>
<input type="submit" value="Submit details" />
</form>
</body>
</html>
//displayDetails.jsp
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<html>
<head><title>Display details</title></head>
<body>
<h1>The details that you entered!</h1>
<table>
<tr><td>First name</td>
<td><c:out value="${param.firstName}" /></td>
</tr>
<tr><td>Last name</td>
<td><c:out value="${param.lastName}" /></td>
</tr>
<tr><td>Age</td>
<td><c:out value="${param.age}" /></td>
</tr>
<%-- The following two values are not passed from the HTML form
and are present to show the syntax for specifying default
values --%>
<tr><td>Partner"s name</td>
<td><c:out value="${param.partnerName}" default="Unknown name" /></td>
</tr>
<tr><td>Partner"s age</td>
<td><c:out value="${param.partnerAge}">
Unknown age
</c:out>
</td>
</tr>
</table>
</body>
</html>
JSP: passing parameter and form
/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
<html>
<head><title>Select Your Portal</title></head>
<body>
<h1>Select your preferred portal:</h1>
<form action="showportal.jsp" method="get">
<select name="portchoice">
<option>news</option>
<option>weather</option>
<option>entertainment</option>
</select>
<input type="submit" value="Select"/>
</form>
</body>
</html>
//showportal.jsp
<%@ taglib prefix="c" uri="http://java.sun.ru/jsp/jstl/core" %>
<html>
<c:choose>
<c:when test="${param.portchoice == "news"}">
<head><title>News Portal</title></head>
<body>
<h1>Welcome to the News Portal!</h1>
</body>
</c:when>
<c:when test="${param.portchoice == "weather"}">
<head><title>Weather Portal</title></head>
<body>
<h1>You Get the Latest Weather!</h1>
</body>
</c:when>
<c:when test="${param.portchoice == "entertainment"}">
<head><title>Entertainment Portal</title></head>
<body>
<h1>Entertainment News Just for You!</h1>
</body>
</c:when>
<c:otherwise>
<head><title>System Portal</title></head>
<body>
<h1>Application logic problem detected!</h1>
</body>
</c:otherwise>
</c:choose>
</html>
JSP Standard Actions: retrieve information and form
/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
Name Guess Jsp
Retrieve Form Data Save To Bean And Output Jsp
Save Form Value To File Jsp
Using Multiple Forms
<HTML>
<HEAD>
<TITLE>Using Multiple Forms</TITLE>
</HEAD>
<BODY>
<H1>Using Multiple Forms</H1>
<%
if(request.getParameter("buttonName") != null) {
%>
You clicked
<%= request.getParameter("buttonName") %>
<%
}
%>
<FORM NAME="form1" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="buttonName" VALUE="button 1">
<INPUT TYPE="SUBMIT" VALUE="Button 1">
</FORM>
<FORM NAME="form2" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="buttonName" VALUE="button 2">
<INPUT TYPE="SUBMIT" VALUE="Button 2">
</FORM>
<FORM NAME="form3" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="buttonName" VALUE="button 3">
<INPUT TYPE="SUBMIT" VALUE="Button 3">
</FORM>
</BODY>
</HTML>