专栏首页浪淘沙使用Jsp编写网页并实现数据库的增删改查

使用Jsp编写网页并实现数据库的增删改查

首先jsp 和Servlet语句基本一样,这里的实现需要用到上篇http://blog.csdn.net/qq_32539825/article/details/70494788里的部分内容 包括UserCoon.java UserDao.java User.java

在这里使用jsp语句也实现了分页内容。

1 login.jsp

<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</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>
    <form action="servlet/Login" method="post">  
用户名:<input type="text" name="username" value=""><br>  
密 码:<input type="password" name="password" value=""><br>  
用户类型:  
<select name="type">
<option value="管理员">管理员</option>
<option value="普通用户">普通用户</option>
</select><br>  
<input type="submit" value="提交">  
<input type="reset" value="取消">  
</form>  
  </body>
</html>
  1. dologin.jsp 首先用javabean得到login中的内容,并调用Insert.java插入到数据库
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>


<%
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>My JSP 'dologin.jsp' starting page</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>
  <%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="user" class="nuc.test.user.User">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
<%
   UserDao usera=new UserDao();
   usera.Insert(user);
 %>

  <body>
</html>

3 queryBena.java 查询数据库中的内容

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
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>My JSP 'queryBean.jsp' starting page</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>
    <%
        UserDao user=new UserDao();
        User usera=new User();
        ResultSet rst=user.Query();
     %>

     <table border=1>
     <tr><td>用户名</td><td>密码</td><td>用户类型</td><td colspan="2" align="center">数据操作</td></tr>
     <%while(rst.next()) {%>
     <tr><td><%=rst.getString("username") %></td><td><%=rst.getString("password") %></td><td><%=rst.getString("type") %></td><td><a href="deleteBean.jsp?id=<%=rst.getString("id")%>">删除操作</a></td><td><a href="updateBean.jsp?id=<%=rst.getString("id")%>">更新操作</a></td></tr>
     <%} %>
     </table>

</body>
</html>

4 deleteBean.jsp 点击查询页面上的删除键 便会超链接到deleteBean.jsp并带着值

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
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>My JSP 'deleteBean.jsp' starting page</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>
   <%  
   UserDao user=new UserDao();
    User usera=new User();
    usera.setId(request.getParameter("id"));
    int rest=user.Delete(usera);

    %>
  </body>
</html>

5 updateBean.jsp 首先将需要更新的一条内容输出到网页上,修改后 点提交 调用doUpdateBean.jsp更新

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

<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'updateBean.jsp' starting page</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>
  <%
  request.setCharacterEncoding("utf-8");
  UserDao user=new UserDao();
  User usera=new User();
  usera.setId(request.getParameter("id"));
  ResultSet rs=user.Select(usera);
  if(rs.next()){
   %>
   <form action="doUpdateBean.jsp?id=<%=rs.getString("id")%>" method="post">
    用户名:<input type="text" value="<%=rs.getString("username") %>" name="username"><br>
    密 码:<input type="text" value="<%=rs.getString("password") %>" name="password"><br>
    用户类型:<select name="type">
   <option value="管理员">管理员</option>
   <option value="普通用户">普通用户</option>
   </select><br>
   <input type="submit" value="提交">
   <input type="reset" value="取消">
   </form>


%} %>

  </body>
</html>

6 doUpdateBean.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
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>My JSP 'doUpdateBean.jsp' starting page</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>
    <%request.setCharacterEncoding("utf-8"); %>
    <jsp:useBean id="use" class="nuc.test.user.User">
    <jsp:setProperty name="use" property="*"/>
    </jsp:useBean>
    <%
    UserDao user=new UserDao();
    //User usera=new User();
    use.setId(request.getParameter("id"));

    int rs=0;
    rs=user.Update(use);
     %>
     <jsp:forward page="queryBean.jsp"/>
  </body>
</html>

7 接下来的下个是用来显示分页的 并且设置每页显示5条信息

Bar.jsp 用来编写分页信息

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>

