登录Servlet没有响应
问题描述:
我想在JSP中使用会话API制作一个基本的登录系统。我认为我的代码很好,当我在tomcat(Eclipse)中运行系统时,填充表单后,servlet加载并且根本不响应(无输出)。登录Servlet没有响应
这是我的login.jsp:
<html>
<body>
<form action="loginservlet" method="post">
<h1>Please Login</h1>
Login: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type=submit value="Login">
</form>
</body>
</html>
这是我loginservlet:
package connectm;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginservlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
String username="";
String userpass="";
String strQuery="";
Statement st=null;
ResultSet rs=null;
HttpSession session = request.getSession(true);
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName,
password);
if (request.getParameter("username") != null
&& request.getParameter("username") != ""
&& request.getParameter("password") != null
&& request.getParameter("password") != "") {
username = request.getParameter("username").toString();
userpass = request.getParameter("password").toString();
/*strQuery = "select * from user where username='" + username
+ "' and password='" + userpass + "'";*/
//System.out.println(strQuery);
strQuery = "select username from user where username= ? and password= ?";
st = conn.createStatement();
PreparedStatement ps=conn.prepareStatement(strQuery);
ps.setString(1, username);
ps.setString(2, userpass);
rs = ps.executeQuery(strQuery);
int count = 0;
while (rs.next()) {
session.setAttribute("username", rs.getString(1));
count++;
}
if (count > 0) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("login.jsp");
}
} else {
response.sendRedirect("login.jsp");
}
out.println("Connected to the database");
conn.close();
out.println("Disconnected from database");
} catch (Exception e) {
// TODO: handle exception
}
}
}
我,什么地方出了错,并在那里真的一无所知。如果有人能指出错误/错误,我会很感激。
答
这里,
} catch (Exception e) {
// TODO: handle exception
}
你完全吞噬,而忽略例外。如果抛出异常,你确实会这样得到一个空白页面。完成TODO
。 处理的例外。例如。
} catch (Exception e) {
throw new ServletException("Login failed", e);
}
然后你就会得到一个不错的服务器的默认HTTP 500错误页面,其中包含有关该问题的原因,这应该是自我解释不够详细所有完整的堆栈跟踪。例如,一个ClassNotFoundException: com.mysql.jdbc.Driver
表示您没有将JDBC驱动程序放入类路径中,或者SQLException: no suitable driver
表示JDBC连接URL错误等等。
无关的具体问题,您应该不使用out.println("Connected to the database");
,而你控制一个servlet请求/响应写入响应体等。而是使用System.out.println()
或记录器。
@BalusC我做了你指出的两件事。堆栈跟踪帮助和问题是'ClassNotFoundException:com.mysql.jdbc.Driver'。但是很奇怪,因为我已经将mysql驱动的jar文件放在项目的构建路径中,并且在(eclipse)之前工作正常: – Anurag 2011-05-03 17:21:17
JAR需要放在'/ WEB-INF/lib'文件夹中,该文件夹默认是webapp运行时类路径的一部分。你不需要做其他事情。当您以正确的方式使用'/ WEB-INF/lib'时,Eclipse会自动设置构建路径。因此,您首先需要撤消构建路径中的所有手动更改/设置。 – BalusC 2011-05-03 17:25:25
@BaluC如你所说,把mysql连接器放在lib文件夹中,但仍然是相同的错误: – Anurag 2011-05-03 17:32:59