实验四 JavaBean及Servlet使用
一、实验目的
1.熟悉JavaBean的开发过程。
2.掌握Servlet的开发过程。
二、实验学时
2H
三、实验性质
综合性实验
四、实验内容
1.创建一个数据库,在数据库建立用户表,插入几条用户数据。(5分)
2. 编写一个JavaBean“User.java”,它包含属性stuno(String)、password(String)、stuname(String),并编写相应的getter和setter方法。(25分)
3.编写一个登录页面,输入学号和密码,在数据库中进行验证,如果验证通过,则在另一个页面中显示学生的姓名;如果验证不通过,返回登录页面,并提示用户不存在信息。要求数据提交到Servlet中进行处理,使用JavaBean封装学生信息,使用DAO查询数据库。(60分)
1.index1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String str="";
if(request.getParameter("a")!=null){
str="登录失败,请重新登录!";
out.print(str);
}else{
out.print(str);
}
%>
<form action="servlet">
学 号:<input type="text" name="uname"></br>
密 码:<input type="password" name="pwd"></br>
<input type="submit" value="登录">
</form>
</body>
</html>
2.index2
<%@ page import="java.util.ArrayList"%>
<%@page import="Bean.Student"%>
<%@page import="java.*"%>
<%@page import="dao.Dao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int i=0;
String str=request.getParameter("uname");
Dao dao=new Dao();
ArrayList<Student> list=dao.findStudent();
while(true){
if(str.equals(list.get(i).getStuno())){
out.print(list.get(i).getStuname());
break;
}
i++;
}
%>
<a href="index1.jsp">返回登录界面</a>
</body>
</html>
3.package dao
package dao;
import java.awt.List;
import java.sql.*;
import java.util.ArrayList;
import Bean.Student;
public class Dao {
public ArrayList<Student> findStudent() throws Exception{
ArrayList<Student> list=new ArrayList<Student>();
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String path = "E:/study.accdb";
String url = "jdbc:ucanaccess://" + path;
Connection conn = DriverManager.getConnection(url);
String sql="select stuno,password,stuname from 表1";
PreparedStatement pstn = conn.prepareStatement(sql);
ResultSet rs = pstn.executeQuery();
while(rs.next()) {
Student stu=new Student();
stu.setStuno(rs.getString("stuno"));
stu.setPassword(rs.getString("password"));
stu.setStuname(rs.getString("stuname"));
list.add(stu);
}
rs.close();
conn.close();
return list;
}
}
4.Servlet01
package Servlet01;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Bean.Student;
import dao.Dao;
@WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
PrintWriter out=resp.getWriter();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Dao dao=new Dao();
ArrayList<Student> list;
try {
list = dao.findStudent();
String str1=request.getParameter("uname");
String str2=request.getParameter("pwd");
for(int i=0;i<list.size();i++) {
if((str1.equals(list.get(i).getStuno()))&&str2.equals(list.get(i).getPassword())) {
request.getRequestDispatcher("index2.jsp").forward(request, response);
}
}
response.sendRedirect("index1.jsp?a=1");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.package Bean
package Bean;
public class Student {
private String stuno;
private String password;
private String stuname;
public String getStuno() {
return stuno;
}
public Student(String stuno, String password, String stuname) {
super();
this.stuno = stuno;
this.password = password;
this.stuname = stuname;
}
public Student() {
super();
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
}
五、实验总结
请书写你对本次实验有哪些实质性的收获和体会,以及对本次实验有何良好的建议?(10分)