后端代码区第一部分
package com.DengLu.And.Zhu.Ce; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServlectDengLuLogic */ @WebServlet("/ServlectDengLuLogic") public class ServlectDengLuLogic extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectDengLuLogic() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * do--post */ response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); ServletContext sc=getServletContext(); //获取web.xml文件中的镜头数据 String a=sc.getInitParameter("QQ"); String b=sc.getInitParameter("PSD"); String c=sc.getInitParameter("USER"); System.out.println(a+"@@@@@@@@"+b); response.getWriter().append("ServletB获得静态数据用来访问web.xml"+a); String aa=request.getParameter("qq"); String bb=request.getParameter("psd"); String cc=request.getParameter("user"); //不区分大小写equalsIgnoreCase(aa) if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)) { //重定向跳转页面 System.out.println("登录成功跳转到2021登录成功"); response.sendRedirect("HomeServlet"); /* response.setHeader("refresh","60;URL=Register.jsp"); */ }else { System.out.println("登录失败五秒后返回Register.jsp"); /* response.sendRedirect("Register.jsp"); */ response.setHeader("refresh","3;URL=Register.jsp"); } } }
package com.DengLu.And.Zhu.Ce; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServlectDengLuLose */ @WebServlet("/DengLuChengGong") public class ServlectDengLuLose extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectDengLuLose() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); pw.append("60秒后回到ZhuCe.jsp"); response.setHeader("refresh","60;URL=ZhuCe.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.DengLu.And.Zhu.Ce; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServlectDengLuLogic */ @WebServlet("/ServlectZhuCe") public class ServlectZhuCe extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectZhuCe() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * do--post */ response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); ServletContext sc=getServletContext(); //获取web.xml文件中的镜头数据 String a=sc.getInitParameter("USER"); String b=sc.getInitParameter("QQ"); String c=sc.getInitParameter("PSD"); String d=sc.getInitParameter("SJ"); String e=sc.getInitParameter("HOME"); System.out.println(a+"@@@"+b+"@@@"+c+"@@@"+d+"@@@"+e); response.getWriter().append("<h1>ServlectZhuCe获得静态数据用来访问web.xml</h1>"); String aa=request.getParameter("user"); String bb=request.getParameter("qq"); String cc=request.getParameter("psd"); String dd=request.getParameter("sj"); String ee=request.getParameter("home"); //不区分大小写equalsIgnoreCase(aa) if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)&&d.equalsIgnoreCase(dd)&&e.equalsIgnoreCase(ee)) { //重定向跳转页面 System.out.println("注册成功跳转到2021登录成功"); response.sendRedirect("Login.jsp"); /* response.setHeader("refresh","60;URL=Register.jsp"); */ }else { System.out.println("注册失败五秒后返回Register.jsp"); /* response.sendRedirect("Register.jsp"); */ response.setHeader("refresh","8;URL=ZhuCeLose"); } } }
package com.DengLu.And.Zhu.Ce; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServlectDengLuLose */ @WebServlet("/ZhuCeLose") public class ZhuCeLose extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ZhuCeLose() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setHeader("refresh", "5"); //设置要求浏览器 /* response.sendRedirect("Servlet1"); */ int i=(int)(Math.random()*900000+100000); int a=(int)(Math.random()*9000+1000); int c=(int)(Math.random()*900+100); System.out.println("@@@@"+(new Random().nextInt(900000)+100000)); /* * 设置随机 */ System.out.println(new Random().nextInt(100)); System.out.println(Math.random()); PrintWriter pw =response.getWriter(); pw.print("你的支付宝到账1000万元,需要的码号为:<h1 style='color:red ;background-color:green'>"+i+"</h1>,打死也不说 \"<hr>\";"); pw.print("你的QQ到账1000万元,需要的码号为:<h2 style='color:red ;background-color:green'>"+c+"</h2>,打死也不说 \"<hr>\";"); pw.print("你的微信到账1000万元,需要的码号为:<span style='color:green'>"+a+"</span>,打死也不说"); pw.print("你的手机支付宝到账1000万<span style='color :red'>"+i+"</span>"); pw.append("<style>"); pw.append(" *{\r\n" + " margin: 0px;\r\n" + " padding: 0px;\r\n" + " background: white;\r\n" + " font-size: 30px;\r\n" + " font-weight: bold;\r\n" + " text-shadow: 1px 1px 1px red;\r\n" + "\r\n" + " }"); pw.append("</style>"); pw.append("<h1 align ='center'>2021你年垮2022年QQ登陆失败的界面</h1>"); pw.append("注册界面失败请重新注册"); response.setHeader("refresh","5;URL=Register.jsp"); pw.append("<h2 align ='center'><a href='Register.jsp'>点击这里返回注册界面进行重新注册</a></h2>"); pw.append("如果用户一分钟后没有任何操作则在60秒后调到index.jsp文件"); response.setHeader("refresh","30;URL=index.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
后端代码第二部分
package com.entity; /** * * 图书实体类 * @author MZFAITHDREAM * 2021/10/19 * */ public class Book { private String bookName; //书名 private String autor; //作者 private String price; private String address;//出版社 private String buyTime; //图书时间 //生成get set方法 //null构造方法 创建对象 在创建对象 //toString public Book() { // TODO Auto-generated constructor stub } public Book(String bookName, String autor, String price, String address, String buyTime) { super(); this.bookName = bookName; this.autor = autor; this.price = price; this.address = address; this.buyTime = buyTime; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAutor() { return autor; } public void setAutor(String autor) { this.autor = autor; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBuyTime() { return buyTime; } public void setBuyTime(String buyTime) { this.buyTime = buyTime; } @Override public String toString() { return "Book [bookName=" + bookName + ", autor=" + autor + ", price=" + price + ", address=" + address + ", buyTime=" + buyTime + "]"; } }
package com.Filter; import java.io.IOException; import java.io.OutputStream; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class MyFilter4 implements Filter{ public MyFilter4() { // TODO Auto-generated constructor System.out.println("我创建了第一个MyFilter过滤器"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); System.out.println("我是myfilter开始拦截请求地址哦哦"); } @Override public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub System.out.println("FilterConfig用来加载初始化数据"); } @Override public void destroy() { System.out.println("Filter所有方法运行完毕后销毁过滤器"); } }
package com.Listener; /** * 第三类接口 */ import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class Listener implements HttpSessionBindingListener { private int id; private String username; private String password; public Listener() { // TODO Auto-generated constructor stub } public Listener(int id, String username, String password) { super(); this.id = id; this.username = username; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Listener [id=" + id + ", username=" + username + ", password=" + password + ", getId()=" + getId() + ", getUsername()=" + getUsername() + ", getPassword()=" + getPassword() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } @Override public void valueBound(HttpSessionBindingEvent event) { // TODO Auto-generated method stub System.out.println("HttpSession 与 Listener绑定"); HttpSessionBindingListener.super.valueBound(event); } @Override public void valueUnbound(HttpSessionBindingEvent event) { // TODO Auto-generated method stub System.out.println("HttpSession 与 Listener解绑绑"); HttpSessionBindingListener.super.valueUnbound(event); } }
package com.Servlect; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HomeServlect2 */ @WebServlet("/HomeServlect2") public class HomeServlect2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HomeServlect2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.append("<h1 style='color:red'>登录成功</h1>"); pw.append("<h2>本案例运用的对象\r\n" + "<hr>\r\n" + "ServletConfig:对象获得静态数据\r\n" + "<hr>\r\n" + "ServletContext对象:获取共享静态数据,获取共享动态数据,获取共享文件数据.\r\n" + "<hr>\r\n" + "HttpServletResponse 对象HttpServlet调用HttpResponse的有关方法,生成响应数据\r\n" + "<hr>\r\n" + "HttpServletRequest对象:HttpServlet调用HttpResponse的有关方法,生成响应数据,用于服务器上接收浏览器发送过来的数据信息\r\n" + "<hr>\r\n" + "Cookie对象,对象Session对象。程序把每个用户的数据以Cookie/Session的形式写给用户各自的浏览器。\r\n" + "<hr>\r\n" + "Filter:过滤器,阻止非法网站的访问。\r\n" + "<hr>\r\n" + "Listener:\r\n" + "实现了特定接口的类为监听器,用来监听另一个Java类的方法调用或者属性改变;** 当被监听的对象发生了方法调用或者属性改变后,\r\n" + "监听器的对应方法就会立即执行.\r\n" + "<hr>\r\n" + "九大内置对象的核心表格\r\n" + "<hr>\r\n" + "out JspWriter 将文本信息输出浏览器\r\n" + "<hr>\r\n" + "request HttpServletRequest 获得浏览器发送来的数据\r\n" + "<hr>\r\n" + "respone HttpServletRequest 回复浏览器请求\r\n" + "<hr>\r\n" + "seesion HttpSeesion 在不换浏览器放信息\r\n" + "<hr> \r\n" + "exception Throwable 异常报错\r\n" + "<hr>\r\n" + "page Servlect(this) jsp翻译出来类的对象\r\n" + "<hr>\r\n" + "config ServlectConfig 静态数据\r\n" + "<hr>\r\n" + "application ServlectContext 共享数据\r\n" + "<hr>\r\n" + "pageContext PageContextjsp 管理者引出对象\r\n" + "<hr>\r\n" + "seesion 浏览器不切\r\n" + "application 服务器不关闭\r\n" + "request:请求转发跳转页面的情况,只能两个页面共享数据\r\n" + "pageContext:该对象中只能页面自己用\r\n" + "才能用setAttribute的方法\r\n" + "将四作用域排序\r\n" + "pageContext request seesion application \r\n" + "</h2>"); pw.append("<h1><a href='index.jsp'>返回主界面</a></h1>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.Servlect; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; /** * Servlet Filter implementation class MyFilter1 */ //你要阻止Servlect2 @WebFilter("/Servlect2") public class MyFilter1 implements Filter { /** * Default constructor. */ public MyFilter1() { // TODO Auto-generated constructor stub System.out.println("构造方法"); } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub System.out.println("销毁数据"); } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); System.out.println("我是myfilter1开始拦截请求地址哦哦"); //实施访问 chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("加载数据"); } }
package com.Servlect; /** * 这一个类让我们去了解Servlect的周期 */ import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class ServlectOne implements Servlet{ public ServlectOne() { // TODO Auto-generated constructor stub System.out.println("@1 Servlec方法的构造方法"); } @Override public void destroy() { System.out.println("@2 Servlect方法的销毁"); } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub System.out.println("@ 3Servlect方法加载静态数据"); return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub System.out.println("@ 4 Servlect的getServletInfo方法"); return null; } @Override public void init(ServletConfig config) throws ServletException { // 获取静态数据的对象 String a=config.getInitParameter("SFZ"); System.out.println(a); System.out.println("@5 Servlecto的init方法"); System.out.println("获取静态数据的对象config"); //动态数据的获取 } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("@5 Servlecto的servlect方法"); } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; 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.entity.Book; /** * Servlet implementation class AlertServlet */ @WebServlet("/AlertServlet") public class AlertServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AlertServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Book book=(Book) request.getAttribute("book"); String info="<h1>当前《"+book.getBookName()+"书籍购买成功,如需需要再次购买请<a href='ShowBuyBookServlet'>点击这里</a>回到购书页面," + "或者10秒之后自动跳转至首页</h1>"; response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); // response.setHeader("refresh", "1"+"开始倒计时"); //五秒后自动跳转到HomeServlet response.setHeader("refresh", "10;url=ZhuCe.jsp"); PrintWriter pw=response.getWriter(); pw.print(info); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; 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.entity.Book; import com.util.BooksDBUtil; /** * Servlet implementation class GetBuyBookInfoServlet */ @WebServlet("/GetBuyBookInfoServlet") public class GetBuyBookInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetBuyBookInfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得浏览器发送的下标 String index=request.getParameter("index"); int i=Integer.parseInt(index); //根据下标从书库中定位到用户要购买的是哪本书 List<Book> oBooks=BooksDBUtil.getBooks(); Book book=oBooks.get(i); //获得当前购买的实时时间 String time=BooksDBUtil.getNowTime(); book.setBuyTime(time); //存储至ServletContext对象中 ServletContext sc=getServletContext(); //先判断ServletContext是否有其他的购买记录 //如果有,新的购买记录只需要添加到原有的list集合里面 //如果没有,现在是第一次买书,ServletContext之前没有保存过购买记录,也就意味着该对象中并不存在list集合 Object object=sc.getAttribute("orders"); List<Book> oList=null; if (object==null) { //说明从来都没有保存过购买记录 oList=new ArrayList<Book>(); oList.add(book); }else { //如果以前有保存过购买记录 oList=(List<Book>) object; oList.add(book); } //将更新后的list集合保存至ServletContext对象 sc.setAttribute("orders", oList); //跳转至一个Servlet页面,告诉用户当前购买成功,如需再次购买请点击这里回到购书页面 //如果Servlet之间跳转不需要携带数据,传递数据,一般使用重定向 //否则使用请求转发 request.setAttribute("book", book); request.getRequestDispatcher("AlertServlet").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; /** * 浏览数据的主界面 */ import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HomeServlet */ @WebServlet(urlPatterns = "/HomeServletBook",initParams = { @WebInitParam(name="buyBook",value="购买图书"), @WebInitParam(name="selectOrder",value="查看记录"), //增加一个界面为手机管理 @WebInitParam(name="buyBookA",value="购买图书A ") }) public class HomeServletBook extends HttpServlet { private static final long serialVersionUID = 1L; String buyBook =""; String buyBookA =""; String selectOrder=""; /** * @see HttpServlet#HttpServlet() */ public HomeServletBook() { super(); System.out.println("HomeServlet() 在运行主界面中"); // TODO Auto-generated constructor stub } @Override public void init(ServletConfig config) throws ServletException { super.init(config); //得到二个静态数据 buyBook =config.getInitParameter("buyBook"); buyBookA =config.getInitParameter("buyBookA"); selectOrder =config.getInitParameter("selectOrder"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //当浏览器访问Servlet的组成浏览器能看到的页面 response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); pw.append("<h1 align ='center'>图书界面</h1>"); pw.append("<div align ='center'style=' background-color: #CCFFFF;'><h1 style='color:red'>"); pw.append("<a href='ShowBuyBookServlet'>"+ buyBook+"</a> <br>"); pw.append("<a href='SelectOrderServlect'>"+selectOrder+"</a> <br>"); pw.append("</h1></div>"); System.out.println(buyBook+selectOrder); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } -
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletContext; 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.entity.Book; /** * Servlet implementation class SelectOrderServlect */ @WebServlet("/SelectOrderServlect") public class SelectOrderServlect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectOrderServlect() { } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从servletContext对象中,获取购买图书,并把图书信息在表格中能看到 ServletContext sc=getServletContext(); Object object = sc.getAttribute("orders"); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); //进行判断 ifobject为null if(object==null) { pw.print("<h1align ='center' style='color:red'>查看记录</h1>"); pw.print("<h1>当前没有购买读书信息请购买图书信息!!!!</h1>"); }else { List<Book> oBooks=(List<Book>) object; pw.append("<div align='center'>"); pw.append("<table border='3' style='width:800px;text-align:center;'>"); pw.append("<tr>"); pw.append("<td>书名</td>"); pw.append("<td>作者</td>"); pw.append("<td>单价</td>"); pw.append("<td>出版社</td>"); pw.append("<td>购买时间</td>"); pw.append("</tr>"); //根据循环遍历list集合,从list集合中获得每一本图书信息 for (int i = 0; i < oBooks.size(); i++) { pw.append("<tr>"); pw.append("<td style=' background-color: lavender'>"+oBooks.get(i).getBookName()+"</td>"); pw.append("<td style=' background-color: lightblue'>"+oBooks.get(i).getAutor()+"</td>"); pw.append("<td style=' background-color: pink'>"+oBooks.get(i).getPrice()+"</td>"); pw.append("<td style=' background-color: lightblue'>"+oBooks.get(i).getAddress()+"</td>"); pw.append("<td>"+oBooks.get(i).getBuyTime()+"</td>"); pw.append("</tr>"); } pw.append("</table>"); pw.append("</div>"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; 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.entity.Book; import com.util.BooksDBUtil; /** * Servlet implementation class ShowBuyBookServlet */ @WebServlet("/ShowBuyBookServlet") public class ShowBuyBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowBuyBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从其他类中获取出要显示在浏览器上的所有图书信息 List<Book> oBooks=BooksDBUtil.getBooks(); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); pw.append("<h1 align ='center' style='color:red'>手机浏览界面</h1>"); pw.append("<div align='center'>"); pw.append("<table border='3' style='width:800px;text-align:center;'>"); pw.append("<tr>"); pw.append("<td>书名</td>"); pw.append("<td>作者 </td>"); pw.append("<td>单价</td>"); pw.append("<td>出版社</td>"); pw.append("<td>操作</td>"); pw.append("</tr>"); //根据循环遍历list集合,从list集合中获得每一本图书信息 for (int i = 0; i < oBooks.size(); i++) { pw.append("<tr>"); pw.append("<td style=' background-color: lavender'>"+oBooks.get(i).getBookName()+"</td>"); pw.append("<td style=' background-color: lightblue'>"+oBooks.get(i).getAutor()+"</td>"); pw.append("<td style=' background-color: pink'>"+oBooks.get(i).getPrice()+"</td>"); pw.append("<td style=' background-color: lightblue'>"+oBooks.get(i).getAddress()+"</td>"); pw.append("<td style=' background-color: yellow'><a href='GetBuyBookInfoServlet?index="+i+"'>" + "<input type='button' value='购买' style='background: lightblue;width:100px'></a></td>"); pw.append("</tr>"); } pw.append("</table>"); pw.append("</div>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }