CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to Pass Data from JSP to Servlet

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

How to Pass Data from JSP to Servlet

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

Passing data from a JSP (JavaServer Pages) to a Servlet is a common task in Java web development. This guide will walk you through the process in a way that’s easy to understand, especially if you’re a college student just starting with Java web applications.

Prerequisites

  • Install and Set Up Java Development Environment
  • Set Up Eclipse with Tomcat Server for Java Web Development

Step-by-Step Guide

Let’s break down the process into simple steps:

  1. Create a JSP Form
  2. Set Up a Servlet
  3. Run and Test Your Application

Step 1: Create a JSP Form

First, you need a JSP page with a form where users can input data. This form will send data to the servlet.

Example JSP (index.jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Data Input Form</title>
</head>
<body>
    <h1>Enter Your Details</h1>
    <form action="DataServlet" method="post">
        Name: <input type="text" name="name"><br>
        Age: <input type="text" name="age"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

In this form:

  • The action attribute specifies the servlet URL pattern (DataServlet).
  • The method attribute is set to post for secure data transmission.
  • Input fields for name and age are provided for user input.

Step 2: Set Up a Servlet

Next, create a servlet that will handle the data submitted from the JSP form.

Example Servlet (DataServlet.java):

package com.codersathi;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DataServlet
 */
@WebServlet("/DataServlet")
public class DataServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Retrieve data from the request
		String name = request.getParameter("name");
		String age = request.getParameter("age");

		// Set response content type
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// Generate response
		out.println("<html><body>");
		out.println("<h1>Received Data</h1>");
		out.println("<p>Name: " + name + "</p>");
		out.println("<p>Age: " + age + "</p>");
		out.println("</body></html>");
	}

}

In this servlet:

  • The doPost method handles POST requests from the JSP form.
  • request.getParameter("name") retrieves the value of the name input field.
  • request.getParameter("age") retrieves the value of the age input field.
  • The servlet generates an HTML response displaying the received data.

Step 3: Run and Test Your Application

3.1. Run your application

Use an IDE like Eclipse or IntelliJ IDEA to deploy your web application to a servlet container like Apache Tomcat.

3.2. Access the JSP Form

Open your web browser and navigate to http://localhost:8080/YourProjectName/index.jsp.

Submit Data JSP Form

3.3. Submit Data

Enter data in the form fields and click Submit button to send data to Servlet.

3.4. View the Response

The servlet processes the submitted data and displays it in the browser.

Receive data from jsp to servlet

Related Posts:

  • Step-by-Step Guide to Set Up Eclipse with Tomcat…
  • Pass by Value vs. Pass by Reference in Java
  • Install and Set Up Java Development Environment
  • Building a Simple JDBC Project: Student Management System
  • Control Statements in Java
  • Install Tomcat
Tags:javajspserver-side-prograingservlet
Was this article helpful?
โ† Previous ArticleMySQL Commands for Developers
Next Article โ†’Step-by-Step Guide to Set Up Eclipse with Tomcat Server for Java Web Development

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap