Bernice Bernice - 1 year ago 182
HTML Question

Passing parameters in a response.sendRedirect() - JSP

I am new to Web Technologies. I am trying to do a simple program which ask the user to input a name, if valid the page redirects to another jsp file

, if invalid the page redirects to
. I am using the
method to do this.

The redirect is working fine. However, I wish to access the name the user inputs in the form from
files so that when a valid name is entered the user is presented with a : Welcome, nameEntered and when failed the message would be nameEntered was not valid. Please go back and try again.

I tried using
from both files but it's returning a
value.. What can I do to access it?

This is the code I have: This is the

<%@ page
contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

String name = request.getParameter("name");

final String NAME_PATTERN = "^[a-zA-Z]{3,15}$";
Pattern pattern = Pattern.compile(NAME_PATTERN);

Matcher matcher = pattern.matcher(name);

if (matcher.matches() == true){

} else {


This is the HTML file where I have the form:

<title> Welcome </title>

<body BGCOLOR="#FDF5E6">
<p> <i> This program redirects to a page if the name entered is valid and to another one if name
entered is invalid... This uses response.sendRedirect() </i> </p>

<form action="RedirectingPage.jsp" method="post">
<font size=6 face="Georgia"> <strong> Enter your name: </strong> </font> <input type="text" name="name"> <br> <br>
<input type="submit" name="btn" value="Submit" >

And this is the Successful page:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Successful Submit </title>


<font face="Georgia" size="6"> Hello, <% request.getParameter("name"); %> </font>


I hope you can help and I was clear in my question. Thanks :)

Answer Source

A redirect consists in sending a response to the browser saying "Please go to the following URL : RedirectIfSuccessful.jsp".

When it receives this response, the browser sends a new request to RedirectIfSuccessful.jsp, without any parameter. So getting the parameter name from RedirectIfSuccessful.jsp will return null.

If you want to have access to the name after the redirection, you need to send a redirect to RedirectIfSuccessful.jsp?name=<the name the user entered>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download