运行结果
五秒后自动跳转
代码
package com.Servlect; /** * A202020895 */ 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 ServlectA */ @WebServlet("/ServlectA") public class ServlectA extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectA() { super(); // TODO Auto-generated constructor stub System.out.println("ServlectA()的登录界面"); System.out.println("进入ServlectA的主界面"); } /** * @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' style='color:red'>欢迎来到登陆界面</h1>"); pw.append("<form action=\"ServlectB\" method=\"get\" style=\"background: pink; font-size: 14px;\">\r\n" + " 姓名:<input type=\"text\" name=\"name\"style=\"color: green;\"><br>\r\n" + " 密码:<input type=\"text\" name=\"password\" style=\"color: purple;\"><br>\r\n" + "再次输入密码:<input type=\\\"text\" name=\"passwords\" style=\\\"color: pink;<br>"+ " <input type=\"submit\" value=\"提交\">" + " </form>" + "<h1>autor 实践出总结 </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; /** * A202020895 * 2021/10/19 */ import java.io.IOException; import java.util.Arrays; import java.util.Enumeration; import java.util.Map; import java.util.Map.Entry; import javax.naming.InitialContext; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.omg.PortableInterceptor.RequestInfo; public class ServlectB extends HttpServlet { public ServlectB() { // TODO Auto-generated constructor stub System.out.println("ServlectB在运行"); System.out.println("开试运行代码程序· 进行判断"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取数据 resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); ServletContext sc=getServletContext(); //获取web.xml文件中的镜头数据 String a=sc.getInitParameter("names"); String b=sc.getInitParameter("pws"); String c=sc.getInitParameter("pwsa"); System.out.println(a+"@@@@@@@@"+b+"######"+c); resp.getWriter().append("ServletB获得静态数据用来访问name:"+a); resp.getWriter().append("ServletB获得静态数据用来访问pws:"+b); resp.getWriter().append("ServletB获得静态数据用来访问pwsa:"+b); String aa=req.getParameter("name"); String bb=req.getParameter("password"); String cc=req.getParameter("passwords"); System.out.println(aa+":"+bb+":"+cc); //不区分大小写equalsIgnoreCase(aa) if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)) { //重定向跳转页面 resp.sendRedirect("ServlectA"); }else { resp.sendRedirect("ServlectC"); } } }
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 ServlectC */ @WebServlet("/ServlectC") public class ServlectC extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectC() { super(); System.out.println("登录失败进入ServlectC界面"); // 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' style='color:red'>跳转至ServletC,显示\"登陆失败,用户名不存在或者密码错误,内" + "容不正确重新开始登录</h1>"); response.setHeader("refresh","5;URL=ServlectA"); } /** * @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); } }
第二个项目
建立项目Day_08_JSp
代码内容
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.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=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.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 = "/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>"+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.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.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; 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.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; } }
运行结果
思考一下这俩个小项目如何综合结合起来使用?