使用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>

这里写图片描述

阅读更多

没有更多推荐了,返回首页