package com.course;import java.io.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;public class login_confirm extends HttpServlet{//響應Post請求public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { String message=null; String id=null;//接收用戶的登錄名id=req.getParameter("id");//創建session對象HttpSession session=req.getSession(true);//將用戶登錄名存入session中session.setAttribute("id",String.valueOf(id));String password=null;//接收用戶登錄的密碼password= req.getParameter("password");String kind =null;//接收用戶級別kind=req.getParameter("kind");//調用getPassword方法,獲取數據庫中查詢出來的密碼String temp =getPassword(req,res,id,kind);//對比查詢出的密碼和用戶輸入的密碼是否匹配if(password.equals(temp))//密碼輸入正確,調用goo方法goo(req,res,kind);else {//密碼輸入錯誤message="用戶名或密碼有誤!";doError(req,res,message) ;}} //根據用戶的級別,分別轉向不同的頁面……//根據用戶的級別和輸入的用戶名,查詢對應的密碼 public String getPassword(HttpServletRequest req, HttpServletResponse res,String id,String kind)throws ServletException, IOException {//聲明數據庫連接類sqlBean的實例sqlBean db= new sqlBean(); String pw=""; String sql="select password from "+kind+" where id='"+id+"'";try{//進行數據庫查詢操作ResultSet rs=db.executeQuery(sql); if(rs.next() ){pw= rs.getString("password");}} catch(Exception e){ System.out.print(e.toString());} return pw; }//處理錯誤頁面public void doError(HttpServletRequest req,HttpServletResponse res,String str)throws ServletException, IOException {req.setAttribute("problem", str);RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");rd.forward(req, res);}…… }我想問題應該出在.equals()語句上,因為若把報錯語句改成message=password+","+temp+"用戶名或密碼有誤!";
在屏幕上顯示的password和temp相同,可是卻總執行doerror頁面!
一只甜甜圈
2023-03-12 18:14:20