Servlect常用对象
ServlectConfig
ServlectContext
HttpServletResponse
HttpServlectRrquest
1 构建上面的项目可以完成,图书界面的注册与登录
2 j进行图书界面的查看图书,购买图书,查看记录
代码编写
package com.Book.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; 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=HomeServlet"); 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.Book.Servlect; 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.Book.Servlect; /** * 浏览数据的主界面 */ 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 = "/HomeServlet",initParams = { @WebInitParam(name="buyBook",value="购买图书"), @WebInitParam(name="selectOrder",value="查看记录"), //增加一个界面为手机管理 @WebInitParam(name="buyBookA",value="购买图书A ") }) public class HomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; String buyBook =""; String buyBookA =""; String selectOrder=""; /** * @see HttpServlet#HttpServlet() */ public HomeServlet() { 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='ShowBuyBookServletA'>"+ buyBookA+"</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.Book.Servlect; 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("<h1 align ='center' style='color:red;font-size:30px'>查看记录</h1>"); pw.print("<h1>当前没有购买读书信息请购买图书信息哦,五秒后跳到图书信息页面</h1>"); response.sendRedirect(" response.sendRedirect('ShowBuyBookServlet');"); }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.Book.Servlect; 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); } }
package com.Book.Servlect; 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; import com.util.BooksDBUtilA; /** * Servlet implementation class ShowBuyBookServlet */ @WebServlet("/ShowBuyBookServletA") public class ShowBuyBookServletA extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowBuyBookServletA() { 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=BooksDBUtilA.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); } }
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 ServlectDengLu */ @WebServlet("/ServlectDengLu") public class ServlectDengLu extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectDengLu() { super(); System.out.println("欢迎来到登陆界面哦"); // 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("<h1 align ='center'>图书BookReading登陆界面</h1>"); pw.append("<div align ='center' style='color':'pink'>"); pw.append("<form action='ServlectDengLuLogic' method='post'>"); pw.append("<h1 align ='center' style='color:red'>欢迎来到登陆界面</h1>"); pw.append("用户名:<input type='text' name='use'>" + "<br>"); pw.append("用户新密码:<input type='password' name='pw'>" + "<br>"); pw.append("请再次输入新密码:<input type='password' name='pws'" + "<br>"+"<br>" ); pw.append("<input type='submit' value='登陆'>"); pw.append("</form>"); pw.append("</div>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package com.DengLu.And.Zhu.Ce; import java.io.IOException; 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 response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @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("uses"); String b=sc.getInitParameter("PW"); String c=sc.getInitParameter("PWS"); System.out.println(a+"@@@@@@@@"+b+"######"+c); response.getWriter().append("ServletB获得静态数据用来访问web.xml"+a); String aa=request.getParameter("use"); String bb=request.getParameter("pw"); String cc=request.getParameter("pws"); System.out.println(aa+":"+bb+":"+cc); //不区分大小写equalsIgnoreCase(aa) if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)) { //重定向跳转页面 System.out.println("登录成功跳转到图书界面"); response.sendRedirect("HomeServlet"); }else { System.out.println("登录失败五秒后返回ServlectDengLuLose"); response.sendRedirect("ServlectDengLuLose"); } } }
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("/ServlectDengLuLose") 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("<h1 align ='center'>BookReading登陆失败的界面</h1>"); pw.append("登陆界面失败请重新登陆"); response.setHeader("refresh","5;URL=ServlectDengLu"); pw.append("<h2 align ='center'><a href='ServlectDengLu'>点击这里返回登陆界面进行重新登陆</a></h2>"); pw.append("如果用户一分钟后没有任何操作则在60秒后调到index.jsp文件"); response.setHeader("refresh","60;URL=insex.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.entity; import java.text.SimpleDateFormat; import java.util.Date; /** * 定义手机名称 手机型号 手机的色彩 价格 * @author MZFAITHDREAM *定义一个标准类 */ public class phone { private String pname;//手机名称 private String pmodel; private String color; private String price; private String land; //手机厚度 public phone() { // TODO Auto-generated constructor stub } public phone(String pname, String pmodel, String color, String price, String land) { super(); this.pname = pname; this.pmodel = pmodel; this.color = color; this.price = price; this.land = land; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPmodel() { return pmodel; } public void setPmodel(String pmodel) { this.pmodel = pmodel; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getLand() { return land; } public void setLand(String land) { this.land = land; } @Override public String toString() { return "phone [pname=" + pname + ", pmodel=" + pmodel + ", color=" + color + ", price=" + price + ", land=" + land + "]"; } public String getTime() { // TODO Auto-generated method stub Date date=new Date(); //2021年10月19日 SimpleDateFormat sdf=new SimpleDateFormat ("YYYY年MM月DD日HH:mm:ss"); //要求当前时间进行格式化 String time=sdf.format(date); return null; } public void setBuyTime(String time) { // TODO Auto-generated method stub //2021年10月19日 } }
package com.util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.entity.Book; /** * 放图书的内容工具类 * @author MZFAITHDREAM *2021/10/19 */ public class BooksDBUtil { public static List<Book> getBooks(){ Book book1 =new Book("java程序设计","自由", "39元","人民日报",""); Book book2 =new Book("java网站设计","马玉", "89元","清华",""); Book book3 =new Book("javascript程序设计","不变", "239元","人民日报",""); Book book4 =new Book("jquery程序设计","胡毅", "189元","江西出版社",""); Book book5=new Book("mysql程序设计","ok", "179元","北大",""); Book book6 =new Book("Sping框架设计","胡三", "129元","人民日报",""); Book book7 =new Book("java高级编程","呼万岁", "159元","河南出版社",""); Book book8=new Book("ps设计","ok", "239元","西安出版社",""); Book book9 =new Book("Sping框架设计","胡三", "129元","人民日报",""); Book book10=new Book("Java程序设计", "马云", "78.0元", "人民邮电出版社", ""); Book book11=new Book("Jsp网络编程", "马化腾", "118.0元", "清华大学出版社", ""); Book book12=new Book("Python爬虫技术", "李彦宏", "65.0元", "电子工业出版社", ""); Book book13=new Book("网页程序设计", "任正非", "38.0元", "江西出版社", ""); Book book14=new Book("MySQL程序教程", "雷军", "59.0元", "吉林出版社", ""); Book book15=new Book("Spring框架技术", "马小云", "46.0元", "人民邮电出版社", ""); List <Book> oBooks=new ArrayList<Book>(); oBooks.add(book1); oBooks.add(book2); oBooks.add(book3); oBooks.add(book4); oBooks.add(book5); oBooks.add(book6); oBooks.add(book7); oBooks.add(book8); oBooks.add(book9); oBooks.add(book10); oBooks.add(book11); oBooks.add(book12); oBooks.add(book13); oBooks.add(book14); oBooks.add(book15); return oBooks; } public static String getNowTime() { Date date=new Date(); //2021年10月19日 SimpleDateFormat sdf=new SimpleDateFormat ("YYYY年MM月DD日HH:mm:ss"); //要求当前时间进行格式化 String time=sdf.format(date); return time; } }
package com.util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.entity.Book; /** * 放图书的内容工具类 * @author MZFAITHDREAM *2021/10/19 */ public class BooksDBUtilA { public static List<Book> getBooks(){ Book book1 =new Book("java基础","自由", "39元","人民日报",""); Book book2 =new Book("html网站设计","马玉", "489元","清华",""); Book book3 =new Book("javascript程序设计","不变", "239元","人民日报",""); Book book4 =new Book("jquery程序设计","胡毅", "189元","江西出版社",""); Book book5=new Book("mysql程序设计","ok", "179元","北大",""); Book book6 =new Book("Sping框架设计","胡三", "129元","人民日报",""); Book book7 =new Book("java高级编程","呼万岁", "159元","河南出版社",""); Book book8=new Book("ps设计","ok", "239元","西安出版社",""); Book book9 =new Book("Sping框架设计","胡三", "129元","人民日报",""); Book book10=new Book("Java程序设计", "马云", "78.0元", "人民邮电出版社", ""); Book book11=new Book("Jsp网络编程", "马化腾", "118.0元", "清华大学出版社", ""); Book book12=new Book("Python爬虫技术", "李彦宏", "65.0元", "电子工业出版社", ""); Book book13=new Book("网页程序设计", "任正非", "38.0元", "江西出版社", ""); Book book14=new Book("MySQL程序教程", "雷军", "59.0元", "吉林出版社", ""); Book book15=new Book("Spring框架技术", "马小云", "46.0元", "人民邮电出版社", ""); List <Book> oBooks=new ArrayList<Book>(); oBooks.add(book1); oBooks.add(book2); oBooks.add(book3); oBooks.add(book4); oBooks.add(book5); oBooks.add(book6); oBooks.add(book7); oBooks.add(book8); oBooks.add(book9); oBooks.add(book10); oBooks.add(book11); oBooks.add(book12); oBooks.add(book13); oBooks.add(book14); oBooks.add(book15); return oBooks; } public static String getNowTime() { Date date=new Date(); //2021年10月19日 SimpleDateFormat sdf=new SimpleDateFormat ("YYYY年MM月DD日HH:mm:ss"); //要求当前时间进行格式化 String time=sdf.format(date); return time; } }
package com.util; import java.sql.Connection; public class Time { public static int time=60*60*60; static Connection time() { while (time>0) { time--; try { Thread.sleep(1000); int hh=time/1/1%1; int mm=time/60%1; int ss=time%6; if(hh==0 &&mm==0 && ss==0) { break; } System.out.println("0小时"+hh+"Сʱ"+mm+"分钟"+ss+"秒"); } catch (InterruptedException e) { // TODO: handle exception }finally { System.out.println("正在跳转页面哦 ServlectB"); System.out.println(".......真在跳转页面 "); } } return null; } public static void main(String[] args) { // TODO Auto-generated method stub time(); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>BookReading</display-name> <welcome-file-list> <!-- <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> --> <welcome-file>index.jsp</welcome-file> <!-- <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> --> </welcome-file-list> <context-param> <param-name>uses</param-name> <param-value>hug</param-value> </context-param> <context-param> <param-name>phone</param-name> <param-value>18172928419</param-value> </context-param> <context-param> <param-name>YM</param-name> <param-value>666666</param-value> </context-param> <context-param> <param-name>PW</param-name> <param-value>123456</param-value> </context-param> <context-param> <param-name>PWS</param-name> <param-value>123456</param-value> </context-param> </web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>BookReading-JSP</title> </head> <body> <h1 style="color: red ;background: yellow;">欢迎来到BookReading管理系统的主界面</h1> <hr> <a href="ServlectZhuZeA" style="color:brown;font-size: 26px">点击这里进入注册界面哦</a> </body> </html>
Servlect真奇妙