Instance:
1. Function description
Implement a simple press release system that includes basic features such as viewing, adding, modifying, and deleting news
2. Specific requirements
(1) Create a database Newssystem, create a table news with the following requirements:
(2) When the program runs, display the ' Publish News ' page (1), enter the relevant content, click the ' Submit ' button to add news content to the database
(3) Click the ' View ' button in Figure 1 to display the ' View News ' page (2), add ' edit ' and ' delete ' link
(4) Click the ' Update ' link in Figure 2 to display the ' Modify News ' page (3), click the ' Modify ' button to confirm the change, click the ' View ' button to display the ' View News ' page (2) To view the results of the changes
(5) Click the ' delete ' link in Figure 2 to delete the news and display the ' View News ' page to display the deleted content (4)
Idea: three-tier architecture anatomy
Display layer:
JSP-formatted files, how many pages are displayed on the page and how many JSP files
This example has figure 1, Figure 2, figure 3 A total of 3 pages, so to build 3 JSP files
Business Logic Layer:
Servlet to place, the inside of the file to achieve the function of adding and deleting changes
Data Access Layer:
Beans and DAO two packages to put
Problem solving: Press 2. Specific requirements for progressive progress
(1) slightly
(2) The File execution order is: Addnews--add.jsp--insertnews
The core code (which appears in this example is the core code):
Addnews
protected void throws servletexception, IOException { request.getrequestdispatcher ("add.jsp"). Forward (Request, Response); }
add.jsp
<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><H1>Press release</H1><formAction= "Insert"Method= "POST">Title:<inputtype= "text"name= "title"><BR><inputtype= "text"name= "Author"><BR>Source:<inputtype= "text"name= "source"><BR>content:<textareaname= "Content"></textarea><BR><inputtype= "Submit"value= "Submit"><ahref= "Show">View</a></form></Body></HTML>
Insertnews
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Get parameters (what user entered in add.jsp)Request.setcharacterencoding ("Utf-8"); News Data=NewNews (); Data.settitle (Request.getparameter ("Title")); Data.setauthor (Request.getparameter ("Author")); Data.setsource (Request.getparameter ("Source")); Data.setcontent (Request.getparameter ("Content")); Date D=NewDate (); Data.settime (d); //working with data (adding new content to the database) Try { NewNewsdao (). Insert (data); } Catch(Exception e) {e.printstacktrace (); } //Jump (return to add.jsp interface after adding data)Response.sendredirect ("Add"); }
(3) The File execution order is: show--show.jsp
Show
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Get parameters (no parameter gets)//working with data (querying all the contents of a database table)Arraylist<news> list=NewArraylist<news>(); Try{List=NewNewsdao (). Select (); } Catch(Exception e) {e.printstacktrace (); } request.setattribute ("Data", list);//load the contents of the query into a packet, the name of the packet called data//jump (go to the page shown in Figure 2)Request.getrequestdispatcher ("show.jsp"). Forward (request, response); }
show.jsp
<body>ArrayList<News> list = (arraylist<news>) request.getattribute ("Data");//take out the data from the show in the data packet . for(News data:list) {//traverse with For Loop%><tr><td><%=data.getnewsid ()%></td><td><%=data.gettitle ()%></td> <td><%=data.getauthor ()%></td><td><%=data.getsource ()%></td><td>< %=data.gettime ()%></td><td><a href= "Edit?id=<%=data.getnewsid ()%>" > Modify </a></ Td>//point to Editnews (named edit) with ID value<td><a href= "Delete?id=<%=data.getnewsid ()%>" onclick= "return confirm (' Confirm to delete <%=data.gettitle ()% >? ') ' > Delete </a></td>//point to Deletenews (named delete) with ID value</tr> <%} %></table> </body>
(4) The File execution order is: Editnews--edit.jsp--updatenews
Editnews
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Get parameters (get ID values from show.jsp)String key=request.getparameter ("id"); //working with DataNews data; Try{Data=NewNewsdao (). Select (key); Request.setattribute ("News", data); } Catch(Exception e) {e.printstacktrace (); } request.getrequestdispatcher ("Edit.jsp"). Forward (request, response); }
edit.jsp
<body><form method= "POST" action= "Update" >News Data= (News) Request.getattribute ("News"));if(Data! =NULL){%><input type= "hidden" name= "NewSID" value= "<%=data.getnewsid ()%>" >Title:<input type= "text" name= "title" Value= "<%=data.gettitle ()%>" ><br><input type= "text" name= "author" value= "<%=data.getauthor ()%>" ><br>Source:<input type= "text" name= "source" value= "<%=data.getsource ()%>" ><br>content:<textarea name= "Content" ><%=data.getcontent ()%></textarea><br><input type= "Submit" Value= "Submit" ><input type= "button" value= "View" ><%}%></form></body>
Updatenews
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("Utf-8"); //Get ParametersNews Data=NewNews (); Data.setnewsid (Integer.parseint (Request.getparameter ("NewSID"))); Data.settitle (Request.getparameter ("Title")); Data.setauthor (Request.getparameter ("Author")); Data.setsource (Request.getparameter ("Source")); Data.setcontent (Request.getparameter ("Content")); Data.settime (NewDate (Request.getparameter ("Time"))); //working with Data Try { NewNewsdao (). Update (data); } Catch(Exception e) {e.printstacktrace (); } //JumpResponse.sendredirect ("Show"); }
(5) File Execution order: Deletenews (only one file is executed)
Deletenews
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//get the parameter (ID show.jsp came from) intNewsid=integer.parseint (Request.getparameter ("id")); //working with Data Try { NewNewsdao (). Delete (NewSID); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); } //jump (jump to Figure 2, jump to show.jsp indirectly via show)Request.getrequestdispatcher ("Show"). Forward (request, response); }
Java EE uses three-tier architecture (display layer, business logic layer, data access layer) to implement data deletion and modification