A simple java web application

In this post, you will learn:

  • How get & post requests are submitted for a servlet?
  • How servlet reads request data during processing?
  • How servlet provides processing result to the server?

When you learned programming chances are that the first meaningful program you wrote was to add two numbers. To continue the convention, we will be doing the same in our first java web application. Usually requests are submitted to web applications from a browser which supports HTML interface hence to receive two numbers from the user and to send them to a servlet on the server we need a HTML page.

 

This is our HTML page named index.html to act as home page of our application:

<html>
<head>
<title>First java web application</title>
</head>
<body>
<form method="post" action="adderServlet">
First No. <input type="text" name="num1"><br/>
Second No. <input type="text" name="num2"><br/>
<input type="submit" value="add">
</form>
</body>
</html>

In this HTML page, an input form is created to submit a post request for adderServlet on the server from which this HTML page is received. Value of the method attribute of the form tag, tells the browser which request type to use for submitting the form. Default request type is get i.e when a form doesn’t have method attribute, it is submitted through a get request. Value of the action attribute represents the URI of the requested component, it is added to the site address to form the complete URL.

In order to process this request a servlet class is required. From previous posts you must have learned that for processing HTTP requests in a servlet, a sub class of HttpServlet is to be defined. Here is the servlet class for our first java web application:

package com.techmentro.learningpad;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class AdderServlet extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		//Request data is read
		int a=Integer.parseInt(request.getParameter("num1"));
		int b=Integer.parseInt(request.getParameter("num2"));
		//Processing is done
		int c=a+b;
		//Content type of the response is set
		response.setContentType("text/html");
		//Output stream is obtained for writing the processing result to the response
		PrintWriter out=response.getWriter();
		//Result is written to the output stream
		out.println("Sum is: "+c);
		//Output stream is closed.
		
	}
}

We are submitting post request from the HTML page hence doPost() method is overridden in the class. This simple servlet represents the work flow of a request processing which can be described as follows:

  1. Data which are submitted by the user as part of request are read.
  2. Processing is done.
  3. Content type of the response is set so that browser can find out how to render the received contents.
  4. An output stream is obtained from the response object.
  5. Processing result to be sent to the end user is written to the output stream.
  6. Output stream is closed.

Description of the methods of ServletRequest & ServletResponse interfaces which are used in the doPost() method.

1. getParameter() method is provided by ServletRequest interface to read request parameters i.e. data which are submitted by the user as part of request.

It has following syntax:

public String getParameter(String paramName);

2. getWriter() method is provided by ServletResponse interface to obtain a PrintWriter object. PrintWriter is a character oriented output stream which provides print() and println() methods.

It has following syntax:

public PrintWriter getWriter() throws IOException;

3. setContentType() method is provided by ServletResponse interface to specify the type and format of response.

It has following syntax:

public void setContentType(String MIMEType);

To specity content type of response, standard MIME types are used. A MIME type has following format: MajorType/MinorType. Major Type specify the type of content and Minor type specify the format of contents. e.g.

[one_third] Common MIME Types of text:

  • text/html
  • text/plain
  • text/xml
  • text/json
[/one_third] [one_third] Common MIME Types of images:

  • image/bmp
  • image/jpeg
  • image/gif
  • image/png
[/one_third] [one_third_last] Common MIME Types of applications:

  • application/msword
  • application/pdf
  • application/excel
  • application/powerpoint
[/one_third_last]

We have created HTML page for submitting request and Servlet class for processing it. Now we need to link the url used in the HTML page to the servlet class using the web.xml file. Here is the web.xml file of of our java web application. It tells the server that an object of AdderServlet class is to be created by s1 name and this object will process the request having uri /adderServlet.

  
  <web-app>
  <servlet>
  <servlet-name>s1</servlet-name>
  <servlet-class>com.techmentro.learningpad.AdderServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>s1</servlet-name>
  <url-pattern>/adderServlet</url-pattern>
  </servlet-mapping>
</web-app>

If you have an IDE such as Eclipse or Netbeans you can deploy and run the application using these components as the IDE provides pre configured web server, takes care of the directory structure, compilation and deployment. If you want to deploy it manually then stay tuned, we will discuss the deployment modes and packaging formats in the next post.


Its time to recap the main concepts:

  1. A web application provides HTML based interface to the user.
  2. Method attribute of HTML form element is used to control request type.
  3. A servlet need to tell the browser what type of contents are sent in which format by it as response.
  4. Standard MIME types are used to specify the type and format of contents.
  5. The servlet uses an output stream to write its processing result into the response object.

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

3 thoughts on “A simple java web application

  1. Sir, Tomcat server error is showing when running app on server :

    error : “Sever ports 8005, 8080, 8009 required by tomcat 7.0 at local host are already in use.”

Leave a Reply

Your email address will not be published. Required fields are marked *