Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java服务器页面。如何将参数从一个JSP页面发送到另一个JSP页面_Java_Javascript_Jquery_Jsp - Fatal编程技术网

Java服务器页面。如何将参数从一个JSP页面发送到另一个JSP页面

Java服务器页面。如何将参数从一个JSP页面发送到另一个JSP页面,java,javascript,jquery,jsp,Java,Javascript,Jquery,Jsp,所以我有两个JSP页面 一个是register.jsp,它由注册表组成。当用户单击register.JSP页面上的submit按钮时,我正在调用第二个JSP页面。第二个JSP页面用于将数据添加到数据库。 我想做的是从这第二页我想回到我的前一页,并显示一条消息,如果注册成功与否。 其他一切都很好。正在将数据添加到数据库中。 这是我的注册表,我点击提交按钮调用另一个jsp页面 <head> </head> <%! String val; %> <articl

所以我有两个JSP页面 一个是register.jsp,它由注册表组成。当用户单击register.JSP页面上的submit按钮时,我正在调用第二个JSP页面。第二个JSP页面用于将数据添加到数据库。 我想做的是从这第二页我想回到我的前一页,并显示一条消息,如果注册成功与否。 其他一切都很好。正在将数据添加到数据库中。 这是我的注册表,我点击提交按钮调用另一个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)
{
//这就是我试图将一个属性设置为true的地方,因为成功添加了数据,这是在上一页访问的数据,但是我得到了错误。
//request.getSession().setAttribute(“val”、“true”);
response.sendRedirect(“http://localhost:8080/mysrc/vt/register.jsp");
}
其他的
{
response.sendRedirect(“http://localhost:8080/mysrc/vt/error.html");
}
}
捕获(例外e){
e、 printStackTrace();
}
%>

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

据我所知,你的方式应该是好的,那么你得到了什么具体的错误

此外,, val=(字符串)request.getSession().getAttribute(“val”); 此val可能为空 所以 if(val.equals(“true”)可以引发NullPointer异常 尝试
如果改为“true”.equals(val))

您认为将业务逻辑代码放在JSP中是一种好的做法吗?JSP用于视图层。还有数据库访问。。你应该考虑重新组织你的代码。@ GALVICS你能解释为什么在JSP中保持业务逻辑代码是不好的吗?m JSPval的新成员=”;val=val+session.getAttribute(“val”);这就是问题所在,现在已经解决了。谢谢:)