JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Handling the Client Request: Form Data

  • The role of form data

  • Creating and submitting HTML forms

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


    The role of form data

    • form is essential part in any web page.
    • Client is interacting  the server with the form.
    • User sends the argument  values to the server with the form fields.
    • In jsp or html page we create form by the form html tag with its action and method attribute.


    Creating and Submitting HTML form


    • A Jsp file or Html file can contain one or more than one form.
    • In the action attribute we give the servlet url or jsp file name.
    • In the method we put get or post.
    • It calls the doGet() and doPost() 

         Reading Single Values

         getParameter()
  • to read a request parameter you simply call the getParameter() method of HttpServletRequest , supplying case-sensitive parameter name.;
  • An empty string is return if parameter exist but it doestn't have any value.
  • Null is return if there was no such value.

         getParameterValues()
  • if  same parameter name might appear in the form data moire than once you should call getParameterValues() instead of getParameter()
  • The return value of getParametereValues() is null for non-existent parameter names and is a non element array

         getParameterNames() and getParameterMap() 
  • The getParameterNames() returns an empty Enumeration(not null) .
  • Enumeration is an interface that merely guarantees that actual class will have hasMoreElements and nextElement.
  •   An alternative to getParameterNames is getParameterMap. This method returns a Map

      Reading input in multiple character sets:  setCharacterEncoding

  •  By default request.getParameter interprets inp1ut using the server’s current character set. To change the default use setCharacterEncoding() method of ServletRequest.

   Using default values when parameters are missing

  • The value is null
  •  The value is an empty string
  •  The value is not empty string of wrong format.

Home.html  

<h2>
<form action="./RS">
First Name<input type="text" name="t1"><br>
Last Name<input type="text" name="t2"><br>
Username<input type="text" name="t3"><br>
Password<input type="password" name="t4"><br>
Gender<br>
<input type="radio" name="t5" value="M">Male<br>
<input type="radio" name="t5" value="F">Female<br>
Qualifications<br>
<input type="checkbox" name="t6" value="UG">Under Graduate<br>
<input type="checkbox" name="t6" value="G">Graduate<br>
<input type="checkbox" name="t6" value="PG">Post Graduate<br>
Country<br>
<select name="t7">
<option>India</option>
<option>UK</option>
<option>USA</option>
</select><br>
Address<textarea name="t8"></textarea>
<input type="submit" value="Register">
<input type="reset">
</form>
</h2>
RegistrationServlet.java 

   
package myservlet;

import javax.servlet.*;
import java.io.*;

public class RegistrationServlet extends GenericServlet
{
  public void service(ServletRequest req,ServletResponse res)
        throws 
ServletException,IOException
  {
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    
    String firstname=req.getParameter("t1");
    String lastname=req.getParameter("t2");
    String username=req.getParameter("t3");
    String password=req.getParameter("t4");
    String gender=req.getParameter("t5");
    String[] qualifications=req.getParameterValues("t6");
    String country=req.getParameter("t7");
    String address=req.getParameter("t8");

    out.println("<h2>");
    out.println("First Name : "+firstname);
    out.println("<br>Last Name : "+lastname);
    out.println("<br>Username : "+username);
    out.println("<br>Password : "+password);
    out.println("<br>Gender : "+gender);
    for(int i=0;i<qualifications.length;i++)
    {
      out.println("<br>Qualification : "+qualifications[i]);
    }
    out.println("<br>Country : "+country);
    out.println("<br>Address : "+address);
    out.println("</h2>");
  }
}  
web.xml
    
<web-app>
  <servlet>
    <servlet-name>RServlet</servlet-name>
    <servlet-class>myservlet.RegistrationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RServlet</servlet-name>
    <url-pattern>/RS</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>HomePage.html</welcome-file>
  </welcome-file-list>  
</web-app>
 
Output

  • After deployment the web application on the tomcat server
  • Open the browser and put the url http://localhost:8080/wap.
  • It opens the following form
  • Pouplate the data in the form 
  • Click on the Register button.


  • After clicking on the register butoon all data from the form
    will be sent to the servlet on the server.
  • These data  are sent to the server with the HttpServletRequest object.
  • Data is  processed on   server.
  • Then processed data is  sent to  the client with the HttpServletResponse object.