📄 login_confirm.java
字号:
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 void goo(HttpServletRequest req, HttpServletResponse res,String kind)
throws ServletException,IOException
{
//转向学生功能页面
if(kind.equals("student")) {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/student.jsp");
rd.forward(req, res);}
//转向教师功能页面
if(kind.equals("teacher")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/teacher.jsp");
rd.forward(req, res);}
//转向管理员功能页面
if(kind.equals("admin")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(req, res);}
}
//根据用户的级别和输入的用户名,查询对应的密码
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);
}
// 响应get请求
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String action = action = req.getParameter("action");
// 检查用户请求参数中是否包含退出参数,如果包含则转向系统的首页面
if ("logout".equalsIgnoreCase(action)) {
HttpSession session=req.getSession(true);
session.invalidate();
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
rd.forward(req, res);
} }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -