Java/JSP/Form TextField

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

Form TextField Demo

//File: index.html
<html>
  <body>
    <h1>Hello Web Application</h1>
    <form action="hello.jsp" method="POST" >
      <table width="75%">
        <tr> 
          <td width="48%">What is your name?</td>
          <td width="52%">
            <input type="text" name="name" />
          </td>
        </tr>
      </table>
      <p> 
        <input type="submit" name="Submit" value="Submit name" />
        <input type="reset" name="Reset" value="Reset form" />
      </p>
    </form>
  </body>
</html>
//File: hello.jsp
<html>
  <head>
    <title>Hello Web Application</title>
  </head>
  <body>
    <h1>Hello Web Application</h1>
    <br/><br/>
    <%
    String name   = request.getParameter("name");
    if (name.trim().length() == 0) {
    %>
      You did not tell me your name!<br><br><br>
    <%
    } else {
    %>
      Hello <%=name%><br><br><br> 
    <%
    }
    %>
    



== Inserting Text into Text Fields ==






   
  <!-- start source code -->
   
    <source lang="java">
//File: index.html
<HTML>
  <HEAD>
    <TITLE>Inserting Text into Text Fields</TITLE>
  </HEAD>
    <BODY>
        <H1>Inserting Text into Text Fields</H1>
        <FORM NAME="form1" ACTION="formAction.jsp" METHOD="POST">
            <INPUT TYPE="TEXT">
            <INPUT TYPE="SUBMIT" VALUE="Click Me!">
        </FORM>
    </BODY>
</HTML>

//File: formAction.jsp
<HTML>
  <HEAD>
    <TITLE>Inserting Text into Text Fields</TITLE>
  </HEAD>
    <BODY>
        <H1>Inserting Text into Text Fields</H1>
        <FORM NAME="form1">
            <INPUT TYPE="TEXT" VALUE="Hello from JSP!">
            <INPUT TYPE="SUBMIT" VALUE="Click Me!">
        </FORM>
    </BODY>
</HTML>





Submitting Text Fields

//File: index.html
<HTML>
    <HEAD>
        <TITLE>Submitting Text Fields</TITLE>
    </HEAD>
 
    <BODY>
        <H1>Submitting Text Fields</H1>
        <FORM ACTION="formAction.jsp" METHOD="POST">
            Please enter your name:
            <INPUT TYPE="TEXT" NAME="text1">
            <BR>
            <INPUT TYPE="SUBMIT" value="Submit">
        </FORM>
    </BODY>
<HTML>

/////////////////////////////////////////////////////////////////////////
//File: formAction.jsp
<HTML>
  <HEAD>
    <TITLE>Reading Data From Text Fields</TITLE>
  </HEAD>
    <BODY>
        <H1>Reading Data From Text Fields</H1>
        Your name is 
        <% out.println(request.getParameter("text1")); %>
   </BODY>
</HTML>





Submitting Text Fields 2

//File: index.html
<HTML>
    <HEAD>
        <TITLE>Submitting Text Fields</TITLE>
    </HEAD>
 
    <BODY>
        <H1>Submitting Text Fields</H1>
        <FORM ACTION="formAction.jsp" METHOD="POST">
            Please enter your name:
            <INPUT TYPE="TEXT" NAME="text1">
            <BR>
            <INPUT TYPE="SUBMIT" value="Submit">
        </FORM>
    </BODY>
<HTML>

//File:formAction.jsp
<HTML>
    <HEAD>
        <TITLE>Decoding an HTTP Request</TITLE>
    </HEAD>
 
    <BODY>
        <H1> Request Information </H1>
        JSP request method: <%= request.getMethod() %>
        <BR>
        URL for the request: <%= request.getRequestURI() %>
        <BR>
        Protocol of the request: <%= request.getProtocol() %>
        <BR>
        Server name: <%= request.getServerName() %>
        <BR>
        Path: <%= request.getServletPath() %>
        <BR>
        Server port: <%= request.getServerPort() %>
        <BR>
        Remote address: <%= request.getRemoteAddr() %>
        <BR>
        Remote host: <%= request.getRemoteHost() %>
        <BR>
        Locale: <%= request.getLocale() %>
        <BR>
        User agent: <%= request.getHeader("User-Agent") %>
    </BODY>
</HTML>