我是 Java EE 的初学者,在创建登录页面时遇到问题。我制作了一个通用登录页面,工程师可以通过该页面登录到他/她的帐户。工程师的用户名以“engg”开头。因此,我编写了以下代码,并在输入错误的用户名(例如我的表中不存在的“uname100”)时出现 IllegalStateException 错误。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.java2d.pipe.GeneralCompositePipe;
import accessdb.Dao;
import accessdb.Getset;
/**
* Servlet implementation class AdminLogIn
*/
public class AdminLogIn extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @return
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
String username=request.getParameter("username");
String password=request.getParameter("password");
Getset g=new Getset();
Dao dao=new Dao();
g.setuname(username);
String newuname=username.substring(0,4);
// System.out.println(""+username);
if(newuname.equals("engg"))
{
ResultSet rs=dao.EnggLogInUseridCheck(g);
System.out.println(""+newuname);
while(rs.next())
{
String uname=rs.getString("uname");
System.out.println(""+uname);
if(uname.equals(username))
{
g.setuname(username);
System.out.println(""+username);
System.out.println(""+uname);
ResultSet rs1=dao.EnggLogInPasswordCheck(g);
while(rs1.next())
{
String password1=rs1.getString("password");
if(password1.equals(password))
{
System.out.println(""+password1);
response.sendRedirect("engghome.jsp");
break;
}
else
{
response.sendRedirect("login.jsp");
return;
}
}
break;
}
else
{
response.sendRedirect("login.jsp");
return;
}
}
}
else
{
response.sendRedirect("login.jsp");
return;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}