jsp文件,其中包含用戶名&在LoginServlet處理的密碼...此servlet在Access數據庫中執行驗證。但即時得到錯誤在Servlet中訪問數據庫
如果用戶名密碼&與Access數據庫則頁面重定向匹配flat_status.jsp
可以ANY1幫我..
package Validate;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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{
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception ex)
{
System.out.println("Initialize connector string");
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Connection conn;
Statement st;
ResultSet rs;
PrintWriter pw = response.getWriter();
String userName = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession(true);
response.setContentType("text/html");
try
{
conn=DriverManager.getConnection("jdbc:odbc:FBS");
st=conn.createStatement();
rs=st.executeQuery("select UserName,Password from user_login where UserName='"+userName+"' and Password='"+password+"'");
while(rs.next())
{
session.setAttribute("USerName",userName);
RequestDispatcher dispatch=request.getRequestDispatcher("/flat_status.jsp");
dispatch.forward(request, response);
}
st.close();
rs.close();
conn.close();
}
catch (SQLException ex)
{
pw.println(ex);
}
}
}
下面是異常的IM在瀏覽器中獲取窗口
java.sql.SQLException:[Microsoft] [ODBC Microsoft Access Driver]標準表達式中的數據類型不匹配。
哪一行是LoginServlet的第58行?我的猜測是它是'out.println'(ex.getMessage());',因爲你寫出來了,它被初始化爲null。 – 2012-02-07 15:07:05
@ ^^ ...這是什麼解決方案?當我刪除PrintWriter和使用System.out.println(ex.getMessage());它顯示空的servlet頁面後點擊提交按鈕 – Shaggy 2012-02-07 16:55:09
它顯示你一個空的頁面,因爲你有一個例外。如果您不想要空白頁面,則向響應寫入程序輸出錯誤消息。 – 2012-02-07 17:21:41