java - 错误 : null value being entered in table through jsp code

标签 java mysql jsp tomcat database-connection

我的代码片段如下:

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

ShopRegister3.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 然而在我的数据库中,正在为属性输入空值:名称、密码和位置。 我已经尝试了很多东西...,到底哪里出错了?

最佳答案

您的 session 变量总是设置为空; 使用:

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

在 session 中设置值之前获取值

关于java - 错误 : null value being entered in table through jsp code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136860/

相关文章:

java - Arraylist.get 返回 null

java - 渗透 : java. sql.SQLException : Access denied for user 'root' @'localhost' (using password: YES). 如何不使用密码?

java - 从 lib 目录加载 jar 文件的顺序

php - 如何将.txt文件导入不同表的mysql数据库

c# - 如何将从 MySQL 数据库获取的单行存储在变量中?

javascript - 在jsp中将值从servlet传递到javascript

Java Servlets 转发一个 View ,其中有一个指向 css 的链接

java - 如何正确使用Reflector Class.cast()?

MySQL/Linux : Custom stopword file - permission denied

javascript - 使用 javascript 显示过去 3 个月的下拉列表