Java Server Pages。如何将参数从一个JSP页面发送到另一个JSP页面

时间:2014-12-10 07:38:30

标签: java javascript jquery jsp

所以我有两个JSP页面 一个是register.jsp,它由注册表组成。当用户点击register.jsp页面上的提交按钮时,我正在调用第二个JSP页面。第二个JSP页面用于将数据添加到数据库。 我想要做的是从第二页我想回到我的上一页,如果注册成功与否则显示一条消息。 其他一切都很好。数据正在添加到数据库中。 这是我的注册表单,在点击提交按钮时调用anther jsp页面。

<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>

这是第二个具有数据库连接的JSP页面,其中数据被添加到数据库中。现在我想再次重定向回到上一页并显示一个弹出声明注册成功是数据添加成功否则失败。

<%@ 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();
                                        }

%>

1 个答案:

答案 0 :(得分:0)

从一个jsp发送参数到另一个jsp有三种方式: 1.在会话中存储参数然后重定向,就像你所做的那样。 2.在url中设置参数,然后重定向。 3.将参数存储在请求中并将请求转发给下一个jsp。

据我所知,你的方式应该没问题,所以你得到了什么具体的错误?

之外, VAL =(字符串)request.getSession()的getAttribute( “VAL”)。 这个val可能为null 所以 if(val.equals(“true”))可以抛出NullPointer异常 尝试 if(“true”.equals(val))而不是