<%
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>My JSP 'Bar.jsp' starting page</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>
         <%
   int pages;
   int currpage=1;
   if(request.getParameter("page")!=null){
       currpage=Integer.parseInt(request.getParameter("page"));

   }
   Find find=new Find();
   int count=find.SelectCount();
   if(count%User.PAGESIZE==0){
         pages=count/User.PAGESIZE;
   }
   else{
         pages=count/User.PAGESIZE+1;
   }
   StringBuffer sb=new StringBuffer();
   for(int i=1;i<=pages;i++){
      if(i==currpage){
          sb.append("["+i+"]");
      }else{
            sb.append("<a href='showPage.jsp?page="+i+"'>"+i+"</a>");
      }
     sb.append("  ");
   }
   out.print(sb);

   request.setAttribute("bar",sb.toString());

    %>

</body>
</html>

ShowPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>
<%
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>My JSP 'showPage.jsp' starting page</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>
     <table align="center" width="800" border="1">
    <tr>
        <td align="center" colspan="5">
        <h2>所有用户信息</h2>
        </td>
    </tr>
    <tr align="center">
    <td>用户名</td><td>密码</td><td>用户类型</td><td colspan="2">操作方法</td>
    </tr>

    <%
        int currpage=1;
        if(request.getParameter("page")!=null){
            currpage=Integer.parseInt(request.getParameter("page"));

        }
        Find find=new Find();
        List<User> listall=new ArrayList<User>();
        listall=find.Selectcontent(currpage);
        Iterator<User> it=listall.iterator();
        while(it.hasNext()){
               User usera=it.next();

     %>
    <tr align="center">
       <td><%=usera.getUsername() %></td>
       <td><%=usera.getPassword() %></td>
       <td><%=usera.getType() %></td>
       <td><a href="servlet/Update?id=<%=usera.getId()%>">修改</a></td>
       <td><a href="servlet/Delete?id=<%=usera.getId()%>">删除</a></td>
    </tr>
    <%} %>
    <tr>
    <td align="center" colspan="3">
           <jsp:include page="Bar.jsp"/>
    </td>
    </tr>
    <tr align="center"><td colspan="5"><a href="first.jsp">添加用户</a></td></tr>
    </table>

  </body>
</html>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Springmvc学习总结

    4、在web.xml中配置SpringMVC的前端控制器DispatcherServlet

    曼路
  • 关于栈的几个小算法

        想法就是创建一个index变量,index指向含有值得下一个数组空间(假设数组中有两个值,index指向2)

    曼路
  • Python学习总结1--初识Python

    学习Python大家可以到 廖雪峰老师的网站 学习基础知识 下面是我自己学习python的总结和笔记 一、使用Python的优点 1.高...

    曼路
  • 芝麻科技朱智:用线下大数据提高商家运营效率 | 镁客请讲

    镁客网
  • 揭开Spark Streaming神秘面纱⑥ - Spark Streaming结合 Kafka 两种不同的数据接收方式比较

    DirectKafkaInputDStream 只在 driver 端接收数据,所以继承了 InputDStream,是没有 receivers 的

    codingforfun
  • TKE操作指南 - 使用TKE CVM容器集群的业务优势(七)

    2.用户可通过访问services IP或者ingress 域名直接访问容器应用。

    亮哥说TKE
  • Nginx反向代理前端limit限速模块总结

    关于ngx_http_limit_conn_module、ngx_http_limit_req_module 模块,echo(需要安装第三方模块 ngx_htt...

    三杯水Plus
  • Linux-insmod/rmmod/lsmod驱动模块相关命令(10)

    insmod:加载模块 参数: -f  不检查目前kernel版本与模块编译时的kernel版本是否一致,强制将模块载入。 -k  将模块设置为自动卸除。 -m...

    张诺谦
  • 第三期:一起来学SLAM

    激光雷达:通过测量激光信号的时间差、相位差确定距离,通过水平旋转扫描或相控扫描测角度,并根据这两个数据建立二维的极坐标系;再通过获取不同俯仰角度的信号获得第三维...

    点云PCL博主
  • 从炒作到行动:边缘计算的下一步[Openstack]

    在过去的几年里,边缘计算已经逐渐爬上了炒作曲线,现在它站在了为什么我们做新事情和推出新技术的中心。为什么它如此重要,它意味着什么,运动背后的资金在哪里,它对你意...

    用户6667850

扫码关注云+社区

领取腾讯云代金券