In the below post, we will examine how servlets can be used to retrieve information from the client browser.
package httpreqresp;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class ParameterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost (request, response); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Parameter Servlet</title>"); out.println("</head>"); out.println("<body>"); Enumeration parameters = request.getParameterNames(); String param = null; while (parameters.hasMoreElements()) { param = (String) parameters.nextElements(); out.println (param + " : " + request.getParameter(param) + "<BR>"); } out.println("</body><//html>"); out.close(); } }
<HTML> <HEAD> <TITLE> Parameter Servlet Form </TITLE> </HEAD> <BODY> <form action="parameter" method="POST"> <table width="400" border="0" cellspacing="0"> <tr> <td>Name : </td> <td> <input type="text" name="name" size="20" maxlength="20"> </td> <td> SSN: </td> <td> <input type="text" name="ssn" size="11" maxlength="11"> </td> <tr> <td>Age: </td> <td> <input type="text" name="age" size="3" maxlength="3"> </td> <td>email :</td> <td><input type="text" name="email" size="30" maxlength="30"></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Reset"> </td> </tr> </table> </FORM> <//BODY> </HTML>
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>ParameterServlet</servlet-name> <servlet-class>httpreqresp.ParameterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ParameterServlet</servlet-name> <url-pattern>parameter</url-pattern> </servlet-mapping> </web-app>
| Directory | Contains |
|---|---|
| /httpreqresp | The root directory of the web application. All JSP and HTML files are stored here |
| /httpreqresp/WEB-INF | This directory contains all resources related to the application that are not in the document root of the application. It is where the web application deployment descriptor web.xml is located. |
| /httpreqresp/WEB-INF/classes | This is where the servlet and other classes are located. |
| /httpreqresp/WEB-INF/lib | This directory contains Java Archive (JAR) files that the web application is dependent on |
| /httpreqresp/src | The directory having the java source code. |
Working of the Servlet to retrieve HTTP data.
Invoke
http://localhost:8080/httpreqresp/Form.html
After filling the required information press the SUBMIT button.
On SUBMIT button, the request is passed to the web container with url-pattern "parameter" (the action value for POST event), which checks in the web.xml file under web application /httpreqresp/WEB-INF folder and gets the corresponding servlet ParameterServlet, where the doGet method of the HttpServlet gets invoked which passes the request to doPost.
Here three methods can be used to retrieve request parameters from the web browser client.
public String ServletRequest.getParameter (String name);
public String[] ServletRequest.getParameterValues (String name);public EnumerationServletRequest.getParameterNames ();getParameter() method can be used when the request has only one parameter.If the request has multiple parameter values, you should use getParameterValues method. getParameterValues returns the values of the specified parameters as an array of java.lang.Strings.As the name suggests, getParameterNames returns the parameter names contained in the request as an Enumeration of strings or an empty enumeration if there are no parameters.
We now compile and deploy this code on the tomcat web server under webapps as follows. If you are using another web server like JBOSS, the deployment folder will be different, and accordingly the deployment directory will change.
We create a new folder httpreqresp under the webapps folder.
We place the .java file in the folder /httpreqresp/src folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\src\ParameterServlet.javaWe place the jsp file in the /httpreqresp folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\Form.htmlWe place the web.xml file in the /httpreqresp/WEB-INF folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\WEB-INF\web.xmlWe compile the code using ant. We download a suitable version of ant and unzip it into a suitable folder path.
For the build we need to tell the ant build cmd a few important path variables
CLASSPATH
ANT_HOME
JAVA_HOME
PATH
In CLASSPATH I need to set the path for servlet-api.jar
set CLASSPATH=E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\lib\servlet-api.jar;
Set the path for ANT_HOME
set ANT_HOME=E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1
Set the path for JAVA_HOME
set JAVA_HOME=E:\install\jdk
In the PATH variable I set the path for ANT_HOME/bin and JAVA_HOME/bin
set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\install\jdk\jdk\bin;E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
I can put all the path variables in a bat file and run the bat file on cmd prompt.
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>set_path
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>set CLASSPATH=E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\lib\servlet-api.jar;
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>set ANT_HOME=E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>set JAVA_HOME=E:\install\jdk
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>set PATH=E:\install\21cXE\dbhomeXE\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\PuTTY\;E:\install\jdk\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 6.0\bin;E:\install\jdk\bin;C:\Users\winni\AppData\Local\Microsoft\WindowsApps;E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
Once the various path variables are set, I create the ant build file and run the ant command.
A sample ant build file to compile my java code is below.
response <project name="httpreqresp" default="all" basedir=".">
<property name="struts.home" value="E:\winnith\java\struts\home"/>
<property name="miscjar.home" value="E:\winnith\java\project\strutsapp"/>
<property name="CATALINA_HOME" value="E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0"/>
<property name="tomcat.home" value="${CATALINA_HOME}"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/WEB-INF/classes"/>
<path id="compile.class.path">
<fileset dir="${tomcat.home}\lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${struts.home}">
<include name="*.jar"/>
</fileset>
<fileset dir="${miscjar.home}">
<include name="*.jar"/>
</fileset>
</path>
<target name="all" depends="clean,compile"/>
<target name="compile" depends="clean">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}"/>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
I now run the ant command as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp>ant
Buildfile: build.xml
clean:
[delete] Deleting directory E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\WEB-INF\classes
compile:
[mkdir] Created dir: E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\WEB-INF\classes
[javac] Compiling 1 source file to E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\httpreqresp\WEB-INF\classes
all:
BUILD SUCCESSFUL
Total time: 0 seconds
Now runhttp://localhost:8080/httpreqresp/Form.html
request
References
God's Word for the day
I have fought the good fight, I have kept the faith.
From now on, there is reserved for me the crown of righteousness,
which the Lord, the righteous judge, will give me on that day,
and not only to me but also to all who have longed for his appearing.
2 Timothy 4:7-8
Salt and Light
You are the light of the world.
A city built on a high hill cannot be hid.
No one after lighting a lamp puts it under the bushel basket,
But on the lampstand, and it gives light to all in the house.
In the same way, let your light shine before others,
so that they may see your good works,
And give glory to your Father in Heaven.
Mathew 5:14-16


No comments:
Post a Comment