JSP+Servlet做不同用户登录,登录Servlet出现空白页,无法跳转到指定页面
前台login.jsp代码
本学期的JavaEE课程设计,不知道哪里出错了,请各位前辈帮忙看看,万分感谢
登录名: | |
密码: | |
管理员 | 车位用户 |
如果没注册点击这里注册!
Servlet类代码
package servlet;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Admin;
import model.User;
import db.DB;
public class MainServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.print("跳到mainServlet");
String adminuser = request.getParameter("AdminUser");
String username=request.getParameter("username");
//
String pwd=request.getParameter("pwd");
//
DB db=new DB();
HttpSession session=request.getSession();
if(adminuser=="1"){
Admin admin= (Admin) session.getAttribute("admin");
//
if(admin==null){
admin=db.checkAdmin(username, pwd);
}
//
session.setAttribute("admin", admin);
if(admin!=null){
response.sendRedirect("index.jsp");
}else if(admin==null){
//
response.sendRedirect("login.jsp");
}
}else if(adminuser=="2"){
User user= (User) session.getAttribute("user");
//
if(user==null){
user=db.checkUser(username, pwd);
}
//
session.setAttribute("user", user);
if(user!=null){
response.sendRedirect("main.jsp");
}else if(user==null){
//
response.sendRedirect("login.jsp");
}
}
}
public void init() throws ServletException {
// Put your code here
}
}
数据库连接 DB类代码
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import model.Admin;
import model.User;
public class DB {
Connection ct;
PreparedStatement pstmt;
// 连接数据库
public DB(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;databaseName=JSP","sa","123456");
} catch (Exception e) {
e.printStackTrace();
}
}
public Admin checkAdmin(String LogId,String AdminWord){
try{
pstmt=ct.prepareStatement("select * from [Admin] where LogId=? and AdminWord=?");
pstmt.setString(1, LogId);
pstmt.setString(2, AdminWord);
ResultSet rs=pstmt.executeQuery();
Admin admin=new Admin();
while(rs.next()){
admin.setAdminId(rs.getInt(1));
admin.setLogId(rs.getString(2));
admin.setAdminWord(rs.getString(3));
return admin;
}
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
// 根据用户名和密码查询用户
public User checkUser(String username,String password){
try{
pstmt=ct.prepareStatement("select * from [userTable] where username=? and password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs=pstmt.executeQuery();
User user=new User();
while(rs.next()){
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
return user;
}
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}