×
DocsConsole

DocsConsole

ServletForm Example

By | | Updated : 2019-04-13 | Viewed : 262 times

ServletForm Example

    Perquisitions

  • JdK 11

  • Servlet 4.0

  • Apache-Tomcat 9.0.16

  • Eclipse 4.X

  • Apache Maven 3.X

Please create Maven Project as given below.

Create Maven project for ServletFormApp-Example
Click on File tab 
--> New 
--> Click on Maven Project 
--> Please check on Create Simple Project (Skip architype selection) 
--> Click on Next --> Enter the values com.docsconsole.tutorials.servlet4 as Group Id, ServletFormApp-Example as Artifact Id 
--> Click on Finish
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.docsconsole.servlet4.tutorials</groupId>
	<artifactId>ServletFormApp-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>ServletFormApp-Example</name>
	<description>ServletFormApp-Example</description>

	<properties>
		<maven.war.plugin.version>3.2.2</maven.war.plugin.version>
		<servlets.version>4.0.0</servlets.version>
		<jsp.version>2.3.1</jsp.version>
		<jstl.version>1.2.2</jstl.version>
		<tld.version>1.1.2</tld.version>
		<log4j.version>1.2.17</log4j.version>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlets.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- JSP Dependency -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- JSTL Dependency -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>${jstl.version}</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${tld.version}</version>
		</dependency>
		<!-- Log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>

				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>${maven.war.plugin.version}</version>
					<configuration>
						<warSourceDirectory>src/main/webapp</warSourceDirectory>
						<warName>${project.artifactId}</warName>
						<failOnMissingWebXml>false</failOnMissingWebXml>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

Please create java and XML files with below given files.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>ServletFormApp-Example</display-name>
	<servlet>
		<servlet-name>FormServlet</servlet-name>
		<servlet-class>com.docsconsole.servlet4.tutorials.FormServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>FormServlet</servlet-name>
		<url-pattern>/readForm</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<servlet-name>FormServlet</servlet-name>
		<url-pattern>/readForm</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>home.jsp</welcome-file>
	</welcome-file-list>
</web-app>
FormServlet.java
package com.docsconsole.servlet4.tutorials;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public FormServlet() {
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("Hello World");		
		System.out.println("firstname"+request.getParameter("firstname"));
		System.out.println("lastname"+request.getParameter("lastname"));
		System.out.println("phoneNumber"+request.getParameter("phoneNumber"));
		System.out.println("gender"+request.getParameter("gender"));
		System.out.println("vehicle1"+request.getParameter("vehicle1"));
		System.out.println("vehicle2"+request.getParameter("vehicle2"));
		System.out.println("The Selected Date"+request.getParameter("bday"));
		System.out.println("carsType"+request.getParameter("carsType"));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
}
home.jsp
<!DOCTYPE html>
<html>
   <head></head>
   <body>
      <div style="padding-left: 50px; padding-top: 50px;">
         <h2>Servlet Form App Example</h2>
         <form action="readForm" method="GET">
            <span style="color: #93f3ee;font-size:18px;padding-right: 45px;">First name:</span>
            <input type="text" name="firstname" style="width: 145px;">
            <br><br>
            <span style="color: #93f3ee;font-size:18px;padding-right: 47px;">Last name:</span>
            <input type="text" name="lastname" style="width: 145px;">
            <br><br>
            <span style="color: #93f3ee;font-size:18px;padding-right: 13px;">Phone Number:</span>
            <input type="text" name="phoneNumber" style="width: 145px;">
            <br><br>
            <span style="color: #93f3ee;font-size:18px;">Please select the Gender:</span><br><br>
            <input type="radio" name="gender" value="male"> Male<br>
            <input type="radio" name="gender" value="female"> Female<br>
            <input type="radio" name="gender" value="other"> Other<br><br>
            <span style="color: #93f3ee;font-size:18px;">Please select vehicle Type:</span><br><br>
            <input type="checkbox" name="vehicle1" value="Bike">Bike<br>
            <input type="checkbox" name="vehicle2" value="Car">Car<br><br>
            <span style="color: #93f3ee;font-size:18px;">Please select DOB:</span><br><br>
            <input type="date" name="bday" min="2000-01-02"><br><br>
            <select name="carsType" style="width: 145px;">
               <option value="volvo">Volvo</option>
               <option value="saab">Saab</option>
               <option value="fiat">Fiat</option>
               <option value="audi">Audi</option>
            </select>
            <br><br>
            <input type="submit">
         </form>
      </div>
   </body>
</html>
confirmation.jsp
<!DOCTYPE html>
<html>
   <head></head>
   <body>
      <div style="padding-left: 50px; padding-top: 50px;">
         <h2>The From has been submitted. Please see the server log values...</h2>
      </div>
   </body>
</html>

The final Project directory of current application is as given below.

ServletFormApp-Example-Directory

Please deploy the application and hit the URL (http://localhost:8080/ServletFormApp-Example) in the browser then you can see page as given below.

ServletFormApp-Example-landing-page

Once you fill the values and click on the submit button you will results as given below.

ervletFormApp-Example-result
ServletFormApp-Example-result1

Download Source Code ServletFormApp-Example.zip

Leave A Reply