protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
viewService vs= new viewService();
ArrayList<BeanClass> al = null;
try {
al = vs.view(request, response);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(BeanClass s : al)
{
System.out.println(s.getUsername() + " | "+ s.getPassword() + " | "+s.getAge()+"|"+s.getEmail());
}
}
另一个班级
public class viewService {
public ArrayList<BeanClass> view(HttpServletRequest request,
HttpServletResponse response) throws SQLException, ClassNotFoundException, IOException
{
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if (session != null) {
String username = (String) session.getAttribute("name");
out.print("Hello, " + username + " Welcome to Profile");
ArrayList<BeanClass> al = new ArrayList<BeanClass>();
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/servlet","root","root");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from registration where Username = name");
while(rs.next())
{
BeanClass bbc= new BeanClass();
bbc.setUsername(rs.getString(1));
bbc.setPassword(rs.getString(2));
bbc.setAge(rs.getString(3));
bbc.setEmail(rs.getString(4));
al.add(bbc);
}
return al;
}
else{out.print("please login first");}
return null;
}}