我的登录表单不起作用,请帮助我...
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName=request.getParameter("userName");
String password=request.getParameter("password");
DataPersistor d=new DataPersistor();
try {
if( d.isValidateUser(userName, password)){
HttpSession session=request.getSession();
session.setAttribute("name", userName);
response.sendRedirect("index.jsp");
}
else{
response.sendRedirect("login.jsp");
}
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的数据持久化类。我从数据库中获取值,
public class DataPersistor {
Connection con;
public DataPersistor() {
con = DBConnector.getConnection();
}
public void registerUser(User user) {
try {
Statement stmt = con.createStatement();
String query = "INSERT INTO user VALUES ('" + user.getUserName() + "','" + user.getPassword() + "','" + user.getEmail() + "','" + user.getLastName() + "','" + user.getFirstName() + "','false')";
stmt.execute(query);
} catch (SQLException ex) {
Logger.getLogger(DataPersistor.class.getName()).log(Level.SEVERE, null, ex);
}
}
public boolean isValidateUser(String username,String password) throws SQLException{
boolean result=false;
PreparedStatement ps;
Statement stmt = con.createStatement();
String query = "SELECT * FROM user WHERE username='" + username + "' and password='"+password+"'";
ps=con.prepareStatement(query);
ps.setString(1,username);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
if(rs.next()==true){
result=true;
}
return result;
}
}
清理和构建之后
Jul 29, 2012 8:51:46 PM com.asiahospital.servlet.LoginServlet doPost
SEVERE: null
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553)
at com.asiahospital.db.DataPersistor.isValidateUser(DataPersistor.java:50)
at com.asiahospital.servlet.LoginServlet.doPost(LoginServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)`enter code here`