将表单提交到与数据库交互的Servlet,结果显示在空白页中 [英] Submitting form to Servlet which interacts with database results in blank page

查看:311
本文介绍了将表单提交到与数据库交互的Servlet,结果显示在空白页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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();
    }
}

问题是浏览器仅显示空白页面,但我希望它在重定向的页面中显示"Hello World".问题可能在哪里?请帮助我进行故障排除.

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.

推荐答案

您需要正确处理异常.您不仅应该打印它们,还应该真正打印throw.

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

替换

    } catch (Exception e) {
        e.printStackTrace(); // Or System.out.println(e);
    }

作者

    } 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.

有几种可能的原因导致您的问题.可能是ClassNotFoundExceptionSQLException.所有这些都应该是不言自明和可以谷歌浏览的.

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

  • How should I connect to JDBC database / datasource in a servlet based application?
  • How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
  • The infamous java.sql.SQLException: No suitable driver found

无关与具体问题无关,您的JDBC代码容易受到资源泄漏和SQL注入攻击的影响.也要对此进行研究并进行相应的修复.

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.

这篇关于将表单提交到与数据库交互的Servlet,结果显示在空白页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