tomcat 错误:通过JSP代码在表中输入了空值

8wigbo56  于 2022-11-13  发布在  其他
关注(0)|答案(3)|浏览(131)

我的代码片段如下:
购物登记2.jsp:

<% 
session = request.getSession();
session.setAttribute("s_id", s_id);
out.println("Your id is : "+s_id);
        %>          
        <br>
        Please note it for further use.!<br>
        <form name="register" method="post" onsubmit=" return reg_form()"     action="ShopRegister3.jsp"><br>
        Enter your name : <input type="text" name="s_name" id="s_name"><br>
        Enter your Location : <input type="text" name="s_location" id="s_location"><br>
        Enter your password : <input type="password" name="s_password" id="s_password">    <br>
        Confirm Password : <input type="password" name="c_s_password"     id="c_s_password"><br>
        <input type="submit" value="Submit"><br>
        </form><br>

购物登记3.jsp:

String s_name=null, s_password=null, s_location=null;
int s_id = 0;   

session.setAttribute("s_name", s_name);
session.setAttribute("s_password", s_password);  
session.setAttribute("s_location", s_location);

String s_id_string=(String)request.getParameter("s_id");
s_id = session.getAttribute("s_id") != null ? (Integer) session.getAttribute("s_id") : 0 ;          
s_name=(String)session.getAttribute("s_name");
s_password=(String)session.getAttribute("s_password");
s_location=(String)session.getAttribute("s_location");

//entering values into Shop table
PreparedStatement s_insert = con.prepareStatement("insert into ShopSystem.Shop values(?,?,?,?)");
s_insert.setInt(1, s_id);
s_insert.setString(2, s_password);                  
s_insert.setString(3, s_location);
s_insert.setString(4, s_name);
int c = s_insert.executeUpdate();
//checking whether the value was inserted successfully
if(c>0)
{    %>
S_ID = <%= s_id %> has been registered successfully.<br>
<%  response.sendRedirect("ShopMenu3.jsp");     
} %>

在上面的代码中,服务器在运行ShopRegister3.jsp之后定向到ShopMenu3.jsp。然而,在我的数据库中,为属性输入了空值:名称,密码和位置。我已经尝试了很多东西...,到底哪里似乎是错误的?

watbbzwu

watbbzwu1#

您的会话变量始终设置为null;用途:

s_name=(String)request.getParameter("s_name");
s_password=(String)request.getParameter("s_password");
s_location=(String)request.getParameter("s_location");

在进程中设置值之前获取这些值

kiz8lqtg

kiz8lqtg2#

也许这就是问题所在?

String s_name=null, s_password=null, s_location=null;

然后将session设置为空值。

o2rvlv0m

o2rvlv0m3#

您必须在setSession语句之前首先请求该参数;因此在ShopRegister3.jsp中,s_name初始化为'null后。请求如下参数:

s_name = request.getParameter("s_name");

现在你可以对这个变量setSession;它现在不会为s_name变量返回null

相关问题