Reader Level:
ARTICLE

ServletRequest and ServletResponse Interface of Servlet in Java

Posted by Sandeep Sharma Articles | NetBeans September 13, 2013
This article explains the ServletRequest interface in Java. The NetBeans IDE is used to create various file formats using servlets.
  • 1
  • 0
  • 2339

Introduction

This article explains the ServletRequest interface in Java. The Netbeans IDE is used to create various file formats using servlets. This article explains other terms, such as public method ServletRequest interface, ServletResponse Interface, and create the application.

ServletRequest interface

This interface is required to provide client request information to the server in the form of content type, attributes, parameter names, etcetera.

Some commonly used public methods of servletRequest interface are:

  • public String getParameter(String pname);

           obtains the value of request parameters.

  • public Enumeration getParameterNmaes();

           determines the name of all request paramters.

  • public void setAttribute(String name, Object attribute);

           obtains the values of an attribute from the request scope.

  • public Object getAttribute(String name);

           obtains the value of an attribute from the request scope.

  • public Enumeration getAttributeName();

           determines the names of attributes saved in the request scope.

  • public boolean removeAttribute(String name);

           removes an attribute from an attribute scope.

  • public ServletInputStream getInputStream();

           obtains an input stream to read request data.

  • etcetera

Get the input stream method of the servlet request to read data as a sequence of byte in the case of a file upload.

ServletResponse interface

This interface is required to provide a response to the client, like content type, attributes, parameter names, etcetera.

Some commonly used public method of the ServletResponse interface are:

1. setContentType()

Specifies the contents type of the response. The contents type of the response is used by the browser to determine how the contents are to be rendored.

Syntax

public void setContentType(String MIME Type);

What is MIME

Standard MIME types are used to specify the type of contents. A MIME type has the two parts "Major-Part" and "Minor-Part".

The Major Part specifies the type and the Minor part specifies the format.

Example:

text/html          image/gif        audio/aac
text/xml           image/jpeg      audio/mp3
text/plain         image/bmp      etcetera

2. getWriter()

Returns a PrintWriter object to generate the response to the client.

Syntax

public PrintWriter getWriter();

3. getOutputStream();

Returns an output stream to generate the response to the client.

Syntax

public OutputStream getOutputStream();

Note:

Only one of these streams can be used in a Servlet to generate a response.

Let's use an example.

How to create various file formats using servlet in Java

In this example we create two different types of files according to their format, in other words one is a Word file and the second one is an Excel file.

The following procedure provides this example.

Step 1

Open the Netbeans IDE.

Fig-1.jpg

Step 2

Now go to the File Menu and choose "New Project", a new window appears like this.

Fig-2.jpg

Step 3

Choose "Java Web" -> "Web application" as shown below:

Fig-3.jpg

Step 4

Click on "Next" and then type your project name. I specified "ServletApp" and click on "Next" as shown below:

Fig-4.jpg

Step 5

Select the "server" and choose the "Java version" and then click on "Finish" as shown below.

Fig-5.jpg

Step 6

Click on "Finish"; your project is set up with a default JSP file "index.jsp" as shown below.

Fig-6.jpg

Step 7

Now delete the JSP file and create a HTML file with the name "index.html" and write the following code for it.

<!DOCTYPE html>

<html>

    <head>

        <title>Text Format Shown</title>

    </head>

    <body>

    <center><h1>Content Type Demo</h1><br/>

        <a href="wordServlet">Word Document</a><br/>

        <a href="excelServlet">Excel Sheet</a>

    </body>

</html>

Fig-7.jpg

Step 8

Now create a new servlet file by right-clicking on the project then choose "New" -> "Servlet", as shown below.

Fig-8.jpg

Step 9

Now type your servlet name as "wordServlet" as shown below.

Fig-9.jpg

Step 10

Now click on "Next" and choose the checkbox option to add your servlet file with "web.xml" automatically by NetBeans. As shown below.

Fig-10.jpg

Step 11

Now write the following code for it.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class wordServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("application/msword");

        PrintWriter out = response.getWriter();

        out.println("This document is dynamically generated by the servlet");

        out.println("It is a text of contet type");

        out.close();

    }

}

Fig-11.jpg

Step 12

Again create a new servlet file with the name "excelServlet" and write the following code for it.

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

import javax.servlet.http.*;

 

public class excelServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("application/vnd.ms-excel");

        PrintWriter out = response.getWriter();

        out.println("Name\t Job\t Salary");

        out.println("Aditya\t Trainee\t 5000");

        out.println("Pankaj\t Trainee\t 4000");

        out.println("Shyam\t Accountant\t 25000");

        out.println("Sonu\t Trainee\t 5000");

        out.close();

    }

}

Fig-12.jpg

Step 13

Now your project is ready to run.

Right-click on the project menu then select "Run" as shown below.

Fig-13.jpg

Step 14

Now the output is shown on your browser window is as in the following:

Fig-14.jpg

Now we have two choices to select, we select them one by one. First we select "Word Document" file. When we click, it starts downloading and then we open the downloaded file. It's shown in Microsoft Word according to their format.

The following figure shows the downloaded file in the left corner of the browser.

Fig-15.jpg

Now click on the downloaded file "wordServlet" to open it. This file contains the following contents.

Fig-16.jpg

Now we click on "Excel Sheet" to download it.

Fig-17.jpg

Now click on the downloaded file "excelServlet" to show its format and contents.

Fig-18.jpg

COMMENT USING

Trending up