- 登录页面
Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>登录页面</title> </head> <body background="login.jpg"> <center> <form action="Check" method="post"> <table bgcolor="#F0FFFF" width="300"height="200"> <hr width="75%" size="3" color="lightgreen"> <tr align="center"> <td colspan="6" align="center">用户登录 <hr width="75%"color="white" align="center"> </td> </tr> <center> <tr ><td colspan="4" align="right">账号</td><td ><input type="text"name="user"></td></tr> <tr><td colspan="4" align="right">密码</td><td colspan="2"><input type="password"name="password"></td></tr> </center> <tr align="center"> <td colspan="10"> <input type="submit"value="登 录"> <input type="reset"value="取 消 "> </td> </tr> </table> </form> </center> </body> </html>
2.登录信息检查页面
Check.java
package ch04; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Check extends HttpServlet { /** * Constructor of the object. */ public Check() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ /* public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } */ /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("user");//获取用户名 String password=request.getParameter("password");//获取密码 if(("client".equals(name))&&"123456".equals(password)){//设置用户名和密码 //如果用户名和密码相对应,则跳转到学生体质管理页面 RequestDispatcher rd=request.getRequestDispatcher("main.jsp"); rd.forward(request, response); }else{//账户名或密码不正确则跳转登录失败页面 RequestDispatcher rd=request.getRequestDispatcher("Faile.jsp"); rd.forward(request, response); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
Servlet配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>Check</servlet-name> <servlet-class>ch04.Check</servlet-class> </servlet> <servlet-mapping> <servlet-name>Check</servlet-name> <url-pattern>/Check</url-pattern> </servlet-mapping> </web-app>
3.登录成功进入学生身体体质信息管理系统
4.登陆失败,显示重新登录
Faile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>失败页面</title> </head> <body> 登陆失败! <!--获取用户名 --> <%String Name=request.getParameter("user"); %> <!--重新跳转到登陆页面 --> <br><a href="Login.jsp">请重新登录,<%=Name %>用户! </body> </html>
5.设计程序,实现利用提交页面提交要添加的学生信息,然后进入添加处理程序实现将信息添加到数据库。
insert.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%> <html> <head> <title>添加记录的提交页面</title> </head> <body background="right.jpg"> 请输入新插入学生的信息<br> <hr width="100%"size="3"> <form action="insert2.jsp"method="post"> <table border="0"width="238"height="252"> <tr><td>学号</td><td><input type="text"name="id"></td></tr> <tr><td>姓名</td><td><input type="text"name="name"></td></tr> <tr><td>性别</td><td><input type="text"name="sex"></td></tr> <tr><td>年龄</td><td><input type="text"name="age"></td></tr> <tr><td>体重</td><td><input type="text"name="weight"></td></tr> <tr><td>身高</td><td><input type="text"name="hight"></td></tr> <tr align="center"> <td colspan="2"> <input type="submit"value="提交"> <input type="reset"value="取消"> </td> </tr> </table> </form> </body> </html>
insert2.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>添加记录的处理页面</title> </head> <body background="right.jpg"> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="students"; String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=utf-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); //注册驱动并建立数据库连接 //形成sql的插入语句 String sql="insert into stu(id,name,sex,age,weight,hight)value(?,?,?,?,?,?)"; //利用连接对象建立PreparedStatement对象 PreparedStatement pstmt=conn.prepareStatement(sql); //避免出现乱码 request.setCharacterEncoding("utf-8"); //得到的学号是字符串啊,用int得包装类Integer的parseInt()方法,将这个值转换为整形 int id=Integer.parseInt(request.getParameter("id")); //获取学生姓名、性别,姓名、性别为字符串类型不用转换类型 String name=request.getParameter("name"); String sex=request.getParameter("sex"); //年龄,体重、身高强制转换类型 int age=Integer.parseInt(request.getParameter("age")); float weight=Float.parseFloat(request.getParameter("weight")); float hight=Float.parseFloat(request.getParameter("hight")); //处理执行结果 //插入学号,姓名,,,身高 pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setString(3, sex); pstmt.setInt(4, age); pstmt.setFloat(5, weight); pstmt.setFloat(6, hight); //调用PreparedStatement对象,执行executeUpdate()方法 int n=pstmt.executeUpdate(); //根据executeUpdate()方法返回的整数,判断是否执行成功,如果大于0表示成功,否则执行失败 if(n==1){%>数据插入操作成功!<br> <% }else{%>数据插入操作失败!<br><%} //关闭资源 if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
6.采用PreparedStatement的对象实现记录的查询记录,要求查询表中所有学生信息并显示在网页上。
select.jsp
<%@ page language="java" pageEncoding="utf-8"%> <html> <head> <title>查询记录提交的页面</title> </head> <body background="right.jpg"> 请选择查询条件 <hr width="100%"size="3"> <form action="select2.jsp"method="post"> 性别:男<input type="radio"value="男"name="sex"checked="checked"> 女<input type="radio"value="女"name="sex"><br><br> 体重范围:<p> 最小<input type="text"value="0"name="w1"><br><br> 最大<input type="text"value="150"name="w2"> </p> <input type="submit"value="提交"> <input type="reset"value="取消"> </form> </body> </html>
select2.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>查询记录处理的页面</title> </head> <body background="right.jpg"> <center> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="students"; String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=utf-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); request.setCharacterEncoding("utf-8"); String sex=request.getParameter("sex"); float weight1=Float.parseFloat(request.getParameter("w1")); float weight2=Float.parseFloat(request.getParameter("w2")); String sql="select * from stu where sex=? and weight>=? and weight <=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1, sex); pstmt.setFloat(2, weight1); pstmt.setFloat(3, weight2); ResultSet rs=pstmt.executeQuery(); rs.last(); %> <font size="5"color="blue"> <%=rs.getRow() %>人 </font> <table border="2"bgcolor="ccceee"width="650"> <tr><td>记录条数</td><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>体重</td><td>身高</td></tr> <%rs.beforeFirst(); while(rs.next()){ %> <tr align="center"> <td><%=rs.getRow() %></td> <td><%=rs.getString("id") %></td> <td><%=rs.getString("name") %></td> <td><%=rs.getString("sex") %></td> <td><%=rs.getString("age") %></td> <td><%=rs.getString("weight") %></td> <td><%=rs.getString("hight") %></td> </tr> <% } %> </table> </center> <% if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
7.设计一个提交页面,将要查询的条件通过该页面提交给查询处理页面,在该页面中获取所提交的信息,并将这些信息作为SQL语句的参数信息,查询结束后,显示出所有满足条件的记录。
list.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>列出全部记录页面</title> </head> <body background="right.jpg"> <center> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="students"; String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); String sql="select * from stu"; //利用连接对象建立PreparedStatement对象 PreparedStatement pstmt=conn.prepareStatement(sql); //调用PreparedStatement对象的executeQuery()方法,并返回ResultSet对象 ResultSet rs=pstmt.executeQuery(); //移至最后一条记录 rs.last(); %> 你要查询的学生数据表中公有 <font size="5"color="blue"> <!--获取行 --> <%=rs.getRow() %>人 </font> <table border="2"bgcolor="ccceee"width="650"> <tr bgcolor="CCCCCC"align="center"> <td>记录条数</td><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>体重</td><td>身高</td> </tr> <%rs.beforeFirst(); //移至第一条记录之前 while(rs.next()){ %> <tr align="center"> <!-- 获取信息 --> <td><%=rs.getRow() %></td> <td><%=rs.getString("id") %></td> <td><%=rs.getString("name") %></td> <td><%=rs.getString("sex") %></td> <td><%=rs.getString("age") %></td> <td><%=rs.getString("weight") %></td> <td><%=rs.getString("hight") %></td> </tr> <%} %> </table> </center> <!-- 释放资源 --> <%if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
8.对数据库students表中的数据表满足条件的记录进行修改。
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>修改记录提交的页面</title> </head> <body background="right.jpg"> 请选择修改记录所满足的条件 <hr width="100%"size="3"> <form action="update2.jsp"method="post"><br> 姓名:<input type="text"name="name"><br><br> 性别:男<input type="radio"value="男"name="sex"> 女<input type="radio"value="女"name="sex"><br><br> <input type="submit"value="提 交"> <input type="reset"value="取消"> </form> </body> </html>
update2.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>读取、显示修改页面</title> </head> <body background="right.jpg"> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="students"; String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=utf-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); request.setCharacterEncoding("utf-8"); //获取姓名、性别 String name=request.getParameter("name"); String sex=request.getParameter("sex"); //存入session容器 session.setAttribute("name", name); session.setAttribute("sex", sex); String sql="select * from stu where name=? and sex=?"; PreparedStatement pstmt=conn.prepareStatement(sql); //获取姓名,性别 pstmt.setString(1,name); pstmt.setString(2,sex); //调用PreparedStatement对象的executeQuery()方法,并返回ResultSet对象 ResultSet rs=pstmt.executeQuery(); if(rs.next()){ //显示修改之前的结果 int id=rs.getInt("id"); String name2=rs.getString("name"); String sex2=rs.getString("sex"); int age=rs.getInt("age"); float weight=rs.getFloat("weight"); float hight=rs.getFloat("hight"); if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> <!-- 显示执行结果update3 --> <form action="update3.jsp"method="post"> <table border="0"width="238"height="252"> <tr><td>学号</td><td><input type="text"name="id"value=<%=id %>></td></tr> <tr><td>姓名</td><td><input type="text"name="name2"value="<%=name2 %>"></td></tr> <tr><td>性别</td><td><input type="text"name="sex2"value="<%=sex2 %>"></td></tr> <tr><td>年龄</td><td><input type="text"name="age"value="<%=age %>"></td></tr> <tr><td>体重</td><td><input type="text"name="weight"value="<%=weight %>"></td></tr> <tr><td>身高</td><td><input type="text"name="hight"value="<%=hight %>"></td></tr> <tr align="center"> <td colspan="2"> <input type="submit"value="提 交"> <input type="reset"value="取 消"> </td> </tr> </table> </form> <%} else { %> 没有找到合适条件的记录!!<% if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} } %> </body> </html>
update3.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>处理修改页面</title> </head> <body background="right.jpg"> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="123456"; String dbName="students"; String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=utf-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); String sql="update stu set id=?,name=?,sex=?,age=?,weight=?,hight=? where name=? and sex=?"; PreparedStatement pstmt=conn.prepareStatement(sql); request.setCharacterEncoding("utf-8"); int id=Integer.parseInt(request.getParameter("id")); String name2=request.getParameter("name2"); String sex2=request.getParameter("sex2"); int age=Integer.parseInt(request.getParameter("age")); float weight=Float.parseFloat(request.getParameter("weight")); float hight=Float.parseFloat(request.getParameter("hight")); //从session容器中取出 String name=(String)session.getAttribute("name"); String sex=(String)session.getAttribute("sex"); pstmt.setInt(1,id); pstmt.setString(2,name2); pstmt.setString(3,sex2); pstmt.setInt(4,age); pstmt.setFloat(5,weight); pstmt.setFloat(6,hight); pstmt.setString(7,name); pstmt.setString(8,sex); int n=pstmt.executeUpdate(); if(n>=1){%> 重写数据操作成功!<br> <%} else{ %> 重写数据操作失败!<%=n %><br> <%} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
9.对数据库students表中的数据表,删除满足条件的记录。
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>删除记录提交的页面</title> </head> <body background="right.jpg"> 请选择删除数据条件 <hr width="100%"size="3"> <form action="delete2.jsp"method="post"> 学号:<input type="text"name="id"><br><br> <input type="submit"value="提 交"> <input type="reset"value="取 消"> </form> </body> </html>
delete2.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>删除记录处理的页面</title> </head> <body background="right.jpg"> <% String driverName="com.mysql.jdbc.Driver";//驱动程序名 String userName="root";//数据库用户名 String userPwd="123456";//数据库密码 String dbName="students";//数据库名 String url1="jdbc:mysql://localhost:3306/students"; String url2="?user=root&password=123456"; String url3="&useUnicode=true&characterEncoding=utf-8"; String url=url1+url2+url3; Class.forName(driverName);//加载并注册驱动程序 Connection conn=DriverManager.getConnection(url);//获取数据库连接对象conn request.setCharacterEncoding("utf-8");//避免出现乱码 int id=Integer.parseInt(request.getParameter("id"));//获取学号并强制转换类型 String sql="delete from stu where id="+id;//sql删除语句 //利用连接对象建立PreparedStatement对象pstmt PreparedStatement pstmt=conn.prepareStatement(sql); //执行已发送的预编译的sql 并返回n的值 int n=pstmt.executeUpdate(); if(n==1){%> 数据删除操作成功!<br> <%} else{ %> 数据删除操作失败!<br> <%} //释放资源 if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
10.主页面设计框架
main.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%> <html> <head> <title>学生身体体质信息管理系统</title> </head> <frameset rows="88,*"> <frame src="main_title.jsp" scrolling="no"> <frameset cols="140,*"> <frame src="main_left.jsp" scrolling="no"> <frame src="main_right.jsp"name="right" scrolling="no"> </frameset> </frameset> </html>
mian_title.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%> <html> <head> <title>页面标题</title> </head> <body background="title.jpg"> <center> <h1>学生身体体质信息管理系统</h1> </center> </body> </html>
main_left.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%> <html> <head> <title>菜单页面</title> </head> <body background="left.jpg"> <br><br><br><br><br><br><br> <p><a href="list.jsp"target="right">列出全部学生</a></p> <p><a href="select.jsp"target="right">按条件查询学生</a></p> <p><a href="insert.jsp"target="right">新添加学生</a></p> <p><a href="delete.jsp"target="right">按条件删除学生</a></p> <p><a href="update.jsp"target="right">按条件修改学生</a></p> </body> </html>
mian_right.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%> <html> <head> <title>信息显示页面</title> </head> <body background="right1.jpg"> </body> </html>