夕是四時落,阳是六時起,你之于我,就是最深最美的执念,你有没有一瞬间,心疼过我的执着
作者 时间 分类 学习 浏览 2817 评论

原本想把获取的信息的代码封装到一个类去,但是自己水平一般,能力有限,封装起来就出现问题,页面500错误。我的思路是发送到封装的类里,然后再调用,但不知道哪里出了错,一直出现500错误,最后实在没有办法,就写在一个servlet里了,等以后有能力了再修改再封装。

代码如下:

AdminDAO ad = new AdminDAO();

servlet()代码省略

response.setCharacterEncoding(“UTF-8”);

AdminName = request.getParameter(“AdminName”);
AdminPassword = request.getParameter(“AdminPassword”);

String sql = “select * from admin where AdminName =? “;

JDBCUtilDao jud = new JDBCUtilDao();
conn = jud.getConnection();
ps = jud.getPrepareStatement(sql);
try {
ps.setString(1, AdminName);
} catch (SQLException e1) {
System.out.println(“发送失败”);
e1.printStackTrace();
}
try {
rs = ps.executeQuery();
while(rs.next()){
sqlAdminName = rs.getString(“AdminName”);
sqlAdminPassword = rs.getString(“AdminPassword”);

ad.setAdminName(sqlAdminName);
ad.setAdminName(sqlAdminName);
}

} catch (SQLException e) {
e.printStackTrace();
}
if(ad.getAdminDAO(sqlAdminName).equals(AdminName)){
if((ad.getAdminDAO(sqlAdminPassword).equals(AdminPassword)){
request.getSession().setAttribute(“AdminName”, AdminName);
response.sendRedirect(“/myblog/admin/index.jsp”);
}
else{
String url = “/myblog/admin/loginerror.jsp?error=”+ URLEncoder.encode(“密码错误”, “UTF-8”);
response.sendRedirect(url);
}
}
else {
String url = “/myblog/admin/loginerror.jsp?error=”+ URLEncoder.encode(“用户不存在”, “UTF-8”);
response.sendRedirect(url);
}
}

这是都写在同一个servlet里的,没有出现问题。

response.setCharacterEncoding(“UTF-8”);

AdminName = request.getParameter(“AdminName”);
AdminPassword = request.getParameter(“AdminPassword”);

String sql = “select * from admin where AdminName =? “;

JDBCUtilDao jud = new JDBCUtilDao();
conn = jud.getConnection();
ps = jud.getPrepareStatement(sql);
try {
ps.setString(1, AdminName);
} catch (SQLException e1) {
System.out.println(“发送失败”);
e1.printStackTrace();
}
try {
rs = ps.executeQuery();
while(rs.next()){
sqlAdminName = rs.getString(“AdminName”);
sqlAdminPassword = rs.getString(“AdminPassword”);
}

} catch (SQLException e) {
e.printStackTrace();
}
if(sqlAdminName.equals(AdminName)){
if(sqlAdminPassword.equals(AdminPassword)){
request.getSession().setAttribute(“AdminName”, AdminName);
response.sendRedirect(“/myblog/admin/index.jsp”);
}
else{
String url = “/myblog/admin/loginerror.jsp?error=”+ URLEncoder.encode(“密码错误”, “UTF-8”);
response.sendRedirect(url);
}
}
else {
String url = “/myblog/admin/loginerror.jsp?error=”+ URLEncoder.encode(“用户不存在”, “UTF-8”);
response.sendRedirect(url);
}
}




微信公众号:八一四


上一篇:     |     下一篇: 期末作业——网站



添加新评论