文章目录
目录
项目文件
1.MyCustomerServlet.java
2.1.Querry.jsp(查询页面)
2.2.reg.jsp(注册页面)
2.3.upxiugai(修改页面)
代码部分
MyCustomerServlet.java
package com.sunguoguo.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sunguoguo.entity.Customer;
import com.sunguoguo.service.CustomerServicelmp;
/**
* Servlet implementation class MyCustomerServlet
*/
@WebServlet("/my")
public class MyCustomerServlet extends HttpServlet {
//调取服务层
CustomerServicelmp cs = new CustomerServicelmp();
HttpServletRequest req;
HttpServletResponse resp;
PrintWriter writer;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
writer = resp.getWriter();
//将请求和响应对象声明的成员变量
this.req=req;
this.resp=resp;
//获取客户端发来的数据
String op = req.getParameter("operation");
//分支判断需要执行什么操作
if(op.equals("query")){
query();
}else if(op.equals("update")){
edit();
}else if(op.equals("updatee")){
editt();
}else if(op.equals("delete")){
delete();
}else if(op.equals("add")){
add();
}else {
System.out.println("默认执行");
};
}
//添加信息
public boolean add(){
//获取页面的值
String accont = req.getParameter("accont");
String password = req.getParameter("password");
String name = req.getParameter("name");
//将数据放入实体类中
Customer customer = new Customer( name, password, new Date(), accont);
boolean b = cs.addCustomer(customer);
if(b){
query();
return true;
}
return false;
}
//删除信息
public boolean delete(){
String cid = req.getParameter("id");
boolean b=cs.Delete(cid);
//删除成功后自动重新查询
if(b){
query();
return true;
}else{
writer.write("删除失败<a href='Querry.jsp'>点击返回查询</a>");
return false;
}
}
//查询信息
public void query(){
List<Customer> list=cs.QueryServiceImp();
//将查询到的数据传入容器
req.setAttribute("l", list);
try {
req.getRequestDispatcher("Querry.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取需要修改信息的实体类
public void edit(){
//先
//请求修改页面
String id = req.getParameter("id");
System.out.println("修改id="+id);
Customer customer = cs.queryObject(id);
req.setAttribute("obj", customer);
//使用请求将对象数据回显示页面上
try {
req.getRequestDispatcher("upxiugai.jsp").forward(req,resp);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//在数据库中修改信息
public void editt(){
String id = req.getParameter("id");
String name = req.getParameter("name");
String accont = req.getParameter("accont");
String password = req.getParameter("password");
Customer customer = new Customer(name, password, new Date(), accont);
//调用方法将数据库中的数据修改
boolean b = cs.updateService(customer, id);
if(b){
System.out.println("修改成功");
query();
}else{
System.out.println("修改失败");
}
}
}
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
<style>
body{
background-color: aqua;
}
.zhuce{
position: relative;
top: 50px;
margin: 0 auto;
width: 300px;
height: 400px;
background-color: aliceblue;
}
.zhuce label {
font-size: 25px;
color: black;
}
.zhuce .shurukuang{
margin-top:20px;
}
.zhuce .shurukuang input{
width: 150px;
height: 20px;
}
.zhuce .anniu{
position: relative;
margin-left: 20%;
margin-top: 20px;
width: 200px;
height: 30px;
}
</style>
</head>
<body>
<div class="zhuce">
<form action="my?operation=add" method="post">
<div style=" height: 100px;"></div>
<div class="shurukuang">
<label for="">名字:</label> <input type="text" name="name" >
</div>
<div class="shurukuang">
<label for="">账号:</label> <input type="text" name="accont" >
</div>
<div class="shurukuang">
<label for="">密码:</label> <input type="password" name="password" >
</div>
<input type="submit" value="注册" class="anniu">
</form>
</div>
</body>
</html>
Querry.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import= "java.util.*"%>
<%@ page import= "java.lang.*"%>
<%@ page import= "com.sunguoguo.entity.Customer"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询</title>
</head>
<body>
<%!
int id;
String username;
String password;
Date time;
String accont;
List<Customer> li;
String ss;
%>
<%
li=(List)request.getAttribute("l");
%>
<h1>客户信息表</h1>
<h6 align='right'><a href='reg.jsp'>点击返回</a></h6>
<table align='center' width='80%' border='1px' cellspacing='0px' cellpadding='0px'>
<tr>
<th>客户编号</th><th>姓名</th><th>密码</th><th>账号</th><th>注册时间</th><th colspan='2'>操作</th>
</tr>
<% for(int i=0;i<li.size();i++){Customer c= li.get(i); int id=c.getId(); String username=c.getUsername();String password=c.getPassword();Date time=c.getTime();String accont=c.getAccont();%>
<tr>
<td><%=id %></td>
<td><%=username%></td>
<td><%=password %></td>
<td><%=accont %></td>
<td><%=time %></td>
<td><a href='my?id=<%=id %>&operation=delete'>删除</a></td><td><a href='my?id=<%=id %>&operation=update'>修改</a></td>
</tr>
<% }%>
</table>
<div>
<h1><=ss></h1>
</div>
</body>
</html>
upxiugai.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import= "java.util.*"%>
<%@ page import= "java.lang.*"%>
<%@ page import= "com.sunguoguo.entity.Customer"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>修改</title>
<style>
body{
background-color: aqua;
}
.zhuce{
position: relative;
top: 50px;
margin: 0 auto;
width: 300px;
height: 400px;
background-color: aliceblue;
}
.zhuce label {
font-size: 25px;
color: black;
}
.zhuce .shurukuang{
margin-top:20px;
}
.zhuce .shurukuang input{
width: 150px;
height: 20px;
}
.zhuce .anniu{
position: relative;
margin-left: 20%;
margin-top: 20px;
width: 200px;
height: 30px;
}
</style>
</head>
<body>
<%!Customer c; %>
<% c=(Customer)request.getAttribute("obj");
%>
<div class="zhuce">
<form action="my?id=<%=c.getId()%>&operation=updatee" method="post">
<div style=" height: 100px;"></div>
<div class="shurukuang">
<label for="">名字:</label> <input type="text" name="name" value="<%=c.getUsername() %>" >
</div>
<div class="shurukuang">
<label for="">账号:</label> <input type="text" name="accont" value="<%=c.getAccont() %>" >
</div>
<div class="shurukuang">
<label for="">密码:</label> <input type="password" name="password" value="<%=c.getPassword() %>">
</div>
<input type="submit" value="修改" class="anniu">
</form>
</div>
</body>
</html>
图片
一.注册
再浏览器输入框中数据路径:localhost:8080/Jsp/reg.jsp 输入---->
二.注册后直接跳转到查询页面
三.点击删除董卓
四.点击修改吕布,来到修改页面
五.修改为貂蝉
谢谢观看~