Passing parameters in a response.sendRedirect() - JSP

不羁的心 提交于 2021-02-17 21:44:10

问题


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 "RedirectIfSuccessful.jsp", if invalid the page redirects to "RedirectIfFailed.jsp". I am using the response.sendRedirect() method to do this.

The redirect is working fine. However, I wish to access the name the user inputs in the form from RedirectIfSuccessful and RedirectIfFailed 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 request.getParameter("name") from both files but it's returning a null value.. What can I do to access it?

This is the code I have: This is the RedirectingPage.jsp

 <%@ page 
    language="java" 
    import="java.util.regex.*"
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%  
    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){
        response.sendRedirect("RedirectIfSuccessful.jsp");

    } else {
        response.sendRedirect("RedirectIfFailed.jsp");
    }

%>

This is the HTML file where I have the form: FormSubmit.html

<html>
    <head>
        <title> Welcome </title>
    </head>

    <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" >
        </form>
    </body>
</html>

And this is the Successful page:

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

<body>

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

</body>
</html>

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


回答1:


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>




回答2:


To get the name in another page, use session.

e.g. session.setAttribute("name",name) at the login page;

To retrieve the name, use session.getAttribute("name"). You can assign it to a variable like this:

<% String name=(string)session.getAttribute("name");%>

success!



来源:https://stackoverflow.com/questions/14020234/passing-parameters-in-a-response-sendredirect-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!