我使用JSP创建了一个Change Password
表单,并使用一个servlet页面来操作数据并将其输入到数据库中。
这是一个内部网站(内部网),因此数据库已包含有关所有用户Email, FName, LName
的详细信息。只有密码字段为空。我要做的是,在用户填写他们的电子邮件地址,密码,确认它然后按下提交按钮后,servlet页面应该更新当前电子邮件的密码字段。不幸的是,在所有情况下,用户似乎都被重定向到ChangeError.jsp
。
请查看随附的两个文件。(ChangePassword.JSP
和ChangePassword.Servlet
)。任何帮助将不胜感激。
//ChangePassword.jsp
<FORM ACTION="ChangePassword" METHOD="GET">
<div id="login_box">
<div id="login_header">
Login
</div>
<div id="form_val">
<div class="label">Email:</div>
<div class="control"><input type="text" name="Email" id="Email"/></div>
<div class="label">Password:</div>
<div class="control"><input type="password" name="Password" id="Password"/></div>
<div class="label">Confirm Password:</div>
<div class="control"><input type="password" name="Password" id="Password"/></div>
<div style="clear:both;height:0px;"></div>
<div id="msgbox"></div>
</div>
<div id="login_footer">
<!-- <script type="text/javascript">
function closewindow() {
window.close()
}
</script>-->
<label>
<input type="submit" onclick="location.href='http://localhost:8080/IntegrateAll/Index.jsp'" window.close();" name="Submit" id="Submit" value="Submit" class="send_button" />
</label>
</div>
</div>
//ChangePassword.servlet
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import javax.naming.*;
public class ChangePassword extends HttpServlet {
private static final long serialVersionUID = 1L;
public ChangePassword() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String Email = request.getParameter("Email");
String Password = request.getParameter("Password");
String CPassword = request.getParameter("Password");
Connection con = GetConnectToDb();
boolean flag = CheckLogin(Password,CPassword,Email,con);
if(flag==true){
gotoPage("/LoginPage.jsp",request,response);
}
else{
gotoPage("/ChangeError.jsp",request,response);
}
}
public void gotoPage(String address, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
public boolean CheckLogin(String Password, String CPassword,String Email, Connection con) {
if (Password != null && CPassword != null && Email !=null) {
try {
PreparedStatement ps = con.prepareStatement("UPDATE PUBLIC.IAUSERS SET Password = ('"+Password+"') WHERE Email='"+Email+"'");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
return true;
}
} catch (Exception e) {
}
}
return false;
}
public Connection GetConnectToDb() {
try {
InitialContext ctx = new InitialContext();
Connection con = null;
DataSource ds = (DataSource) ctx.lookup("java:/DefaultDS");
con = ds.getConnection();
return con;
} catch (Exception e) {
return null;
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}}
答案 0 :(得分:4)
必须使用executeUpdate()
执行更新查询,而不是executeQuery()
。
您的代码中还有其他问题:
if (flag == true)
很难看。使用if (flag)
id
属性的元素。 ID应该是唯一的。