简体   繁体   中英

Java Server Pages. How to send parameters from one JSP page to another

So m having two JSP page one is register.jsp which consists of registration form. I'm calling a 2nd JSP page when user clicks submit button on register.jsp page. The 2nd JSP page is for adding the data to the database. What i want to do is from this second page i want to go back to my previous page and display a message if registration is successful or not. Everything else is working just fine. Data is being added to the database. This is my registration form, m calling anther jsp page on clicking submit button.

<head>
</head>
<%!
String val;
%>
<article class="registrationform">
    <%
//Here m trying to access the attribute val which is set in the 2nd jsp page and whose value is true or false depending on data is added successfully or not.
    val=(String)request.getSession().getAttribute("val");
    if(val.equals("true"))
    {
    %>
    <h2 color="blue">Registration Successfull </h2>
    <%
    }else if(val.equals("false"))
    {
    %>
        <h2 color="red">Registration Failed. Please Try Again</h2>
    <%
    } else{

    }
    %>
    <h2>Registration form</h2>

    <form class="registration form-horizontal" name="regform" method="post" action="http://localhost:8080/mysrc/vt/components/jsp/RegisterDataBase.jsp">

      <fieldset id="personalinfo">
        <legend>Personal Info</legend>

                <section class="row">
                    <label class="col col-lg-4 control-label" for="myfirstname">First Name</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="text" name="myfirstname" id="myfirstname">
                    </div><!-- controls -->
                </section><!-- row -->
<section class="row">
                    <label class="col col-lg-4 control-label" for="mylastname">Last Name</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="text" name="mylastname" id="mylastname" >
                    </div><!-- controls -->
                </section><!-- row -->


                <section class="row">
                    <label class="col col-lg-4 control-label" for="myemail">Email</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="email" name="myemail" id="myemail" required autocomplete="off" />
                    </div><!-- controls -->
                </section><!-- row -->

                <section class="row">
                    <label class="col col-lg-4 control-label" for="mypassword">Password</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="password" name="mypassword" id="mypassword" required autocomplete="off" />
                    </div><!-- controls -->
                </section><!-- row -->

      </fieldset><!-- personal info -->

      <fieldset id="otherinfo">
            <legend>Other Info</legend>

                <section class="row">
                  <label class="col col-lg-4 control-label" for="dateofbirth">D.O.B</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="date" name="dateofbirth" id="dateofbirth" />
                    </div><!-- controls -->
                </section><!-- row -->

                                <section class="row">
            <label class="col col-lg-4 control-label">Gender</label>
                    <div class="controls col col-lg-8">
                        <label class="radio">
                            <input type="radio" name="gender" value="male" /> Male
                        </label>
                        <label class="radio">
                            <input type="radio" name="gender" value="female" /> Female
                        </label>
                    </div><!-- controls -->
                </section><!-- row -->


                <section class="row">
                    <label class="col col-lg-4 control-label" for="collegename">College</label>
                    <div class="controls">
                        <input class="col col-lg-8" type="text" name="college" id="collegename" />
                    </div><!-- controls -->
                </section><!-- row -->
                <section class="row">
                    <label class="col col-lg-4 control-label" for="myaddress">Address</label>

                    <div >
                        <textarea class="col col-lg-8" type="text" name="address" id="myaddress" rows="3"></textarea>
                    </div><!-- controls -->
                </section><!-- row -->
    </fieldset>
      <button class="btn" type="submit" >Submit</button>

    </form>

</article>

this is the 2nd JSP page with data base connectivity where the data is being added to the database. Now i want to again redirect back to previous page and display a pop stating registration successful is data is added successfully else failed.

<%@ page language="java" %>

<%@ page import="java.sql.*" %>
<%!     
    String myfirstname, mylastname, myemail, mypassword, dateofbirth, gender,college, address;  
    Connection con;
    PreparedStatement pst;
%>

<% 
myfirstname=request.getParameter("myfirstname");
mylastname=request.getParameter("mylastname");
myemail=request.getParameter("myemail");
mypassword=request.getParameter("mypassword");
dateofbirth=request.getParameter("dateofbirth");
gender=request.getParameter("gender");
college=request.getParameter("college");
address=request.getParameter("address");

        try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection("jdbc:odbc:vtdsn");
                    pst=con.prepareStatement("insert into studentdetail values(?,?,?,?,?,?,?,?)");
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }

        try{
                     pst.setString(1,myfirstname);
                     pst.setString(2,mylastname);
                     pst.setString(3,myemail);
                     pst.setString(4,mypassword);
                     pst.setString(5,dateofbirth);
                     pst.setString(6,gender);
                     pst.setString(7,college);
                     pst.setString(8,address);
                     int i=pst.executeUpdate();
                     if(i>=1)
                     {

//this is what i tried to set an attribute as true is data is added successfully which is being accesses at the previous page but m getting errors.

                     //request.getSession().setAttribute("val", "true");

                     response.sendRedirect("http://localhost:8080/mysrc/vt/register.jsp");
                     }
                     else
                     {
                     response.sendRedirect("http://localhost:8080/mysrc/vt/error.html");
                     }
                    }
                    catch(Exception e){
                                        e.printStackTrace();
                                        }

%>

Send parameters from one jsp to another has three ways: 1. Store parameters in session then redirect, just as what you did. 2. Set parameter in the url then redirect. 3. Store parameter in request and forward the request to the next jsp.

As far as I know , your way should be ok, so what specific error did you get?

besides, val=(String)request.getSession().getAttribute("val"); this val may be null so if(val.equals("true")) can throw NullPointer Exception try if("true".equals(val)) instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM