最近在做项目web后台数据增删查改的时候,看到一篇较为详细的经典文章,
所以转载了下来,
文章出处在:https://blog.csdn.net/qq_32539825/article/details/70657340
如果作者认为侵权的告诉我,我立马删。
首先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 pagetitle>
<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">
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>
<%@ 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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dologin.jsp' starting pagetitle>
<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">
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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'queryBean.jsp' starting pagetitle>
<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">
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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'deleteBean.jsp' starting pagetitle>
<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">
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" %>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'updateBean.jsp' starting pagetitle>
<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">
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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doUpdateBean.jsp' starting pagetitle>
<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">
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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Bar.jsp' starting pagetitle>
<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">
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(""+i+"");
}
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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showPage.jsp' starting pagetitle>
<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">
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 listall=new ArrayList();
listall=find.Selectcontent(currpage);
Iterator 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>