在表单提交时,请求被传递给httpservlet请求,其中我试图将值插入用户模型(使用setter方法),如下所示:
的Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Request Parameters from Registraion Servlet: ");
System.out.println("=========================================== ");
System.out.println(request.getParameter("uname")); //Doesn't print anything in console
System.out.println(request.getParameter("lname")); //Doesn't print anything in console
System.out.println(request.getParameter("email")); //Doesn't print anything in console
System.out.println(request.getParameter("pwd")); //Doesn't print anything in console
try {
System.out.println("Registration Servlet INSIDE try block:"); //Doesn't print anything in console
User user = new User();
user.setFirstName(request.getParameter("uname"));
user.setLastName(request.getParameter("lname"));
user.setEmail(request.getParameter("email"));
user.setPassword(request.getParameter("pwd"));
RegistrationDAO rsDao = new RegistrationDAO();
rsDao.insert(user);
} catch (Exception e) {
System.out.println("DB related Error");
}
System.out.println("Registration Servlet OUTSIDE try block:"); //Doesn't print anything in console
RequestDispatcher rd = request.getRequestDispatcher("EmailConfirmation.jsp"); //Successfully Redirects
rd.forward(request, response);
}
它成功重定向到我提到的JSP页面,但没有打印request.getparameters(xxx)
中的任何值。我想知道什么时候从表单的动作方法成功调用servlet,为什么不打印值?
抓挠我的头但却无法弄清楚出了什么问题?
这是我的HTML页面:
<form name="myForm" action="RegistrationServlet.do" method="POST" novalidate>
<input type="text" class="form-control input-sm" name="uname" ng-model="uname" ng-pattern="/^[a-zA-Z]{3,20}/" unique-username="" required placeholder="First Name" /><br>
<input type="text" class="form-control input-sm" name="lname" ng-model="lname" ng-pattern="/^[a-zA-Z]{3,20}/" required placeholder="Last Name"/><br>
<input type="email" class="form-control input-sm" name="email" ng-model="email" ng-pattern="/^[a-zA-Z]{3,20}/" required placeholder="Email"/><br>
<input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd" ng-model="pwd" required placeholder="Password"/><br>
<input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd2" ng-model="pwd2" value-matches="pwd" required placeholder="Confirm password"/><br>
<input type="checkbox" class="" name="agreement" value=""><small> I agree to RTH agreement</small>
<!--<button class="form-control btn btn-success" type="submit" ng-click="submit($event)">Connect now</button>-->
<input type="submit" class="form-control btn btn-success" value="Connect now" />
</form>
DAO课程:
public class RegistrationDAO {
public void insert(User user) {
try {
System.out.println("Printing USER Values");
System.out.println("====================");
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getEmail());
System.out.println(user.getPassword());
Connection con = DBConnection.getConnection();
String query = "insert into TBL_USER(USR_FIRST_NAME,USR_LST_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD) values(?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, user.getFirstName());
pst.setString(2, user.getLastName());
pst.setString(3, user.getEmail());
pst.setString(4, user.getPassword());
pst.executeUpdate();
} catch (Exception e) {
System.out.println("@@@@Record insertion error in Registration DAO@@@@");
System.out.println(e);
}
}
}
模特课:
public class User implements Serializable{
private String firstName;
private String lastName;
private String password;
private String email;
//setters and getters here
}
SQL表格式:
CREATE TABLE "RTH"."TBL_USER"
(
"USR_ID" VARCHAR2(1 BYTE) NOT NULL ENABLE,
"USR_IS_ANONYMOUS" RAW(1),
"USR_FIRST_NAME" CLOB,
"USR_MID_NAME" CLOB,
"USR_LST_NAME" CLOB,
"USR_PRIMARY_EMAIL" CLOB,
"USR_ALT_EMAIL" CLOB,
"USR_PRIMARY_CNCT_NMBR" CLOB,
"USR_SECONDARY_CNCT_NMBR" CLOB,
"USR_TYPE" CLOB,
"USR_CATEGORY" CLOB,
"USR_DOB" TIMESTAMP (0),
"USR_CREATE_DT" TIMESTAMP (0),
"CUPN_ID" VARCHAR2(1 BYTE),
"USR_MARITIAL_STUTUS" VARCHAR2(20 BYTE),
"USR_GENDER" VARCHAR2(1 BYTE),
"USR_IMAGE" RAW(1),
"USR_PASSWORD" VARCHAR2(20 BYTE),
CONSTRAINT "XPKTBL_USER" PRIMARY KEY ("USR_ID")
)
更新
问题是我没有调用dao类的insert(User user)
方法。现在它的工作,但我有插入语句String query = "insert into TBL_USER(USR_FIRST_NAME,USR_LST_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD) values(?,?,?,?)";
我收到错误:java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("RTH"."TBL_USER"."USR_ID")
UPDATE2:
很少研究我将sql查询更改为以下内容:
`String query = "update TBL_USER set USR_FIRST_NAME = ?, USR_LST_NAME = ?, USR_PRIMARY_EMAIL=?, USR_PASSWORD = ?";`
现在我没有得到任何错误,但表中没有插入
答案 0 :(得分:1)
您确定要用需要插入的值替换?你没有向我们展示你是如何做到的,我怀疑这是出错的地方。
答案 1 :(得分:0)
你也可以发布用户实体类吗?
您获得的第一个异常意味着您在表的列
上传递空值答案 2 :(得分:0)
创建序列并触发自动更新ID列修复了问题。这是解决方案:
CREATE TABLE "RTH"."TBL_USER"
(
"USR_ID" VARCHAR2(1 BYTE) NOT NULL ENABLE,
"USR_IS_ANONYMOUS" RAW(1),
"USR_FIRST_NAME" CLOB,
"USR_MID_NAME" CLOB,
"USR_LST_NAME" CLOB,
"USR_PRIMARY_EMAIL" CLOB,
"USR_ALT_EMAIL" CLOB,
"USR_PRIMARY_CNCT_NMBR" CLOB,
"USR_SECONDARY_CNCT_NMBR" CLOB,
"USR_TYPE" CLOB,
"USR_CATEGORY" CLOB,
"USR_DOB" TIMESTAMP (0),
"USR_CREATE_DT" TIMESTAMP (0),
"CUPN_ID" VARCHAR2(1 BYTE),
"USR_MARITIAL_STUTUS" VARCHAR2(20 BYTE),
"USR_GENDER" VARCHAR2(1 BYTE),
"USR_IMAGE" RAW(1),
"USR_PASSWORD" VARCHAR2(20 BYTE),
CONSTRAINT "XPKTBL_USER" PRIMARY KEY ("USR_ID")
);
自动递增ID值的序列和触发器:
CREATE SEQUENCE TBL_USER_SEQ
START WITH 1
INCREMENT BY 1
CACHE 100;
CREATE OR REPLACE TRIGGER "RTH"."TBL_USER_TRIGGER"
BEFORE INSERT ON TBL_USER
FOR EACH ROW
BEGIN
SELECT TBL_USER_SEQ.nextval
INTO :new.USR_ID
FROM dual;
END;