由于数据是写在了文件里,修改和删除不太容易实现,所以只实现了查询,添加,重点是
1.首页要判断用户是否登录
2.练习jsp,EL,JSTL,表达式的使用.
如果没有jsp,我们给浏览器回应一个页面是很困难的,
jsp= HTML+servlet ?
再加上 通过EL 和JSTL 方便的获取共享域数据,于是往浏览器回写一个页面就变得非常简单.
首页
<%--
Created by IntelliJ IDEA.
User: xiaoyan
Date: 2021/4/27
Time: 18:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<c:if test="${username!=null}">
<a href="/xuesheng/ServletFindAllStudent">查询所有学生</a>
<a href="/xuesheng/Id.jsp">按照Id查询学生</a>
<a href="/xuesheng/sunmit1.jsp">增加学生信息</a>
</c:if>
<c:if test="${username==null}">
<a href="login.jsp">您还未登录,点击这里去登录</a>
</c:if>
</body>
</html>
登录界面
<%--
Created by IntelliJ IDEA.
User: xiaoyan
Date: 2021/4/27
Time: 18:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录界面</h1>
<div>
<form action="/xuesheng/Servlet1">
<lable for="username">用户名</lable>
<input type="text" id="username" name="username">
<lable for="password">密码</lable>
<input type="text" id="password" name="password">
<input type="submit"value="提交">
</form>
</div>
</body>
</html>
存储登录信息
package itcast.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
req.getSession().setAttribute("username",username);
req.getSession().setAttribute("password",password);
resp.sendRedirect("/xuesheng/index.jsp");
}
}
返回首页 选择操作
package itcast.servlet;
import itcast.domian.Student;
import itcast.service.ServiceFindAllStudent;
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;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/ServletFindAllStudent")
public class ServletFindAllStudent extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
ServiceFindAllStudent serviceFindAllStudent = new ServiceFindAllStudent();
List<Student> list = serviceFindAllStudent.findAllStudent();
// PrintWriter writer = resp.getWriter();
// for (Student student : list) {
// writer.write(student.toString());
// }
//通过jsp展示页面
req.setAttribute("AllStudentMessage",list);
req.getRequestDispatcher("/view.jsp").forward(req,resp);
}
}
展示页面
<%--
Created by IntelliJ IDEA.
User: xiaoyan
Date: 2021/4/27
Time: 19:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Title</title>
<link href="css/01.css"/>
<style>
td {
text-align: center;
border: 1px solid blue;
}
table {
margin: auto;
}
</style>
</head>
<body>
<div>
<table>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>生日</th>
<th>爱好</th>
</tr>
<c:forEach items="${AllStudentMessage}" var="stu">
<tr>
<td>${stu.number}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.birthday}</td>
<td>
<c:forEach items="${stu.hobbey}" var="s">
${s}
</c:forEach>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
添加
package itcast.servlet;
import itcast.domian.Student;
import itcast.service.AddStudentService;
import org.apache.commons.beanutils.BeanUtils;
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;
import java.util.Map;
//import itcast.service.AddStudentService;
@WebServlet("/ServletAddStudent")
public class ServletAddStudent extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
Map<String, String[]> map = req.getParameterMap();
Student stu = new Student();
try {
BeanUtils.populate(stu,map);
} catch (Exception e) {
e.printStackTrace();
}
AddStudentService addStudentService = new AddStudentService();
Boolean flag = addStudentService.addStudent(stu);
if(flag==true){
resp.getWriter().write("学生信息录入成功");
}
}
}
添加页面
<%--
Created by IntelliJ IDEA.
User: xiaoyan
Date: 2021/4/28
Time: 9:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1 align="center">录入学生信息</h1>
<div>
<form action="/xuesheng/ServletAddStudent">
<lable for="number">学号</lable>
<input type="text" name="number" id="number">
<lable for="name">姓名</lable>
<input type="text" name="name" id="name">
<lable for="age">年龄</lable>
<input type="text" name="age" id="age">
<lable for="birthday">生日</lable>
<input type="date" name="birthday" id="birthday"><br>
<lable for="hobbey">爱好()</lable><br>
<input type="checkbox" name="hobbey" id="hobbey" value="学习"> 学习<br>
<input type="checkbox" name="hobbey" value="唱歌">唱歌<br>
<input type="checkbox" name="hobbey" value="跳舞">跳舞<br>
<input type="checkbox" name="hobbey" value="吃饭">吃饭<br>
<input type="checkbox" name="hobbey" value="睡觉">睡觉<br>
<input type="checkbox" name="hobbey" value="java">java<br>
<input type="submit">
</form>
</div>
</body>
</html>
添加
package itcast.dao;
import itcast.domian.Student;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
public class AddStudentDao {
public Boolean addStudent(Student stu) throws IOException {
//这的路径该怎么写?
//BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt",true));
//BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt",true));
//文件的IO流是相对于启动环境来说的.
//根据的是启动环境 ,它写到了 tomcat 的启动目录下.
//getclasslosder 是写到了相应的字节码下.
String path = AddStudentDao.class.getClassLoader().getResource("Student.txt").getPath();
BufferedWriter bw = new BufferedWriter(new FileWriter(path, Charset.forName("utf-8"),true));
//System.out.println(path);
String str1 = "";
for (String s : stu.getHobbey()) {
str1=str1+s;
}
String str2 = stu.getNumber()+","+stu.getName()+","+stu.getAge()+","+stu.getBirthday()+",";
bw.write(str2+str1);
bw.flush();
bw.newLine();
bw.close();
return true;
}
}