我刚刚开始学习jsp,我遇到了一个特殊的问题。我一直试图通过从数据库中获取电子邮件来预先填充我的html。员工需要输入他们的员工ID,并根据员工ID将电子邮件发送到表单
我试图使用jstl发送员工ID,我失败了我总是得到一个错误,说员工id为null,也许我不明白使用servlets或jstl发送雇员id的逻辑
用户registration.jsp
<div class="regtop">
<h2>User Registration</h2>
</div>
<!-- <form action="RegisterControllerServlet"> -->
<c:url var="templink" value="RegisterControllerServlet">
<c:param name="command" value="LOAD"/>
</c:url>
<div class="container">
<p>Please fill in this form to create an account.</p>
<hr>
<label for="email"><b>Enter employee id</b></label>
<input type="text" name="u_eid" >
<a href="${templink}" >Get email</a>
<br>
<label for="email"><b>Email</b></label>
<input type="text" name="email" value="${THE_USER.email}">
register controller Servlet.Java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//read the "command" parameter
String theCommand=request.getParameter("command");
//if the command is missing,then default to listing pharmacists
//route to the appropriate method
switch(theCommand)
{
case "LOAD":
loadUser(request,response);
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
throw new ServletException(e);
}
}
private void loadUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
String eid=request.getParameter("u_eid");
Employee themp=registerDbutil.getUser(eid);
//plaqce user in the request attribute
request.setAttribute("THE_USER", themp);
//send to jsp page:update -user-reg.jsp
RequestDispatcher dispatcher=
request.getRequestDispatcher("/user registartion.jsp");
dispatcher.forward(request,response);
}
register D but IL.Java
public Employee getUser(String eid) throws Exception{
Employee theuser=null;
Connection myConn =null;
PreparedStatement myStmt=null;
ResultSet myRs=null;
try
{
//get connection to db
myConn=dataSource.getConnection();
//create sql to get selected user email
String sql="select email from employee where eid=? ";
//create prepared statements
myStmt=myConn.prepareStatement(sql);
myStmt.setString(1, eid);
//execute state
myRs=myStmt.executeQuery();
if(myRs.next())
{
String email=myRs.getString("email");
theuser=new Employee(email);
}
else
{
throw new Exception("Could not find employee id:" +eid);
}
return theuser;
}finally
{
close(myConn,myStmt,null);
}
}
我希望输出在输入员工ID时预先填充员工的html表单
它是null,因为当您单击RegisterControllerServlet
的链接时,您没有将employee id作为参数传递:
<c:url var="templink" value="RegisterControllerServlet">
<c:param name="command" value="LOAD"/>
</c:url>
您需要在此JSTL url标记中添加employee id作为参数...但是因为您希望用户在输入字段中编写employee id,所以您需要使用form标记,而不是JSTL url标记(因为您不知道员工的身份是什么)。
<!-- <c:url var="templink" value="RegisterControllerServlet">
<c:param name="command" value="LOAD"/>
</c:url> -->
<div class="container">
<p>Please fill in this form to create an account.</p>
<hr>
<form action="RegisterControllerServlet">
<label for="email"><b>Enter employee id</b></label>
<input type="text" name="u_eid" >
<label for="email"><b>Email</b></label>
<input type="text" name="email" value="${THE_USER.email}">
<input type="submit" name="command" value="LOAD"/>
</form>
</div>