Entry
Java: Servlet: Program: Create: Simple: How to create a Java servlet with HTML form?
Apr 22nd, 2007 09:29
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 22 April 2021 - 06:19 pm ----------------------
Java: Servlet: Program: Create: Simple: How to create a Java servlet
with HTML form?
===
Steps: Overview:
1. Install Tomcat
(in general a web server that can handle servlets)
2. -Download and install Java JDK
(or similar Java Enterprise Edition JxEE)
3. -Create your servlet source code using your favorite texteditor
(inside the servlet put source code to generate (=print) a HTML
page)
4. -You get your information using the doGet() method
and send your information using the doPost() method
--- cut here: begin --------------------------------------------------
import java.io.IOException;
//
import java.io.PrintWriter;
//
import java.util.Enumeration;
//
import javax.servlet.ServletException;
//
import javax.servlet.http.HttpServlet;
//
import javax.servlet.http.HttpServletRequest;
//
import javax.servlet.http.HttpServletResponse;
//
public class HtmlFormServlet extends HttpServlet {
//
public void doGet( HttpServletRequest request, HttpServletResponse
response ) throws ServletException, java.io.IOException {
//
response.setContentType( "text/html" );
//
java.io.PrintWriter out = response.getWriter();
//
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<html>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<head>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<title>" );
out.println( "HTML form servlet" );
out.println( "</title>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</head>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<body>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<h2>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "Fill in the information and click the submit
button" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</h2>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<form" );
out.println( " method=\"post\"" );
out.println( " action =\"" + request.getContextPath( )
+ "/servlet/HtmlFormServlet\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<table" );
out.println( " border=\"0\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "Field1:" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<input" );
out.println( " type=\"text\"" );
out.println( " name=\"field1\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "Field2:" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<input" );
out.println( " type=\"text\"" );
out.println( " name=\"field2\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "Field3:" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<input" );
out.println( " type=\"text\"" );
out.println( " name=\"field3\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<input" );
out.println( " type=\"submit\"" );
out.println( " value=\"Submit your information\"" );
out.println( ">" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</td>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</tr>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</table>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</form>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</body>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</html>" );
out.println( "<!----------------------------------------------------
---------------->" );
}
//
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, java.io.IOException {
//
Enumeration fieldNames = request.getParameterNames();
//
String s;
//
boolean fieldNamesFoundB = false;
//
if ( ! fieldNames.hasMoreElements() ) {
fieldNamesFoundB = true;
}
//
response.setContentType( "text/html" );
//
java.io.PrintWriter out = response.getWriter();
//
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<html>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<head>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<title>" );
out.println( "Your filled information" );
out.println( "</title>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</head>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<body>");
out.println( "<!----------------------------------------------------
---------------->" );
//
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "<h2>" );
out.println( "<!----------------------------------------------------
---------------->" );
//
if ( fieldNamesFoundB ) {
out.println( "No information sent" );
}
else {
out.println( "Here the fields filled in" );
}
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</h2>" );
out.println( "<!----------------------------------------------------
---------------->" );
//
while( fieldNames.hasMoreElements() ) {
s = ( String ) fieldNames.nextElement();
out.println( "<!---------------------------------------------------
----------------->" );
out.println( s );
out.println( "<!---------------------------------------------------
----------------->" );
out.println( ":" );
out.println( " " );
out.println( "<!---------------------------------------------------
----------------->" );
out.println( request.getParameter( s ) );
out.println( "<!---------------------------------------------------
----------------->" );
out.println( "<br/>" );
out.println( "<!---------------------------------------------------
----------------->" );
} // while
//
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</body>" );
out.println( "<!----------------------------------------------------
---------------->" );
out.println( "</html>" );
out.println( "<!----------------------------------------------------
---------------->" );
}
}
--- cut here: end ----------------------------------------------------
4. Save this as
HtmlFormServlet.java
(e.g. in the c:\temp\ directory)
5. Compile and debug it
1. -Run javac.exe located in your Java JDK bin directory with this
parameters
cd %JAVA_HOME%\bin\
javac.exe -classpath "%JAVA_HOME%;%CATALINA_HOME%\lib\servlet-
api.jar" c:\temp\HtmlFormServlet.java
2. -This will create a file
HtmlFormServlet.class
in the same directory, when you have compiled
successfully
3. -Copy this file
HtmlFormServlet.class
to your Tomcat class directory
(by default in a 'classes' subfolder of the WEB-INF folder of
the Tomcat ROOT directory)
===
E.g. (all on one line)
md "%CATALINA_HOME%\webapps\ROOT\WEB-INF\classes\"
copy c:\temp\HtmlFormServlet.class "%CATALINA_HOME%
\webapps\ROOT\WEB-INF\classes\"
4. -Backup and edit the file
web.xml
in the directory
"%CATALINA_HOME%\webapps\ROOT\WEB-INF\web.xml"
E.g. edit this file using Notepad:
notepad.exe "%CATALINA_HOME%\webapps\ROOT\WEB-INF\web.xml"
--- cut here: begin --------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version
2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>
--- cut here: end ----------------------------------------------------
5. -Add the following tag to it, giving the name of your servlet
--- cut here: begin --------------------------------------------------
<servlet>
<servlet-name>HtmlFormServlet</servlet-name>
<servlet-class>HtmlFormServlet</servlet-class>
</servlet>
--- cut here: end ----------------------------------------------------
6. -Add also the following tag to it, giving the URL to your
servlet
(this is in general /servlet/<your servlet name>)
--- cut here: begin --------------------------------------------------
<servlet-mapping>
<servlet-name>HtmlFormServlet</servlet-name>
<url-pattern>/servlet/HtmlFormServlet</url-pattern>
</servlet-mapping>
--- cut here: end ----------------------------------------------------
7. -So all together your new web.xml becomes
--- cut here: begin --------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version
2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>HtmlFormServlet</servlet-name>
<servlet-class>HtmlFormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HtmlFormServlet</servlet-name>
<url-pattern>/servlet/HtmlFormServlet</url-pattern>
</servlet-mapping>
</web-app>
--- cut here: end ----------------------------------------------------
6. -*Important*
If you save this file make sure there are no empty lines
at the top, or your Tomcat will give a lot of (XML parse)
errors
7. Run this servlet file in your browser
1. -First make sure your servlet server program (=Tomcat) is
running (e.g. by running its startup.bat batch file in the
Tomcat 'bin' directory)
-Thus typing in your browser the URL
http://localhost:8080
should show the Tomcat home page
2. -To test your HtmlFormServlet.class servlet file locally on your
computer, open your browser, and type the URL:
http://localhost:8080/servlet/HtmlFormServlet
===
4. -This URL will send a request to your servlet
using the HTTP GET protocol.
So the doGet() method will be activated which
sends back (by printing it to the standard output)
an HTML form
5. -You should see in your browser
--- cut here: begin --------------------------------------------------
Fill in the information and click the submit button
Field1: [ ]
Field2: [ ]
Field3: [ ]
[Submit your information]
--- cut here: end ----------------------------------------------------
6. -When you press the 'Submit' button, it will send away
the information using the HTTP POST method
(because that is the method that was chosen in the example
program in the 'method' parameter in the 'FORM')
to the URL which is indicated in the 'action' parameter
in the FORM.
This URL is again the same as before
http://localhost:8080/servlet/HtmlFormServlet
and thus only the method (=POST) is different.
So the doPost() method will be activated which
sends back the field information you filled in on the form
7. -You should see in your browser
--- cut here: begin --------------------------------------------------
Here the fields filled in
field1 : something filled in by you
field3 : something else filled in by you
field2 : another thing filled in by you
--- cut here: end ----------------------------------------------------
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
Tomcat v6
Java JDK v1.6
===
[book: source: author=Perry, Bruce W. - title=Java Servlet & JSP
Cookbook - publisher=O'Reilly - year=2004, January - pages total=746 -
ISBN=0-596-00572-5]
===
Internet: see also:
---
Language: Computer: Java: Servlet: Link: Overview: Can you give an
overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/40576/fid/778
----------------------------------------------------------------------