package Student;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import Student.student;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/tset";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String sql = "select * from student ";
ResultSet rs = stmt.executeQuery(sql);
List<student> list = new ArrayList<student>();
while(rs.next()){
student stu=new student();
stu.setUser(rs.getString("user"));
stu.setPass(rs.getString("pass"));
stu.setNick(rs.getString("nick"));
list.add(stu);
}
request.setAttribute("list", list);
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("output.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2017/11/7
Time: 16:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="Student.student"%>
欢迎
<%
List list=(List)request.getAttribute("list");
String nickname="sss";
if (list == null) {
nickname="不存在这人";
}
else {
for (student student : list) {
if (request.getParameter("username").equals(student.getUser()) && request.getParameter("password").equals(student.getPass()) ) {
nickname = student.getNick();
}
else{
nickname="密码错误";
}
}
}
%>
<%=nickname%>