📄 login.java
字号:
package data;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class Login
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/furniture";
String user = "root";
String password = "123123";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
boolean a;
//读取提交的表单内容
String aId = request.getParameter("aId");
String pwd = request.getParameter("aPassword");
String career = request.getParameter("select");
HttpSession session=request.getSession();
session.setAttribute("user",aId);
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "select * from " +career+ " where aId=\"" + aId +
"\" and aPassword=\"" + pwd + "\"";
rs = stmt.executeQuery(sql);
a = rs.next();
if ((a == true)&&(career.compareTo("admin") == 0)) {
response.sendRedirect("Admin.jsp");
}else
{
out.println("<html>");
out.println("<head><title>Login</title></head>");
out.print("</p>");
out.print("<p> </p>");
out.print("<p>对不起!您的输入有误,请重新填写!</p>");
out.print(
"<p align='center'><a href=\'Login.jsp\'>返回</a></p>");
out.println("</body>");
out.println("</html>");
}
out.close();
}
catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序");
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -