📄 login_confirm.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBeans.SQLBean;
public class login_confirm extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String message=null;
String id=null;
id=req.getParameter("id").toString();
HttpSession session=req.getSession(true);
session.setAttribute("id",String.valueOf(id));
String password=null;
password=req.getParameter("password").toString();
String kind =null;
kind=req.getParameter("kind").toString();
String temp=getPassword(req,res,id,kind);
if( password.equals(temp))
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("studba")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(req, res);}
}
public String getPassword(HttpServletRequest req, HttpServletResponse res,
String id,String kind)
throws ServletException, IOException {
/* try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.err.println("执行查询有错误:"+e.getMessage() );
System.out.print("执行查询有错误:"+e.getMessage()); //输出到客户端
}
Connection conn = null;
Statement st = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/studb","root","125125");
st = conn.createStatement();
}catch(SQLException ex){
System.err.println("执行查询有错误:"+ex.getMessage() );
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端
}
String sql="select PASSWORD from "+kind+" where ID='"+id+"'";
String pw="";
try{
ResultSet rs = st.executeQuery(sql);
if(rs.next())
pw = rs.getString("PASSWORD");
}catch(Exception ec){
System.out.print(ec.toString());
}*/
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);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String 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 + -