jsp+servlet+jdbc增删改查功能实现(二)

默认WebContent目录下创建jsp页面

1.putin.jsp(输入信息页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>学生信息输入</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript"">
    function validate()
    {
    var id=document.forms[0].id.value;
    var name=document.forms[0].name.value;
    var age=document.forms[0].age.value;
    var major=document.forms[0].major.value;
    if(id<=0){
    	alert("学号不能为空,请输入学号!");
    	return false;
    }
    else if(name.length<=0){
    	alert("姓名不能为空,请输入姓名!");
    	return false;
    }
    else if(age<=0){
    	alert("请输入合法年龄!");
    	return false;
    }

        else if(major.length<=0){
    	alert("专业不能为空,请输入所学专业!");
    	return false;
    }

    else{
    	return true;
    }
    	//document.getElementById("form").submit();
    }
    </script>
  </head>
  
  <body>
     <br>
  <center>
  <h2>学生信息输入</h2><hr>
 <form action="AllServlet" method="post" id="form" onSubmit="return validate()" >
 <input type="hidden" name="methodName" value="0"/>
<h4>  学号:<input type="text" name="id" class="{required:true}" title="学号必须为数字"></input><br></h4>
<h4>  姓名:<input type="text" name="name"title="姓名不能为空"></input><br></h4>
<h4>  年龄:<input type="text" name="age"title="年龄必须为数字"></input><br></h4>
<h4>  性别:<input type="radio" name="gender" value="男">男
            <input type="radio" name="gender" value="女">女<br></h4>
<h4>  专业:<input type="text" name="major"title="专业不能为空"></input><br></h4>
 <input type="submit" value="提交"/>
  </form>
  <br>
  <a href="AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>">查看已输入信息</a>
  </center>
  </body>
</html>

2.layout.jsp(数据列表分页显示,可删除,查询,修改)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="bean.StudentInfo" %>
<%@ page import="bean.Page" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>学生信息</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 <script type="text/javascript">
    function confirmdialog(){
       if(window.confirm("您确定要删除此条信息?")){
       return true;
       }
       else{
     //  alert("取消删除!");
       return false;
       }      
    }
 </script>
  </head>
  
  <body>
    
    <br>
    <h1>学生信息</h1>  <br>  <hr>    
  
     <br>
  <h3>全部学生信息如下</h3>
   <table width="510" border="100" cellSpacing=1 style="border: 1pt dashed ; font-size: 15pt;" height="31">
    <tr>
    <td>学号</td>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    <td>专业</td>
    </tr>
    <%
     response.setCharacterEncoding("UTF-8");
     request.setCharacterEncoding("UTF-8");
     Page pager=(Page)request.getAttribute("pager");
     List<StudentInfo> subResult=(List<StudentInfo>)request.getAttribute("subResult");
       if(!subResult.isEmpty()){
       for(int i=0;i<subResult.size();i++){
            StudentInfo st=subResult.get(i);
            out.print("<tr>");
            out.print("<td>"+st.getId()+"</td>");
            out.print("<td>"+st.getName()+"</td>");
            out.print("<td>"+st.getAge()+"</td>");
            out.print("<td>"+st.getGender()+"</td>");
            out.print("<td>"+st.getMajor()+"</td>"); 
            
     %>
     <td><a href="AllServlet?id=<%=st.getId() %>&methodName=<%=2 %>" οnclick="return confirmdialog()">删除</a></td>
     
     <td><a href="AllServlet?id=<%=st.getId() %>&name=<%="" %>&methodName=<%=4 %>">修改</a></td>
    <% 
    out.print("</tr>");
         }
       }
     %>
     </table>
     <span><font size="2">总<%=pager.getTotalRecord() %>条记录|总<%=pager.getTotalPage() %>页
     |当前<%=pager.getCurrentPage()+1 %>页|每页<%=pager.getPageSize() %>条|
     <%
        int last=pager.getCurrentRecord()-pager.getPageSize();
        int next=pager.getCurrentRecord()+pager.getPageSize();
        int currentRecord;
        if(last<0){
        out.println("首页|");
        }
        else{
        out.print("<a href='AllServlet?currentRecord="+last+"&methodName=1'>上一页</a>|");
        }
        if(next>=pager.getTotalRecord()){
          out.println("尾页|");
        }
        else{
          out.print("<a href='AllServlet?currentRecord="+next+"&methodName=1'>下一页</a>|");
        }
      %>
      </font>
      </span>
     <br>
    <form action="AllServlet" method="post">
    <input type="hidden" name="methodName" value="5"/>
      <h3>按学号姓名查询:</h3>
                              学号:<input type="text" name="id"  value="" title="学号必须为数字" ></input>
                              姓名:<input type="text" name="name" value="" title=""></input>
      <input type="submit" value="查询" />
    </form>
 <br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3> 
<br>
  </body>
</html>

3.update1.jsp(修改信息页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="bean.StudentInfo" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>学生信息修改</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <br>
    <h2>学生信息</h2>  <hr>    
    <br> 
  <h3>要修改的学生信息如下</h3>
   <table width="496" border="100" cellSpacing=1 style="border: 1pt dashed ; font-size: 15pt;" height="31">
    <tr>
    <td>学号</td>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    <td>专业</td>
    </tr>
    <%
    int id=0;
       ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
        result=(ArrayList<StudentInfo>)request.getAttribute("result");
       if(!result.isEmpty()){
       for(int i=0;i<result.size();i++){
            StudentInfo st=result.get(i);
            id=st.getId();
            out.print("<tr>");
            out.print("<td>"+st.getId()+"</td>");
            out.print("<td>"+st.getName()+"</td>");
            out.print("<td>"+st.getAge()+"</td>");
            out.print("<td>"+st.getGender()+"</td>");
            out.print("<td>"+st.getMajor()+"</td>"); 
            out.print("</tr>");
            }
            }
     %>
    </table>
    <h3>将学生信息更改为:</h3>
 <form action="AllServlet" method="post" >
 <input type="hidden" name="methodName" value="3"/>
<h4> 学号:<input type="text" name="id"value="<%=id %>" title="学号不能改变"></input><br></h4>
<h4>  姓名:<input type="text" name="name"title="姓名不能为空"></input><br></h4>
<h4>  年龄:<input type="text" name="age"title="年龄不能为空"></input><br></h4>
<h4>  性别:<input type="radio" name="gender" value="男">男
       <input type="radio" name="gender" value="女">女<br></h4>
<h4>  专业:<input type="text" name="major"title="专业不能为空"></input><br></h4>
 <input type="submit" value="修改"/>
  </form>

 <br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<h3><a href=AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h3>
  </body>
</html>

4.update.jsp(修改成功后显示)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="bean.StudentInfo" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>修改成功</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
      <br>
    
    <h3>修改后的信息为:</h3>
    
    <hr>
    <br>
    <br>
<table width="450" border="100" cellSpacing=1 style="font-size:15pt;border:dashed 1pt">
    <tr>
    <td>学号</td>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    <td>专业</td>
    </tr>
    <%
     response.setCharacterEncoding("UTF-8");
     request.setCharacterEncoding("UTF-8");
       ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
       result=(ArrayList<StudentInfo>)request.getAttribute("result");
       if(!result.isEmpty()){
       for(int i=0;i<result.size();i++){
            StudentInfo st=result.get(i);
            out.print("<tr>");
            out.print("<td>"+st.getId()+"</td>");
            out.print("<td>"+st.getName()+"</td>");
            out.print("<td>"+st.getAge()+"</td>");
            out.print("<td>"+st.getGender()+"</td>");
            out.print("<td>"+st.getMajor()+"</td>"); 
            out.print("</tr>");
              }
       }
     %>
          </table>
      <br>
      <br>
      <h3><a href=putin.jsp>返回信息输入页面</a></h3>
      <h3><a href=AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h3>
  </body>
</html>

5.delete.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>删除界面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    out.print("<center><br><br><h3>删除成功!</h3></center>");
     %>
      <br>
 <br>
     <center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
  </body>
</html>

6.idnameselect.jsp(按学号姓名查询显示页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="bean.StudentInfo" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>按学号查询</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <br>
        <h3>符合条件的学生信息</h3><hr> 
    <br>
     <table width="450" border="100" cellSpacing=1 style="font-size:15pt;border:dashed 1pt">
    <tr>
    <td>学号</td>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    <td>专业</td>
    </tr>
    <%
     response.setCharacterEncoding("UTF-8");
     request.setCharacterEncoding("UTF-8");
     
       ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();    
       result=(ArrayList<StudentInfo>)request.getAttribute("result");
       
       if(!result.isEmpty()){
       for(int i=0;i<result.size();i++){
            StudentInfo st=result.get(i);
            out.print("<tr>");
            out.print("<td>"+st.getId()+"</td>");
            out.print("<td>"+st.getName()+"</td>");
            out.print("<td>"+st.getAge()+"</td>");
            out.print("<td>"+st.getGender()+"</td>");
            out.print("<td>"+st.getMajor()+"</td>"); 
          
     %>
     <tr>
     <td><a href="AllServlet?id=<%=st.getId() %>&&methodName=<%=2 %>">删除</a></td>
     
     <td><a href="AllServlet?id=<%=st.getId() %>&&methodName=<%=4 %>">修改</a></td>
       </tr>
    <% 
    out.print("</tr>");
            }
       }
 
     %>
      </table>
        <br>
      <br>
       <h4><a href=AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h4>
  </body>
</html>

7.selectnothing.jsp(查无此人反馈页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>查无此人</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <%
    out.print("<center><br><br><h3>没有符合此条件的信息!</h3></center>");
     %>
      <br>
 <br>
     <center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1 %>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
  </body>
</html>

8.web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>CRUD</display-name>
  <description>JSP SERVLET JDBC 增删改查范例</description>
  <welcome-file-list>
    <welcome-file>putin.jsp</welcome-file>
  </welcome-file-list>
  <servlet>  
   <servlet-name>AllServlet</servlet-name>     
   <servlet-class>dbservlet.AllServlet</servlet-class>   
  </servlet>
  <servlet-mapping>  
    <servlet-name>AllServlet</servlet-name>  
    <url-pattern>/AllServlet</url-pattern>  
  </servlet-mapping>  
</web-app>


  • 12
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值