JSP and Servlet Interaction in Java with an example

Below example shows how to print the data that is retrieved from a Form using a JSP and then passing it to a Servlet for printing it on the GUI.
What components are needed for this ?
  • JSP Page with a form input
  • A Controller 
  • Web Configuration file (web.xml)
JSP Page :

Below is the code for the JSP Page with a Form input. The Form has the below input fields.
  • First name
  • Last name
  • City
The JSP Page code looks like below.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
 <form action="/Index" method="post">
        <input type="text" name="fname"/><br>        
        <input type="text" name="lname"/>
        <input type="text" name="city"/>                                                                                                                                                                         
        <input type="submit" value="submit">            


Once we have the JSP with the Form data, we need to create the Controller. Below is the code for the controller.

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 ControlServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String city = request.getParameter("city");
        System.out.println("FirstName :"+ fname);
        System.out.println("Lastname :"+ lname);
        System.out.println("City :"+ city);

Once we have created the Controller, we need to map this in the web.xml file. Below is the code for the web.xml file for the servlet mapping.


Hope this example helps you understand how you can pass the data from the JSP to a Servlet.

Unable to load JNI shared library Error in spring tool suite Installation

Friends, many of you would have faced this issue when opening the spring IDE. Below is the error message.

Error : Failed to load JNI shared library C:\Program Files(x86)\Java\jre1.8.0_40\bin\client\jvm.dll

How to resolve this issue ? :

First thing to do in this situation is to check the versions of OS, JDK & STS.

  • 32-bit OS - 32-bit JDK - 32-bit STS (32-bit only)
  • 64-bit OS - 32-bit JDK - 32-bit STS
  • 64-bit OS - 64-bit JDK - 64bit STS (64-bit only)

Second thing to do is to check the make sure that the JRE/JDK path is pointed correctly in the config uration file. It should look like below.

C:\<path to 64 bit program files>\Java\<jdk version>\bin\javaw.exe
After making sure the above two checks the error will be resolved.

How to Pass data from a JSP page to a Servlet using Plain Java J2EE

Below example explains how to use the JSP to get the Form data input and pass it to the servlet to store/update the database.

Step 1:  Create a class that extends the HttpServlet() as shown below.
public class FormServlet extends HttpServlet {}
Step 2 : 

Map this servlet in the web.xml as shown below
Step 3 :  In the Form action POST method , point the action to the Form as shown below.
<form action="form" method="post">
Step 4 :
Then create a doPost() method in your servlet which takes the input as request parameters. The input field name is the request parameter name.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String fname = request.getParameter("firstname");
    String lname = request.getParameter("lastname");
Step 5 : 

Pass these details to the persistence API's as show below..
User user = new User(fname, lname);