[code="java"][size=large]import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet {
private PreparedStatement pstmt;
private PreparedStatement stmtQuery;
private ResultSet rset;
/**
* Constructor of the object.
*/
public Registration() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ID = request.getParameter("ID");
String password = request.getParameter("password");
HttpSession httpSession = request.getSession();
IDInformation IDInf = new IDInformation(ID, password);
httpSession.setAttribute("IDInf", IDInf);
out.println("<form method = \"post\" action = "
+ "\"http://localhost:8080/RegistrationLogInSystem/servlet/Registration\">");
out.println("<br>Your ID and Password are : ");
out.println("<br>ID: " + ID);
out.println("<br>Password: " + password);
out.println("<br><input type = \"submit\" value = \"Confirm\">");
out.println("</form>");
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
IDInformation IDInf = (IDInformation) session.getAttribute("IDInf");
String ID = IDInf.ID;
String password = IDInf.password;
try {
[color=red]while (rset.next()) {
if (ID == rset.getString(1)) {
out.println("The ID " + rset.getString(1)
+ "has already existed");
return;
}
out.println(rset.getString(1));
}[/color]
if (ID.length() == 0 || ID.length() > 20) {
out.println("ID's length must between 0~20");
return;
}
if (password.length() == 0 || password.length() > 20) {
out.println("password's length must between 0~20");
return;
}
pstmt.setString(1, ID);
pstmt.setString(2, password);
pstmt.executeUpdate();
out.println("Registration Successed");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
out.close();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
initJdbc();
}
private void initJdbc() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "179401");
pstmt = conn
.prepareStatement("insert into RegistrationTable (ID, password) values (?, ?)");
[color=red]stmtQuery = conn.prepareStatement("select ID from RegistrationTable");
rset = stmtQuery.executeQuery();[/color]
System.out.println("Init Successfully!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class IDInformation {
private String ID = "";
private String password = "";
public IDInformation(String ID, String password) {
this.ID = ID;
this.password = password;
}
}
}[/size]
[/code]
这是一个我自己写的注册页面
我从一个HTML页面获取ID和password,然后到数据库获取一个ResultSet查看注册页面填写的ID是否已经注册.
红色部分是有关结果集的代码,但是每一次confirm以后都是Registration Successfully.
即使我输入了数据库中已有的ID也会成功,也就说判断语句不起作用,帮我看看为什么