How to read GET request parameters in servlet jsp

 Posted On  | Yashwant Chavan 

In this tutorial, You will learn how to read GET request parameters in servlet and jsp. This post will help you to setup Java web application using servlets and jsp.

Servlet Jsp - Eclipse Project set up

Read GET request parameters in servlet jsp - Eclipse Project Setup

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>ServletJspExamples</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>HelloController</servlet-name>
        <servlet-class>com.technicalkeeda.servlet.HelloController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloController</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

HelloController.java

package com.technicalkeeda.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController extends HttpServlet {
    private static final long serialVersionUID = 1 L;

    public HelloController() {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");

        Map < String, String > name = new HashMap < String, String > ();
        name.put("first_name", firstName);
        name.put("last_name", lastName);

        request.setAttribute("name", name);

        RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
        rd.forward(request, response);
    }

}

result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Map" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <body>
      <h2>Result Page</h2>
      <% 
         Map<String,String> name = (Map<String,String>) request.getAttribute("name");
         %>
      <table cellpadding="1"  cellspacing="1" border="1" bordercolor="gray">
         <tr>
            <td>First Name</td>
            <td>Last Name</td>
         </tr>
         <tr>
            <td><%=name.get("first_name") %></td>
            <td><%=name.get("last_name") %></td>
         </tr>
      </table>
   </body>
</html>

Deploy and Execute

Enter the Url http://localhost:8080/ServletJspExamples/hello?firstName=Yashwant&lastName=Chavan in your browser. Here we have two parameters "firstName" and "lastName" along with its values.

read GET request parameters in servlet jsp - output


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0302 seconds.