Java/JSP/Form

Материал из Java эксперт
Перейти к: навигация, поиск

JSP form and Java beans

   <source lang="java">

//info.html <html> <head> <title>Request More Information</title> </head> <body>

More Information

Please use this basic form to select the course that you would like further information on. <form method="get" action="moreInformationRequestWithBean.jsp">

 
<input type="radio" name="courses" value="Java Programming"> Java Programming
<input type="radio" name="courses" value="Java Web Development"> Java Web Development
<input type="radio" name="courses" value="J2EE Development"> J2EE Development
<input type="radio" name="courses" value="XML Introduction"> XML Introduction
<input type="radio" name="courses" value="XML Schema"> XML Schema
<input type="radio" name="courses" value="Web Services"> Web Services

First name: <input type="text" name="firstName">
Last name: <input type="text" name="lastName">
Email: <input type="text" name="email"> <p><input type="submit" name="Submit"> </form> </body> </html> <%@ 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>

Thankyou for your request

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;

}


      </source>
   
  
 
  



JSP form parameters with default values

   <source lang="java">

<html>

 <head><title>Create Person</title></head>
 <body>

Enter your details

   <form action="displayDetails.jsp" method="post">
First name: <input type="text" name="firstName" />
Last name: <input type="text" name="lastName" />
Age: <input type="text" name="age" />
     <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>

The details that you entered!

<%-- The following two values are not passed from the HTML form and are present to show the syntax for specifying default values --%>
First name <c:out value="${param.firstName}" />
Last name <c:out value="${param.lastName}" />
Age <c:out value="${param.age}" />
Partner"s name <c:out value="${param.partnerName}" default="Unknown name" />
Partner"s age <c:out value="${param.partnerAge}">
               Unknown age
             </c:out>
 </body>

</html>


      </source>
   
  
 
  



JSP: passing parameter and form

   <source lang="java">

/* 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>

Select your preferred portal:

<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>

Welcome to the News Portal!

     </body>
   </c:when>
   <c:when test="${param.portchoice == "weather"}">
      <head><title>Weather Portal</title></head>
      <body>

You Get the Latest Weather!

      </body>
   </c:when>
   <c:when test="${param.portchoice == "entertainment"}">
      <head><title>Entertainment Portal</title></head>
      <body>

Entertainment News Just for You!

      </body>
   </c:when>
   <c:otherwise>
      <head><title>System Portal</title></head>
      <body>

Application logic problem detected!

      </body>
   </c:otherwise>

</c:choose> </html>

      </source>
   
  
 
  



JSP Standard Actions: retrieve information and form

   <source lang="java">

/* Beginning JavaServer Pages Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell ISBN: 0-7645-7485-X

  • /


      </source>
   
  
 
  



Name Guess Jsp

Retrieve Form Data Save To Bean And Output Jsp

Save Form Value To File Jsp

Using Multiple Forms

   <source lang="java">

<HTML>

   <HEAD>
       <TITLE>Using Multiple Forms</TITLE>
   </HEAD>
   <BODY>

Using Multiple Forms

       <% 
           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>

      </source>