Handling FORM Data using Servlets HTML Forms An
- Slides: 11
Handling FORM Data using Servlets
HTML Forms An interface controls to collect data from the user and transmit it to server.
Element in Forms • TEXT CONTROLS: <INPUT TYPE="TEXT" NAME="NAME" VALUE="INIT"> • PASSWORD FIELDS: <INPUT TYPE="PASSWORD" NAME="PASSWORD"> • TEXT AREAS: <TEXTAREA NAME="RESUME" ROWS=5 COLS=30>INPUT YOUR RESUME HERE </TEXTAREA> • Checkbox <input type="checkbox" name="checkbox" checked> <input type="checkbox" name="checkbox"> • Radio Button <input type="radio" name="radio" checked> <input type="radio" name="radio">
Cont. • List <select name="list"> <option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select> • Multilist <select name="multilist" size="3" multiple> <option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select>
Cont. • Submit Button <input type="submit" name="submit" value="Submit"> • Reset Button <input type="reset" name="reset" value="Reset Fields"> • Image Button <input type="image" name="image" src="go. gif"> • File <input type="file" name="file">
Http. Servlet. Request - Methods Enumeration get. Parameter. Names() an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters java. lang. String[] get. Parameter. Values (java. lang. String name) Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. java. lang. String get. Parameter (java. lang. String name) Returns the value of a request parameter as a String, or null if the parameter does not exist.
Example <html> <body> <form action="Check. Box" method="POST" target="_blank"> <input type="checkbox" name="maths" checked="checked" /> Maths <input type="checkbox" name="physics" /> Physics <input type="checkbox" name="chemistry" checked="checked" /> Chemistry <input type="submit" value="Select Subject" /> </form> </body> </html>
Servlet CODE –Using get. Parameter import java. io. *; import javax. servlet. http. *; public class Check. Box extends Http. Servlet { public void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { Print. Writer out = response. get. Writer(); out. println("<html>n" + "<head><title>" + title + "</title></head>n" + "<body >n" + "<ul>n" + " <li><b>Maths Flag : </b>: " + request. get. Parameter("maths") + "n" + " <li><b>Physics Flag: </b>: " + request. get. Parameter("physics") + "n" + " <li><b>Chemistry Flag: </b>: " + request. get. Parameter("chemistry") + "n" + "</ul>n" + "</body></html>"); }
Servlet Code -Using get. Parameter. Values import java. io. *; import javax. servlet. http. *; public class Check. Box extends Http. Servlet { public void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { Print. Writer out = response. get. Writer(); Enumeration param. Names = request. get. Parameter. Names(); while(param. Names. has. More. Elements()) { String param. Name = (String)param. Names. next. Element(); out. print("<tr><td>" + param. Name + "</td>n<td>"); String[] param. Values = request. get. Parameter. Values(param. Name); if (param. Values. length == 1) { String param. Value = param. Values[0]; if (param. Value. length() == 0) out. println("<i>No Value</i>"); else out. println(param. Value); } else { out. println("<ul>"); for(int i=0; i < param. Values. length; i++) { out. println("<li>" + param. Values[i]); } out. println("</ul>"); } } out. println("</tr>n</table>n</body></html>"); }
Using getparameternames http: //way 2 java. com/servlets/servlet-getparameternamesexample/
DEMO
- Core servlets
- Servlets notes
- Canvas доска
- Doctype html html head
- Doctype html html head
- Doctype html html head
- Doctype html html head
- Html forms attributes
- Moving and handling competency assessment template
- Why are related forms more agreeable than unrelated forms
- Was not short form
- Why are related forms more agreeable than unrelated forms