Java Tutorial/JSP/Form Post

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

Form Calculator

index.jsp



<HTML>
    <HEAD>
    </HEAD>
    <BODY>
        <FORM ACTION="basic.jsp" METHOD="POST">
            <CENTER>
                <H1>The JSP Calculator</H1>
                <INPUT TYPE="TEXT" NAME="op1" VALUE="">
                <BR>
                +
                <BR>
                <INPUT TYPE="TEXT" NAME="op2" VALUE="">
                <BR>
                <INPUT TYPE="SUBMIT" VALUE="&nbsp;&nbsp;=&nbsp;&nbsp;">
            </CENTER>
        </FORM>
    </BODY>
</HTML>





Get Form Parameter By Index

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Property Access</title>
  </head>
  <body>
    <c:if test="${pageContext.request.method=="POST"}">
    <c:set var="idx" value="name" />
    param.name = 
    <c:out value="${param.name}" />
    <br />
    param[name] = 
    <c:out value="${param[idx]}" />
    <br />
    </c:if>
    <br />
    <form method="post">Please enter your name? 
    <input type="text" name="name" />
    <input type="Submit" />
    <br />
    </form>
  </body>
</html>





Get Form Parameter Enumeration

index.jsp



<HTML>
    <HEAD>
        <TITLE>Getting Parameter Names</TITLE>
    </HEAD>
 
    <BODY>
        <H1>Getting Parameter Names<H1>
        <FORM ACTION="basic.jsp" METHOD="POST">
            <INPUT TYPE="TEXT" NAME="text1">
            <BR>
            <SELECT NAME="select1" SIZE="5" MULTIPLE>
                <OPTION>Option 1</OPTION>
                <OPTION selected>Option 2</OPTION>
                <OPTION>Option 3</OPTION>
                <OPTION>Option 4</OPTION>
                <OPTION>Option 5</OPTION>
            </SELECT>
            <BR>
            <INPUT TYPE="SUBMIT" VALUE="Submit">
        </FORM>
    </BODY>
</HTML>





Get submit button value

<HTML>
    <HEAD>
        <TITLE>Get submit button value</TITLE>
    </HEAD>
    <BODY>
        <H1>Using Multiple Forms</H1>
        <% 
            if(request.getParameter("submit") != null) {
        %>
            You clicked 
            <%= request.getParameter("submit") %>
        <%
            }
        %>
        <FORM NAME="form1" METHOD="POST">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 1">
        </FORM>
        <FORM NAME="form2" METHOD="POST">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 2">
        </FORM>
        <FORM NAME="form3" METHOD="POST">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 3">
        </FORM>
    </BODY>
</HTML>





Process the Form action in the save page

<HTML>
    <HEAD>
        <TITLE>Using Buttons</TITLE>
    </HEAD>
    <BODY>
        <% 
            //if(request.getParameter("buttonName") != null) {
            if(request.getParameterNames() != null) {
        %>
            You clicked 
            <%= request.getParameter("buttonName") %>
        <%
            }
        %>
        <FORM NAME="form1" METHOD="POST">
            <INPUT TYPE="HIDDEN" NAME="buttonName">
            <INPUT TYPE="BUTTON" VALUE="Button 1" ONCLICK="button1()">
            <INPUT TYPE="BUTTON" VALUE="Button 2" ONCLICK="button2()">
            <INPUT TYPE="BUTTON" VALUE="Button 3" ONCLICK="button3()">
        </FORM>
        <SCRIPT LANGUAGE="JavaScript">
            <!--
            function button1()
            {
                document.form1.buttonName.value = "button 1"
                form1.submit()
            }    
            function button2()
            {
                document.form1.buttonName.value = "button 2"
                form1.submit()
            }    
            function button3()
            {
                document.form1.buttonName.value = "button 3"
                form1.submit()
            }    
            // --> 
        </SCRIPT>
    </BODY>
</HTML>





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