在上一篇博客中,我们已经搭建好了初始的环境啦!本篇文章我将会重点讲述开发过程中要注意的细节问题!
登录功能部分:
Jsp部分代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录页面</title>
</head>
<body>
<h3>欢迎来到登录界面</h3>
<form action="${pageContext.request.contextPath}/loginServlet" method="post">
<table>
<tr>
<td>账号:</td>
<td>
<input type="text" name="username" id="username" />
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password" id="password" />
</td>
</tr>
</table>
<input type="submit" value="登录" />
</form>
</body>
</html>
Servlet层的代码:
package servlet;
import service.StudentService;
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 java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
StudentService studentService = new StudentService();
//获取用户输入的账号和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//传入service层进行账号和密码的校验
if (studentService.loginService(username,password)) {
//如果账号密码无误的话就直接进入我们操作的页面
response.sendRedirect(request.getContextPath()+ "/findAllServlet");
} else {
//如果账号密码有误的话就显示到错误提示
response.getWriter().println("<script language='javascript'>alert('username or password is wrong!');</script>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
Service层和DAO层的代码:
/** DAO层:
* 根据用户输入的账号和密码进行登录的方法
* @param username 用户输入的账号
* @param password 用户输入的密码
* @return 找到则登录成功/true、否则false
*/
public boolean login(String username,String password){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from stu where name = ? and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,password);
rs = pstmt.executeQuery();
while (rs.next()) {
String current_username = rs.getString("name");
String current_password = rs.getString("password");
//如果账号和密码匹配正确的话就直接返回true
if (username.equals(current_username) && password.equals(current_password)) {
return true;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭连接资源
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
//Service层
public boolean loginService(String username,String password){
return studentDao.login(username,password);
}
由于登录功能是最基础的功能,这里相信大家已经很熟悉登录操作了,这里就不做具体介绍了。
* 查询所有数据库信息展示到Jsp页面:
DAO层和Service层代码:
/** DAO层代码
* @return 封装好数据的student集合对象
*/
public List<Student> findAll(){
//创建一个用于封装对象的list集合
List list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from stu";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()){
Student student = new Student();
student.setSno(rs.getInt("sno"));
student.setName(rs.getString("name"));
student.setPassword(rs.getString("password"));
student.setEmail(rs.getString("email"));
student.setQq(rs.getString("qq"));
student.setAge(rs.getInt("age"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs,pstmt,conn);
}
return list;
}
//Service层代码
StudentDao studentDao = new StudentDao();
/**
*
* @return 返回一个封装好student数据的集合对象
*/
public List<Student> findAllService(){
return studentDao.findAll();
}
JSP文件通过表格的方式显示具体的数据库信息内容,这里主要是采用了JSTL表达式的foreach语句,以及EL表达式:
<table border="1px" cellspacing="0px" >
<tr>
<th><input type="checkbox" onclick="sel('choice')">全选</th>
<th>序号</th>
<th>姓名</th>
<th>密码</th>
<th>邮箱</th>
<th>qq</th>
<th>年龄</th>
<th>修改</th>
</tr>
<c:forEach items="${requestScope.students}" var="student" varStatus="s">
<tr>
<%--
这里我们需要进行选中删除的操作,主要是通过表单提交的方式来进行删除
当我们选中这一行数据之后,其就会同时对应一个sno的value值
可以通过getParameterVlues的方式来获取每一个value值
最后还是通过点击删除选择按钮的onclick来进行submit提交操作
--%>
<td><input type="checkbox" name="choice" value="${student.sno}"></td>
<td>${s.count}</td>
<td>${student.name}</td>
<td>${student.password}</td>
<td>${student.email}</td>
<td>${student.qq}</td>
<td>${student.age}</td>
<td>
<form id="form2" action="${pageContext.request.contextPath}/deleteServlet?id=${student.sno}" method="post">
<input type="submit" name="delete" value="删除">
</form>
<%--<a href="${pageContext.request.contextPath}/findByIdServlet?id=${student.sno}">修改</a>--%>
<form id="form2" action="${pageContext.request.contextPath}/findByIdServlet?id=${student.sno}" method="post">
<input type="submit" name="alter" value="修改">
</form>
</td>
</tr>
</c:forEach>
</form>
</table>
* Servlet层的代码:
package servlet;
import service.StudentService;
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 java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
StudentService studentService = new StudentService();
//获取用户输入的账号和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//传入service层进行账号和密码的校验
if (studentService.loginService(username,password)) {
//如果账号密码无误的话就直接进入我们操作的页面
response.sendRedirect(request.getContextPath()+ "/findAllServlet");
} else {
//如果账号密码有误的话就显示到错误提示
response.getWriter().println("<script language='javascript'>alert('username or password is wrong!');</script>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
注意事项:
1、关于form表单的action提交的路径,我们最好就是加上虚拟路径的读取方式,使用${pageContext.request.contextPath}这种EL表达式来直接获取虚拟路径,后面拼接Servlet注解文件名即可。
2、在DAO层我们将查询到的所有数据库的信息都封装好了在Student对象中去了,并且在DAO层已经把这个对象放入到了List集合中去直接返回,当我们调用这个DAO层的方法的时候即可获得一个封装好Student对象的List集合,然后我们再将封装好数据的Student对象直接保存到Servlet层的list中去,再去设置request.setAttriubte来进行数据共享,再使用getRequestDispatcher重新发送到Jsp页面层。此时Jsp页面就有一个list集合,里面包含了所有的Student数据库的数据内容了。在Jsp层我们需要用foreach语句,对存在于request域的list集合进行遍历操作,逐个取出student对象中的sno、name、password.......显示到我们的table表格中去。
3、要切记,一般来说数据处理的逻辑是:Jsp层表单(提交到)→ servlet层 → Service层 → Dao层
处理完了之后:Dao层 → Service层 → servlet层 → Jsp层表单显示
获取到数据库的信息之后我们的页面也将数据库的信息展示到页面来啦!