jsp中mysql跳转后为空白,将表单提交到与数据库交互的Servlet,结果显示在空白页中...

I've a servlet that checks username and password from database.

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mvs_user", "root", "pass");

if (req.getParameter("usrnm") != null && req.getParameter("pwd") != null) {

String username = req.getParameter("usrnm");

String userpass = req.getParameter("pwd");

String strQuery = "select * from user where username='" + username + "' and password='" + userpass + "'";

System.out.println(strQuery);

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(strQuery);

if (rs.next()) {

req.getSession(true).setAttribute("username", rs.getString(2));

res.sendRedirect("adminHome.jsp");

} else {

res.sendRedirect("index.jsp");

}

} else {

res.sendRedirect("login.jsp");

}

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

The problem is the browser only displays a blank page and yet I expect it to display "Hello World" in the redirected page. Where could the problem be? Please help me troubleshoot.

解决方案

You need to properly handle exceptions. You should not only print them but really throw them.

Replace

} catch (Exception e) {

e.printStackTrace(); // Or System.out.println(e);

}

by

} catch (Exception e) {

throw new ServletException("Login failed", e);

}

With this change, you will now get a normal error page with a complete stacktrace about the cause of the problem. You can of course also just dig in the server logs to find the stacktrace which you just printed instead of rethrowed.

There are several possible causes of your problem. Maybe a ClassNotFoundException or a SQLException. All which should be self-explaining and googlable.

See also:

Unrelated to the concrete problem, your JDBC code is prone to resource leaking and SQL injection attacks. Do a research on that as well and fix accordingly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值