HTML TO SERVLET COMMUNICATION:

WelcomeServlet.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {

@Override

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

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

/*

* Get the value of form parameter

*/

String name = request.getParameter("name");

String welcomeMessage = "Welcome "+name;

/*

* Set the content type(MIME Type) of the response.

*/

response.setContentType("text/html");



PrintWriter out = response.getWriter();

/*

* Write the HTML to the response

*/

out.println("<html>");

out.println("<head>");

out.println("<title> A very simple servlet example</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>"+welcomeMessage+"</h1>");

//out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>");

out.println("</body>");

out.println("</html>");

out.close();

}

public void destroy() {

}

}

CALLER.html

<html>

<head>

<title>The servlet example </title>

</head>

<body>

<h1>A simple web application</h1>

<form method="POST" action="WelcomeServlet">

<label for="name">Enter your name </label>

<input type="text" id="name" name="name"/><br><br>

<input type="submit" value="Submit Form"/>

<input type="reset" value="Reset Form"/>

</form>

</body>

</html>



Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<servlet>

<servlet-name>WelcomeServlet</servlet-name>

<servlet-class>WelcomeServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WelcomeServlet</servlet-name>

<url-pattern>/WelcomeServlet</url-pattern>

</servlet-mapping>

</web-app>