Handling The Client Handling The Client Request: Form Data Q
Handling The Client Handling The Client Request: Form Data Q
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets, JSP, JSF 2, Ajax (with jQuery, Dojo, Prototype, Ext-JS, etc.), GWT, Java 6, SOAP-based and RESTful Web Services, Spring, Hibernate/JPA, Android, Hibernate/JPA Android and customized combinations of topics topics.
For live Java EE training, please see training courses at http://courses.coreservlets.com/. http://courses coreservlets com/
Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. organization. Contact [email protected] for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
The role of form data Creating and submitting HTML forms g q p Reading individual request parameters Reading the entire set of request parameters Handling missing and malformed data Dealing with incomplete form submissions Filtering special characters out of the request parameters
Form Basics
Customized Java EE Training: http://courses.coreservlets.com/
5
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
<HTML> <HEAD><TITLE>A Sample Form Using GET</TITLE></HEAD> <BODY BGCOLOR="#FDF5E6"> <H2 ALIGN="CENTER">A Sample Form Using GET</H2>
<FORM ACTION="http://localhost:8088/SomeProgram"> <CENTER> First name: <INPUT TYPE="TEXT" NAME="firstName" VALUE="J. Random"><BR> Last name: <INPUT TYPE="TEXT" NAME="lastName" VALUE="Hacker"><P> <INPUT TYPE="SUBMIT"> <!-- Press this to submit form --> </CENTER> </FORM> </BODY></HTML>
You normally use a relative URL for the ACTION. This URL is just for testing because I am running a test server on port 8088 that echoes the data it receives.
Example
Eclipse project name: forms Files
WebContent/test1.html WebContent/someDir/test2.html
URLs
8
http://localhost/forms/test1.html http://localhost/forms/someDir/test2.html
10
12
13
Ad Advantages of GET t f
Can bookmark results page Browsers can cache results Easier to test interactively
14
XHTML
End tags always required
If no body content, can use collapsed form like <br/>
HTML 5 DOCTYPE
Most people who use the HTML 5 DOCTYPE do so as a co ve e ce, a d o ow convenience, and follow XHTML syntax in their pages. sy a e
Examples
HTML 4
<INPUT TYPE= TEXT NAME="Blah"><BR> TYPE="TEXT" NAME= Blah ><BR>
XHTML
<input type="text" name="Blah"/><br/>
15
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
request.getParameterValues("name")
18
19
20
21
22
23
24
25
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Malformed
27
28
Have the same servlet present the form, process the data, and present the results. Have one servlet present the form; have a second servlet process the data and present the results. Have a JSP page manually present the form; have a servlet or JSP page process the d t and present the results. th data d t th lt Have a JSP page present the form, automatically filling in the fields with values obtained from a data object. Have a servlet or JSP page process the data and present the results.
29
30
31
33
34
35
36
37
38
39
40
Advanced Topics
Customized Java EE Training: http://courses.coreservlets.com/
41
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
42
CSS
43
legend { font-weight: bold; color: black; background-color: white; border: 1px solid #cccccc; padding: 4px 2px; }
44
Servlet/JSP APIs have no builtin way to parse files Popular third-party library available from the Apache/Jakarta Commons library
http://jakarta.apache.org/commons/fileupload/
45
46
Summary
Make a form: <form > </form>
Relative URL for action. Textfields need name. Should always have submit button.
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
48
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.