Saturday, 17 November 2012

J2ee Tutorial part 2 :- Retrieving form data

//J2ee Tutorial part 2 :- Retrieving form data {The data entered by user will be taken and retrieved in the server side and again shown to the user using servlet}

Step 1 ;- create a dynamic web project as given in my Tutorial part 1
Step 2 ;- create a html page with name Weather.html under webcontent as given bellow
//Weather.html
<html>
    <head>
        <title>Weather Details</title>
    </head>
    <body>
        <form action="Weather" method="post">//form is of type post and our servlet is Weather
       
          <table>
            <tr>
              <th colspan="2">Observer Information</th>
            </tr>
            <tr>
              <td>Observer Id:</td>
              <td><input type="text" name="user" size="10" maxlength="5" /></td>
            </tr>
            <tr>
              <td>Observer Pasword:</td>
              <td><input type="password" name="password" size="10" maxlength="5" /></td>
            </tr>
            <tr>
              <td>Hidden Info:</td>
              <td><input type="hidden" name="hiddenInfo" value="Discrete Information" /></td>
            </tr>
          </table>
          <h2>Weather Report</h2>
          <p>Check all the types of weather you have observed today
            <br /><input type="checkbox" name="weathertype" value="rain" />Rain
            <br /><input type="checkbox" name="weathertype" value="showers" />Showers
            <br /><input type="checkbox" name="weathertype" value="snow" />Snow
            <br /><input type="checkbox" name="weathertype" value="sun" />Sun
            <br /><input type="checkbox" name="weathertype" value="fog" />Fog
          </p>
          <p>Choose today's highest recorded temperature
            <br /><input type="radio" name="temp" value="vlow" />Below zero degrees C
            <br /><input type="radio" name="temp" value="low" />0 to 10 degrees C
            <br /><input type="radio" name="temp" value="med" checked />11 to 20 degrees C
            <br /><input type="radio" name="temp" value="high"/>Above 20 degrees C
          </p>
         
          <h2>Weather Station</h2>
          <p>
            <select name="stations">
  <option value="ONE">Weather Station 1</option>
  <option value="TWO" selected>Weather Station 2</option>
  <option value="THREE">Weather Station 3</option>
</select>

          </p>


          <h2>Comments on the weather</h2>
          <textarea name="comments" rows="5" cols="35">Use this area to type additional commments about the weather</textarea>
          <p>
            <input type="submit" />//after filling data user will click this button
          </p>
         

        </form>
       
    </body>
</html>
Step3 :- Creating servlet under the src folder which will handle the server side logic of retrieving the user filled form data

package web.tutorial;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This servlet exists to display all the parameters passed to it.
 */
public class WeatherParams extends HttpServlet {

    protected void doPost(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

        // Set appropriate content type for response, and obtain
        // the HTML output stream.

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // Set up start of HTML page
        out.write("<HTML>\n<HEAD>\n<TITLE>Display Weather Parameters</TITLE>\n</HEAD>\n<BODY>");

        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            out.write("<h4>" + paramName + "</h4>");
            String[] paramValues = request.getParameterValues(paramName);
            for (int i =0; i < paramValues.length; i++ ) {
                out.write("<br />" + paramValues[i]);
            }
        }
       
        // Finish off the HTML page and close cleanly

        out.write("\n</BODY>\n</HTML>");
        out.close();

    }


}


Step 4 :- Url mapping for servlet in web.xml

<?xml version="1.0" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>J2ee tutorial part2</display-name>
  <welcome-file-list>
    <welcome-file>weather.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Weather</servlet-name>
    <servlet-class>web.tutorial.WeatherParams</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Weather</servlet-name>
    <url-pattern>/Weather</url-pattern>
  </servlet-mapping>
</web-app>

Step 5:- Clean build and run the project


Output:-





User fills the form  data and clicks the submit button :-



No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...